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 up vote ,comment if any query . Thanks for Question .
Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .
Program Plan :
Program :
.data
#variable declare to use in .text section
firstNumber : .asciiz "\nEnter first non negative
number : "
secondNumber : .asciiz "Enter second non negative
number : "
divideByZero : .asciiz "divide by zero error
occured.\n"
multiply : .asciiz "multipication is of user input
numbers : "
quotient : .asciiz "\nQuotient of user input numbers :
"
exitP : .asciiz "Good bye!"
.text #data section
main:
#load address of number 2 in $a0 to
print string
la $a0,firstNumber
li $v0,4 #syscall 4 to print
string
syscall
li $v0,5 #syscall 5 to read integer
from user
syscall
addi $s0,$v0,0 #move user input
from $v0 to $s0 = $s0=$v0+0
#load address of number 2 in $a0 to
print string
la $a0,secondNumber
li $v0,4 #syscall 4 to print
string
syscall
li $v0,5 #syscall 5 to read input
from user
syscall
addi $s1,$v0,0 #move user input
from $v0 to $s1 = $s1=$v0+0
beq $s1,0,divideByZeroError #if
s1==0 go to divideByZero s1 = secondnumber
li $t0,1 #t0=1
addi $t1,$s0,0 #t1=$s0(first number
)
multiplyLoop: #loop1 label
bge $t0,$s1,printMultipication #if
$t0>=$s1 go to printMultiply label
add $t1,$t1,$s0 #add first number
to $t1 = $t1+$s0
addi $t0,$t0,1 #increment loop
count
j multiplyLoop #jump to loop1
#print multiply
printMultipication:
#print multiply
string
la
$a0,multiply
li $v0,4
#syscall 4
syscall
#move
multipication value from $t1 to $a0
addi
$a0,$t1,0
li $v0,1
#syscall 1 to print integer
syscall
li $t0,0 #t0=0
addi $t1,$s0,0 #$t1=$s0 (first
number )
divideLoop: #loop2 label
blt $t1,$s1,printDivide #if
$t1<$s1 go to printDivide label
sub $t1,$t1,$s1 #subtract
$t1=$t1-s1
addi $t0,$t0,1 #increment
quotient
j divideLoop
printDivide: #print divide label
#print divide
string
la
$a0,quotient
li $v0,4
#syscall 4
syscall
#move quotient
value from $t0 to $a0
addi
$a0,$t0,0
li $v0,1
#syscall 1 to print integer
syscall
j main #jump to main again
divideByZeroError: #divide by zero
#we already know second number is
zero
#if first number is also zero exit
program and print good bye message
beq $s0,0,exit
#if first number not zero but
second number is zero print divide by Zero message
la $a0,divideByZero #load address
of byZero string in $a0
li $v0,4 #syscall 4 to print
string
syscall
j main #again jump to main label to
get input
exit:#exit label
la $a0,exitP #print good bye
message
li $v0,4 #syscall 4 to print
string
syscall
li $v0,10 #syscall 10 to terminate
program
syscall #syscall call
Output :
Please thumbs up ,comment if any query . Be safe .