# This program isolates 3 bits from a bit string and prints them out. # The user must supply two arguments. # # centerbitnumber # The number of the center bit of three bits in the bit string. 0..31 # # edgecontrol # When centerbitnumber specifies one of the edge bits of the entire # bit string, edgecontrol determines what to substitute for the # missing bit to the right or left. 0 or 1. # # If you want to extend the program, you could allow for an optional third # argument to specify the bitstring, instead of using the default string. # You could also extend it so that the bit strings can be more than one # word long. .data bitstring: .word 0xA01F # A sample bit string. This can be any value. theSlice: .word 0 part1: .asciiz "The 3-bit slice of " part2: .asciiz " centered at bit number " part3: .asciiz " is " newline: .asciiz "\n" usageMessage: .asciiz "\nUsage: \n" .text main: subu $sp,48 # create stack frame sw $ra,40($sp) # save return address sw $s4,32($sp) # save $s4 (length(bitstring)) sw $s3,28($sp) # save $s3 (addr(bitstring)) sw $s2,24($sp) # save $s2 (edgecontrol) sw $s1,20($sp) # save $s1 (centerbitnumber) sw $s0,16($sp) # save $s0 (argv) move $s0,$a1 # $s0 = argv bne $a0,2,usageError # must have exactly two arguments (all integers) # decode centerbitnumber lw $a0,0($s0) # $a0 = address(1st argument string) = argv[0] jal decode_int move $s1,$v0 # store the result ($v0) as centerbitnumber ($s1) # decode edgecontrol lw $a0,4($s0) # $a0 = address(2nd argument string) = argv[1] jal decode_int move $s2,$v0 # store the result ($v0) as edgecontrol ($s2) # get addr(bitstring), length(bitstring) la $s3,bitstring # the default bitstring li $s4,32 # the length of the default bitstring move $a0,$s1 # $a0 = centerbitnumber move $a1,$s2 # $a1 = edgecontrol move $a2,$s3 # $a2 = addr(bitstring) move $a3,$s4 # $a3 = bit string length jal bitSlice3 sw $v0,theSlice # store the result # print: The 3-bit slice of bitstring centered at centerbitnumber is bitslice3 la $a0,part1 # "The 3-bit slice of" li $v0,4 # print_string syscall # move $a0,$s4 # number of bits in bitstring move $a1,$s3 # addr(bitstring) jal printBitString # print the bit string la $a0,part2 # "centered at" li $v0,4 # print_string syscall # move $a0,$s1 # center bit number li $v0,1 # print_int syscall # la $a0,part3 # "is" li $v0,4 # print_string syscall # li $a0,3 # 3 bits in slice la $a1,theSlice # address of the slice storage jal printBitString # la $a0,newline # \n li $v0,4 # print_string syscall # all_done: lw $ra,40($sp) # lw $s4,32($sp) # lw $s3,28($sp) # lw $s2,24($sp) # lw $s1,20($sp) # lw $s0,16($sp) # addu $sp,$sp,48 # j $ra # # Print the usage error message usageError: la $a0,usageMessage # usage error li $v0,4 # print_str syscall # j all_done # # --------------------------------------------------------------------------- # bitSlice3 function # get three bits out of a bit string # a0 offset from 0 to the center bit, 0..(bit string length - 1) # a1 edge control, 0=> fill edge with 0, 1 => fill edge with 1 # [optional: -1 => wrap bit from the other end of the bit string] # a2 address(bit string), word aligned # a3 bit string length, must be > 0 # [optional: For the assignment, you can ignore $a3 and use 32 as the # bit string length in your bitSlice3 function.] .text # Project 1: Implement this procedure bitSlice3: li $v0,0 # j $ra # return to caller # --------------------------------------------------------------------------- # Converts an integer value from a textual representation to its machine representation # Requires: # a0 the address of the start of the textual representation # Must be a null terminated string. # Returns: # v0 the machine representation of the integer value # -1 => decode error .text # Project 1: Implement this procedure decode_int: move $v0,$zero # cumulative decoded value = 0 jr $ra # return # --------------------------------------------------------------------------- # printBitString function # a0 number of bits to print # a1 address(bit string), word aligned # t0 lots of uses # t1 current word offset into the bit string # t2 current word contents # t3 current bit offset # t4 address of 2-word array containing addresses of "0", "1" strings .data zeroStr: .asciiz "0" oneStr: .asciiz "1" strings: .word zeroStr,oneStr .text printBitString: blez $a0,exitBitString # quit if nothing to print addi $t0,$a0,-1 # t0 = bitcount - 1 srl $t1,$t0,5 # t1 = word offset = (bitcount-1)/32 sll $t1,$t1,2 # make it a word address andi $t3,$t0,0x1F # t3 = bit offset = (bitcount-1) & 0x1F la $t4,strings # t4 = output strings address array pBSOuter: add $t0,$a1,$t1 # address(current word) lw $t2,0($t0) # t2 = value(current word) pBSInner: srlv $t0,$t2,$t3 # shift the bit andi $t0,1 # mask the bit sll $t0,$t0,2 # make it a word offset add $t0,$t0,$t4 # addr(addr(string)) lw $a0,0($t0) # addr(string) li $v0,4 # print_string syscall # addi $t3,$t3,-1 # bit offset = bit offset - 1 bgez $t3, pBSInner # loop if more bits li $t3,31 # bit offset = 31 addi $t1,$t1,-4 # word offset = word offset - 4 bgez $t1, pBSOuter # loop if more words exitBitString: jr $ra #