In: Computer Science
in MIPS assembly, ask a user for two numbers and to either enter a '1' for addition or '2' for subtraction. using jal, jump to the appropriate procedure for additing or subtracting. then print out result and ask if there is another calculation (terminate program if 'no').
Program:
.data
msg1: .asciiz "\nEnter number 1: "
msg2: .asciiz "Enter number 2: "
msg3: .asciiz "Enter operation to be performed
(1-addition,2-subtraction): "
error_msg: .asciiz "\n\nInvalid input exitig the
program."
result: .asciiz "The result is: "
repeat: .asciiz "\nDo you want to perform another
calculation?\n(Enter y for yes or n for no)\n"
char: .space 1
yes: .asciiz "y"
no: .asciiz "n"
.text
.globl main
main:
again: li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
move $t0,$v0 #$t0 = number1
li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
move $t1,$v0 #$t1 = number2
li $v0,4
la $a0,msg3
syscall
li $v0,5
syscall
move $t2,$v0
beq $t2,1,addtn
beq $t2,2,subtn
li $v0,4
la $a0,error_msg
syscall
b exit
addtn: add $t3,$t0,$t1
b next
subtn: sub $t3,$t0,$t1
next: li $v0,4
la $a0,result
syscall
li $v0,1
move $a0,$t3
syscall
li $v0,4
la $a0,repeat
syscall
li $v0,12
syscall
sb $v0,char
lb $t0,yes
lb $t1,no
beq $v0,$t0,again
beq $v0,$t1,exit
li $v0,4
la $a0,error_msg
syscall
exit:
OUTPUT:'