In: Computer Science
Write a MIPS assembly program to repeatedly
read two non-negative integers and print the integer product and
quotient without using the multiplication and division
instructions. Each input number will be entered on a separate line.
Your program will terminate when two zeroes are entered by the
user. If only the second number in a pair is zero, then a
divide-by-zero error message should be printed for the division
operation. Use comments generously in your program. Test your
program with the following sets of values:
18 6
16 5
5 11
21 4
2 16
17 2
0 10
10 0
20 5
6 6
0 0
Hand in the program with output.
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
code.asm
.data
prompt: .asciiz "\nEnter the numbers pairs below"
prompt1: .asciiz "Product : "
prompt2: .asciiz " Quotient : "
prompt3: .asciiz "\nNum1 : "
prompt4: .asciiz "Num2 : "
.globl main
.text
main:
li $v0,4
la $a0,prompt #it will print prompt
syscall
loop2:
li $v0,4
la $a0,prompt3 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t1,$v0 #save a to t1
li $v0,4
la $a0,prompt4 #it will print prompt
syscall
li $v0,5
syscall #ask user input
move $t0,$v0 #save a to t1
beq $t0,$zero,checkNext
j skipCheck
checkNext:
beq $t1,$zero,exitCode
skipCheck:
li $t2,0 #quotient
move $t3,$t1 #product
loopDiv:
blt $t1,$t0,exit #load while $t1 >= $t0
sub $t1,$t1,$t0
add $t2,$t2,1 #increase quotient by one
j loopDiv
exit:
li $t4,0 #store sum
li $s0,0 #load index to -
loopMul:
bge $s0,$t0,exit2 #load while $t1 >= $t0
add $t4,$t4,$t3 #add number
add $s0,$s0,1 #increase quotient by one
j loopMul
exit2:
#extra code to print both valuesli $v0,1
li $v0,4
la $a0,prompt1 #it will print prompt
syscall
li $v0,1
move $a0,$t4
syscall #print quotient
li $v0,4
la $a0,prompt2 #it will print prompt
syscall
li $v0,1
move $a0,$t2
syscall #print quotient
j loop2
exitCode:
li $v0,10
syscall