# integer.s -- investigate integer rep'ns for lab # This program asks the user to enter an integer, and then # it will print this number according to various encoding schemes. # We assume the number of bits to print is 32, and we use # the bitprint procedure similar to the one in demo.s # .data blank: .asciiz " " newline: .asciiz "\n" int_prompt: .asciiz "Please enter an integer: " twos_intro: .asciiz "The 2's complement representation is: " ones_intro: .asciiz "The 1's complement representation is: " unsigned_intro: .asciiz "The unsigned representation is: " sm_intro: .asciiz "The sign-magnitude representation is: " biased_intro: .asciiz "The biased-(2^31) representation is: " # Purpose of registers # $s0 = user's number, the 2's complement rep'n that should not change # $s1 = 1's complement # $s2 = unsigned # $s3 = sign-magnitude # $s4 = biased (2^31) .text main: li $v0, 4 # print prompt la $a0, int_prompt syscall li $v0, 5 # get integer from user and put it in $s0 syscall move $s0, $v0 #------------------------------------- # TWO'S COMPLEMENT # There's really nothing to do to handle 2's complement # because the hardware uses 2's complement rep'n by default li $v0, 4 # introduce 2's complement representation la $a0, twos_intro syscall move $a0, $s0 # print this number in 2's complement jal bitprint #------------------------------------- # ONE'S COMPLEMENT # Let $s1 contain the 1's complement representation. # ou need to modify the binary representation of $s0 so that it will # represent the 1's complement rep'n of the same number. # Here's a hint: If the number is positive, no change is needed. # But what should we do if the number is negative? li $v0, 4 # introduce 1's complement representation la $a0, ones_intro syscall move $s1, $s0 move $a0, $s1 # print this number in 1's complement jal bitprint #--------------------------------------- # UNSIGNED # Let $s2 contain the unsigned representation of the number # whose 2's complement representation is in $s0. In other words, # you need to modify the binary representation of $s0 # so that it is really the unsigned binary rep'n of the # original number. Here's a hint: if the number is # negative, print an error message because such a rep'n # does not exist. li $v0, 4 # introduce unsigned representation la $a0, unsigned_intro syscall move $s2, $s0 move $a0, $s2 # print this number in unsigned jal bitprint #--------------------------------------- # SIGN-MAGNITUDE # Let $s3 contain the sign-magnitude representation of the number # whose 2's complement representation is in $s0. # If the number is positive, there is really nothing to change, # so the real issue is what to do if the number is negative. li $v0, 4 # introduce sign magnitude representation la $a0, sm_intro syscall move $s3, $s0 move $a0, $s3 # print this number in sign magnitude jal bitprint #--------------------------------------- # BIASED # Let $s4 contain the biased 2^31 representatin of the number # whose 2's complement representation is in $s0. # Why such a big bias? Because we essentially want to have the # same range of values, -2^31 to 2^31 - 1. Note that biased # is a lot like unsigned. li $v0, 4 # introduce 2's complement representation la $a0, biased_intro syscall move $s4, $s0 move $a0, $s4 # print this number in biased jal bitprint #---------------------------------------- li $v0, 10 # end of program syscall #-------------------------------------------------------------------------- # procedure bitprint - print the 32 bits of the number $a0, and put a space # after every 8 bits for readability # # $s3 = result of remainder calculation # $s4 = number to display copied from $a0 # $s5 = mask # $s6 = loop counter # $s7 = bitwise result bitprint: sw $s3, 0($sp) # save registers $s3-$s7 on stack sw $s4, -4($sp) sw $s5, -8($sp) sw $s6, -12($sp) sw $s7, -16($sp) addi $sp, $sp, -20 move $s4, $a0 # copy parameter into $s4 li $s3, 1 sll $s5, $s3, 31 # put 1 in most significant bit (leftmost bit) li $s6, 0 loop: bge $s6, 32, endloop # done with loop? and $s7, $s5, $s4 # is most significant bit = 0? beqz $s7, put_zero li $v0, 1 # print bit '1' li $a0, 1 syscall j after_put_zero put_zero: li $v0, 1 # print bit '0' li $a0, 0 syscall after_put_zero: addi $s6, $s6, 1 # increment loop counter sll $s4, $s4, 1 # shift number left by 1 bit position rem $s3, $s6, 8 # compute the mod by 8 to see if we need space bnez $s3, loop li $v0, 4 # print space la $a0, blank syscall j loop endloop: li $v0, 4 # print newline la $a0, newline syscall addi $sp, $sp, 20 # restore the saved registers lw $s7, -16($sp) lw $s6, -12($sp) lw $s5, -8($sp) lw $s4, -4($sp) lw $s3, 0($sp) jr $ra # return