In: Computer Science
Write MIPs program that will read two integers from the user and compute for the sum and difference of the two integers. Ask the user whether he wants to repeat the program : "[Y/y] / [N/n] ?".
Greetings!!
Code:
#DATA SEGMENT
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
sum: .asciiz "\nThe sum is: "
dif: .asciiz "\nThe difference is: "
rep: .asciiz "\nDo you want to continue? Y/y | N/n: "
#CODE SEGMENT
.text
main:
#MAIN STARTS HERE
#DISPLAY PROMPT1
repeat:
la $a0,prompt1
li $v0,4
syscall
#READ NUMBER1
li $v0,5
syscall
move $t0,$v0
#DISPLAY PROMPT2
la $a0,prompt2
li $v0,4
syscall
#READ NUMBER2
li $v0,5
syscall
move $t1,$v0
#CALCULTE THE SUM AND DIFFERENCE
add $t2,$t0,$t1
sub $t3,$t0,$t1
#DISPLAY SUM IS
la $a0,sum
li $v0,4
syscall
#DISPLAY SUM
add $a0,$0,$t2
li $v0,1
syscall
#DISPLAY DIFFERENCE IS
la $a0,dif
li $v0,4
syscall
#DISPLAY DIFF
add $a0,$0,$t3
li $v0,1
syscall
#DISPLAY CONTINUE?
la $a0,rep
li $v0,4
syscall
#READ OPTIONS
li $v0,12
syscall
beq $v0,'Y',repeat
beq $v0,'y',repeat
#EXIT
li $v0,10
syscall
Output screenshot:
Hope this helps