Question
Problem 2:(20Marks)Write a recursiveMIPS program to compute Greatest Common Divisor (GCD) of two positive integers Xand Y.Take X and Y input from the userusing I/O
Problem 2:(20Marks)Write a recursiveMIPS program to compute Greatest Common Divisor (GCD) of two positive integers Xand Y.Take X and Y input from the userusing I/O operation as given in Problem1.asm.An algorithm to compute GCD of X and Y is given below:
My code is supposed to return the GCD of two numbers but for some reason, it's only returning the y-value and I don't know how to fix it.
Here's my code for reference:
# Create some null terminated strings to use .data strPromptX: .asciiz "Enter x-value:" strPromptY: .asciiz "Enter y-value:" strGCD: .asciiz "GCD is: " strCR: .asciiz " " .text .globl main main: # STEP 1: get the first operand # Print a prompt asking user for input x li $v0, 4 # syscall number 4 will print string whose address is in $a0 la $a0, strPromptX # "load address" of the string syscall # actually print the string
# Now read in the first operand li $v0, 5 # syscall number 5 will read an int syscall # actually read the int add $s0, $v0, $zero # save result in $s0 for later # STEP 2: get the second operand # Print a prompt asking user for input y li $v0, 4 # syscall number 4 will print string whose address is in $a0 la $a0, strPromptY # "load address" of the string syscall # actually print the string # Now read in the second operand li $v0, 5 # syscall number 5 will read an int syscall # actually read the int add $s1, $v0, $zero # save result in $s0 for later
# STEP 3: Calling the GCD move $a0, $s0 # move the input x to register $a0 move $a1, $s1 # move the input y to register $a1 jal GCD # jump to the function sum to calculate the sum move $s3, $v0 # move to register $s1
# STEP 4: print the GCD # First print the string prelude li $v0, 4 # syscall number 4 -- print string la $a0, strGCD syscall # actually print the string # Then print the actual GCD li $v0, 1 # syscall number 1 -- print int move $a0, $s1 # put the result in $a0 for print syscall # actually print the int
# STEP 5: Finally print a carriage return li $v0, 4 # syscall for print string la $a0, strCR # address of string with a carriage return syscall # actually print the string
# STEP 6: exit li $v0, 10 # Syscall number 10 is to terminate the program syscall # exit now GCD: # check base condition beq $a0, 0, ifStatementOne beq $a1, 0, ifStatementOne # checking first if condition ifStatementOne: li $v0, 0 jr $ra # checking second if condition beq $a0, $a1, ifStatementTwo ifStatementTwo: move $v0, $a0 jr $ra # recursive statement blt $a0, $a1, if if: sub $a1, $a1, $a0 j GCD else: sub $a0, $a0, $a1 j GCD
Write a recursive MIPS program to compute Greatest Common Divisor (GCD) of two positive integers X and Y. Take X and Y input from the user using I/O operation as given in Probleml.asm. An algorithm to compute GCD of X and Y is given below: If X or Y = 0 then GCD (X,Y) = 0 If X = Y then GCD (X, Y) = X else if XStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started