Exam 3 bonus Programming Problem Write the program in Notepad® and run it succes
ID: 3712678 • Letter: E
Question
Exam 3 bonus Programming Problem
Write the program in Notepad® and run it successfully in SPIM before uploading it to eCampus as a text file with .s extension. If not running correctly, include comments on what assembly error or output you are getting.
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
.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) "
.text
main:
li $v0,4
la $a0,prompt
syscall
input:
li $v0 5
syscall
move $t1 $v0
#
push:
beq $t3 0 pop
rem $t3 $t1 2
mov $s0 $t3
sw $s0 0x0($sp)
addi $sp $sp -4
div $t3 $t3 2
pop:
li $v0 4
la $a0 ans
syscall
beq $sp 0 exit
addi $sp $sp +4
lw $t4 ($sp)
li $v0 4
la $a0 $t4
syscall
#
bgt $t1 127 l1
blt $t1 -128 l1
l1:
li $v0,4
la $a0,err
syscall
j l2
l2:
li $v0 4
la $a0 newin
syscall
#respond
li $v0 5
syscall
move $t2 $v0
beq $t2 'y' input
j exit
exit:
li $v0,4
la $a0,term
li $v0 10
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.