Write a MIPS assembly language program that repeatedly prompts the user for a ZI
ID: 3589102 • Letter: W
Question
Write a MIPS assembly language program that repeatedly prompts the user for a ZIP code, calculates the sum of all digits then prints out the result. If the input is 0 then the program stops. The sum calculation part must be written in 2 versions: recursive and non-recursive and the results are stored in two memory locations A and B, respectively. The output should be something that shows both calculations:
The sum of all digits using the recursive function is xxxxxx, using the non-recursive function is yyyyy. xxxxxx is taken from memory location A and yyyyy is from memory location B. Obviously, both numbers must be the same.
No input validation is needed.
(Consider to use the divide (div) instruction to do division then the move from LO and move from HI register (mflo and mfhi, respectively) instructions to get the results (quotient and remainder) from the LO and HI registers.)
Explanation / Answer
.data
prompt: .asciiz "Enter a zip code of integer: "
output: .asciiz " The total sum is: "
.text
.globl main
main:
la $a0, prompt
li $v0, 4
syscall
li $v0, 5
syscall
move $t2, $v0
la $a0, output
li $v0, 4
syscall
Loop:
div $t2, $t2, 10
mflo, $t1
mfhi, $t3
beqz $t1, Exit
add $t1, $t1, 0
b additive
additive:
add $t0, $t1, $t1
j Loop
Exit:
la $a0, output
li $v0, 4
syscall
la $v0, 10
syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.