There is a recursive version of this algorithm also shown on that page. You are
ID: 3839182 • Letter: T
Question
There is a recursive version of this algorithm also shown on that page. You are NOT to implement the recursive version for this assignment. You are to write a MIPS function named "gcd". The function will accept two parameters representing the two integers m and n. It will return the GCD of those two numbers. You should then write a main program that asks the user to input a numerator and a denominator of a fraction. The program should then call the "gcd" function to obtain the GCD of the two numbers. Then the program should then use that GCD to reduce the fraction and output it in a readable format. The user should be prompted by the program for the numerator and denominator and should label the output. Here's an example of how the program should function, with the user input shown in italics: Numerator? 144 Denominator? 240 Reduced fraction is: 3/5 Note that this is the answer because the GCD of 144 and 240 is 48.Explanation / Answer
pr1: .asciiz "Numerator? "
pr2: .asciiz "Denominator? "
rs1: .asciiz "The GCD of "
rs2: .asciiz " and "
rs3: .asciiz " is "
rs4: .asciiz ". "
.text
.globl main
main:
li $v0, 4 # System call to print string
la $a0, pr2 # load address of pr1
syscall # print pr1
li $v0, 5 # System call to read numerator
syscall # reads integer
move $s0, $v0 # moves integer in $v0 to $s0
li $v0, 4
la $a0, pr2
syscall # print pr1
li $v0, 5 # System call to read denominator
syscall
move $s1, $v0 # moves integer in $v0 to $s1
gcd:
beq $s0, $s1, exit # branch to exit if $s0 and $s1 are equal
slt $t0, $s1, $s0 # $t0 is set to one if $s1 is less than $s0
bne $t0, $zero, L1 # branches to L1 if $t0 is not equal to zero
sub $s1, $s1, $s0 # subtracts $s0 from $s1 and stores it in $s1
j gcd
L1:
sub $s0, $s0, $s1 # subtracts $s1 from $s0 and stores it in $s0
j gcd
exit:
add $v0, $s0, $zero # adding zero to $s0 and storing it in $v0
li $v0, 4 # printing result string.
la $a0, rs1 # load address of rs1
syscall # print rs1
li $v0, 1 # Print numerator.
move $a0, $s0
syscall
li $v0, 4
la $a0, rs2
syscall # prints rs2
li $v0, 1 # prints denominator
move $a0, $s1
syscall
li $v0, 4
la $a0, rs3
syscall # prints rs3
li $v0, 1 # Prints gcd
move $s0, $s0
syscall
li $v0, 4
la $a0, rs4
syscall # prints rs4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.