Write a program using MIPS (MARS Simulator) assembly language to divide an unsig
ID: 3669270 • Letter: W
Question
Write a program using MIPS (MARS Simulator) assembly language to divide an unsigned integer x, by another unsigned integer y, and produce a quotient and a remainder.
Use repeated subtraction to carry out the division. The algorithm is trivial, and does not need much explaination. If x = 25 and y = 9 then you can subtract 9 at most twice from 25 (quotient will be 2) and the remainder after the subtraction will be 7.
The user should be able to enter a number between 0 and 255 after the prompts "Enter x" and "Enter y" are displayed on the screen. The results will be displayed on the screen as "Quotient=" and Remainder="
Explanation / Answer
This below MIPS code will ask the user to enter a and b then will calculate the remainder and quotient ..It may be helpul to solve the above question or let me know by writing a comment ..
# Given positive integers a and b, output a/b and a%b.
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz "a/b = "
str4: .asciiz "a%b = "
str5: .asciiz "a*b = "
newline: .asciiz " "
.text
main: li $v0, 4 # system call code for print_string
la $a0, str1 # address of str1
syscall # print str1
#get the first number from user, put it into $s0
li $v0, 5 # system call code for read_int
syscall # read an integer into $v0 from console
add $s0, $v0, $zero # copy $v0 into $s0 (a)
#read print_string for str2
li $v0, 4 # system call code for print_string
la $a0, str2 # address of str1
syscall # print str1
# get second number from user, put it into $t1
li $v0, 5 #load syscall for read_int
syscall #make the syscall
move $s1, $v0 #move the number read into $s1(b)
#DO THE CALCULATIONS................................................
div $s0, $s1 #diving $s0 by $s1
mflo $t0 #storing value of lo(quotient) in
#register $t0
mfhi $t1 #storing value of hi(remainder) in
#register $t1
mult $s0, $s1
mflo $t2
li $v0,1
move $a0, $t2
syscall
li $v0,4
la $a0, str5
syscall
#read print_string for str3
li $v0, 4 # system call code for print_string
la $a0, str3 # address of str1
syscall # print str1
#print a/b
li $v0, 1 #load syscall print_int into $v0
move $a0, $t0 #move the number to print into $t2
syscall
# read print string for str4
li $v0, 4
la $a0, str4
syscall
# print remainder
li $v0, 1
move $a0, $t1
syscall
#end of program
li $v0, 10 #system call code for exit
syscall
mips
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.