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 . Be safe .
Note : check attached image for output ,code compiled and tested in MARS MIPS simulator .
Program Plan :
Program :
.data #variable section
#string declaration to print and prompt user
number1 : .asciiz "\nEnter first non negative number :
"
number2 : .asciiz "Enter second non negative number :
"
byZero : .asciiz "divide by zero error.\n"
multiply : .asciiz "multipication is : "
divide : .asciiz "\ndivide is : "
done : .asciiz "Good bye!"
.text #data section
main:
#load address of number 2 in $a0 to
print string
la $a0,number1
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,number2
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,divideByZero #if s1==0 go
to divideByZero s1 = secondnumber
li $t0,1 #t0=1
addi $t1,$s0,0 #t1=$s0(first number
)
loop1: #loop1 label
bge $t0,$s1,printMultiply #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 loop1 #jump to loop1
#print multiply
printMultiply:
#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 )
loop2: #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 loop2
printDivide: #print divide label
#print divide
string
la
$a0,divide
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
divideByZero: #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,byZero #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,done #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 up vote ,comment if any query .