1. Write a MIPS subprogram called “addtwo” which takes two integer arguments and
ID: 3588213 • Letter: 1
Question
1. Write a MIPS subprogram called “addtwo” which takes two integer arguments and adds their values. Then write a main program that uses this subprogram to add the values in $t0 and $t1 and print out the sum. Set the value of $t0 to 5 and $t1 to 9.
2. Write a MIPS subprogram called “gthan” which takes two integer arguments and determines the greater of the two integers. Then write a main program that uses the subprogram to determine if $t0 or $t1 has the greater number. If $t0 is greater than $t1, print 0 else print 1. Set the value of $t0 to 7 and $t1 to 15.
Explanation / Answer
Given below are the mips program for the question. Please don't forget to rate the answer if it helped. Thank you.
Answer 1
.data
msg: .asciiz "The sum is "
.text
#initailize $t0 = 5 and $t1 = 9
addi $t0, $zero, 5
addi $t1, $zero, 9
#pass arguments to addTwo
move $a0, $t0
move $a1, $t1
jal addTwo
#display the result from $v0
move $t2, $v0
li $v0, 4
la $a0, msg
syscall
#show the sum from $t2
li $v0, 1
move $a0, $t2
syscall
#exit
li $v0, 10
syscall
#---------------------------------------
#addTwo : takes 2 int arguments in $a0 and $a1
# It returns the sum of the 2 numbers in $v0
#---------------------------------------
addTwo:
add $t2, $a0, $a1 #t2 = $a0 + $a1
move $v0, $t2 #store the result in $v0 and return
jr $ra
output
The sum is 14
-- program is finished running --
Answer 2
.data
.text
#initailize $t0 = 7 and $t1 = 15
addi $t0, $zero, 7
addi $t1, $zero, 15
#pass arguments to gthan
move $a0, $t0
move $a1, $t1
jal gthan
#get the return value from $v0, the greater number
move $t2, $v0
#compare $t0 to $t2, the greater number
beq $t0, $t2, printZero
#display 1 when t1 is greater
li $v0, 1
li $a0, 1
syscall
b exit
printZero:
#display 0 when t0 is greater
li $v0, 1
move $a0, $zero
syscall
exit:
#exit
li $v0, 10
syscall
#---------------------------------------
#gthan : takes 2 int arguments in $a0 and $a1
# It returns the greater of the 2 numbers in $v0
#---------------------------------------
gthan:
bgt $t0, $t1, t0greater
#move t1 as the return value
move $v0, $t1
b finish
t0greater:
move $v0, $t0
finish:
jr $ra
output
1
-- program is finished running --
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.