Write an Assembly language program in MARS simulator to get two integers from th
ID: 3545000 • Letter: W
Question
Write an Assembly language program in MARS simulator to get two
integers from the user and perform 4 arithmetic operations (addition, subtraction,
multiplication, division) on these integers. Your program should get user input for which
arithmetic operation to perform on the integer numbers. For this, you may display a menu to
let user select from one of these options:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Quit
Based on what the user has selected, perform this operation. Once an operation is performed,
your program CAN quit execution.
Please use subroutines to implement this program.
Explanation / Answer
please rate - thanks
I did integer division--if that's not good let me know--will change it
would have been much shorter without functions
.data
menu: .asciiz "1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Quit >>"
getfirst: .asciiz "Enter first number: "
getsecond: .asciiz "Enter second number: "
invalid: .asciiz "invalid choice "
solution: .asciiz " solution: "
nl: .asciiz " "
.text
.globl main
main:
jal getnumbers
doit:
jal getoperation
li $t1,1 #add?
beq $v0,$t1,addd
li $t1,2 #sub?
beq $v0,$t1,subb
li $t1,3 #mult?
beq $v0,$t1,multt
li $t1,4 #div?
beq $v0,$t1,divv
li $t1,5 #quit
beq $v0,$t1,done
la $a0,invalid
li $v0,4
syscall
j doit
addd:
jal addition
j print
subb:
jal subtract
j print
multt:
jal multiply
j print
divv:
jal divide
print:
li $v0,4
la $a0,solution
syscall
li $v0,1 #print result
move $a0,$a1
syscall
li $v0,4
la $a0,nl #followed by new line
syscall
j doit
done:
li $v0,10 #exit program
syscall
#this function gets 2 numbers
getnumbers:
li $v0,4
la $a0, getfirst #get first number
syscall
li $v0,5
syscall
move $s0,$v0 #save in s0
li $v0,4
la $a0, getsecond #get 2nd number
syscall
li $v0,5
syscall
move $s1,$v0 #save in s1
jr $ra #return
#this function gets the operation
getoperation:
li $v0,4
la $a0, menu #print the menu
syscall
li $v0,5 #get the number
syscall
j $ra
addition:
add $a1,$s0,$s1
j $ra
subtract:
sub $a1,$s0,$s1
j $ra
multiply:
mult $s0,$s1
mflo $a1
j $ra
divide:
div $s0,$s1
mflo $a1
j $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.