Write a complete MIPS program that prompts the user for two integer (in a subrou
ID: 3889368 • Letter: W
Question
Write a complete MIPS program that prompts the user for two integer (in a subroutine - call it twice), calculates the product of the integers (also in a subroutine) and prints the product with simple descriptive text (also in a subroutine). You will have three little subroutines. Use the "simple linkage convention" (no stack use, use appropriate registers for arguments and return values). Do not worry about branch delays. Some syscall codes you may need are: print string, code 4 in $v0, string address in $a0; read integer, code 5, value returned in $v0; print integer, code 1, value in $a0; end program, code 10.
Explanation / Answer
CODE: (Explanation in comments)
.data
numbers: .space 8 # set aside 8 bytes for 2 integers
result: .space 4 # set aside 4 bytes for result
prompt: .asciiz "Enter a number "
prompt2: .asciiz "The result is "
.globl main
.text
main:
li $t0, 0 # set offset to 0
jal getnum # call function to read integer
addi $t0, $t0, 4 # increment offset to next word
jal getnum # call function to read integer
jal product # call function to calculate product
jal print # call print function
li $v0, 10 # set command to exit
syscall # end program
getnum:
li $v0, 4 # set command to print string
la $a0, prompt # load address of string
syscall # print string
li $v0, 5 # set command to read integer
syscall # prompt user to read integer
sw $v0, numbers($t0) # store read value in numbers location with offset $t0
jr $ra # return from call
print:
li $v0, 4 # set command to print string
la $a0, prompt2 # load address of string
syscall # print string
li $v0, 1 # set command to print product
lw $a0, result # load product into $a0
syscall # print the product
jr $ra # return from call
product:
lw $t0, numbers+0 # load num1 into register $t0
lw $t1, numbers+4 # load num2 into register $t1
mul $t2, $t0, $t1 # calculate their product in register $t2
sw $t2, result # store the product in result
jr $ra # return from call
OUTPUT:
Enter a number 5
Enter a number 6
The result is 30
-- program is finished running --
Enter a number 9
Enter a number 20
The result is 180
-- program is finished running --
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.