MIPS/SPIM programming Your program is to print the binary representation of a de
ID: 3719476 • Letter: M
Question
MIPS/SPIM programming
Your program is to print the binary representation of a decimal number using a divide by 2 conversion routine.
1. Use syscall 4 to prompt for a number between -128 and 127 as listed in the data declaration label prompt:
2. Input a number (using syscall 5)
3. If the number is not within the limits, print message at label err: and try again.
4. Convert this decimal number to binary and output its binary representation to the console using divide by 2 method.
4a. Push each remainder on the stack then pop them and print them using syscall 1. A header as listed in the data declaration label ans: should precede that.
4b. The magnitude of the number will always be 8 bits with MSB =1 for negative numbers and 0 for positive numbers.
5. The program will continue prompting for a number as long as y is entered when prompted as listed in the data declaration label newin:
5a. If any other character is entered, the program prints the message at label term: in the data declaration and exits the program.
Hint: The 8-bit binary representation of a negative number m is the same as that of positive number equal to 256 + m.
The beginning of the program and the data declaration are given below
# Program to convert a number between -128 and 127 to its binary equivalent and
# outputs its binary representation to the console.
.text
main:
.data
prompt: .asciiz " Input a decimal number between -128 and 127: "
ans: .asciiz " Binary representation is "
err: .asciiz " input error - try again!"
term: .asciiz " Program terminated"
newin: .asciiz "Do you want to try another number? (y for yes) "
Explanation / Answer
Here is the assembly code for the given problem
.data
msg: .asciiz "Enter a number to convert : "
.text
li $v0, 4
la $a0, msg
syscall
li $v0, 5
syscall
add $t0, $zero, $v0 # Get number read from previous syscall
li $t1, 31 #bits to shift
Loop:
blt $t1, 0, EndLoop
srlv $t2, $t0, $t1 #shift the bit to right
and $t2, 1 #extract bit by ANDDING 1
li $v0, 1
move $a0, $t2
syscall
#decrement the bit number
sub $t1, $t1, 1
b Loop
EndLoop:
li $v0, 10
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.