Question

I am having trouble with getting this code to work 100% correctly. If the user enters a larger number as the first number, then the program gives the correct Greatest Common Denominator.

However, If a Smaller number is entered at first, then the smaller number is returned as the GCD.

I have been trying to figure this out for hours to no avail. Please someone help!!

Was it helpful?

Solution

Well, you could easily swap the registers $a0 and $a1 if the second input item is greater than the first one. E.g.:

move $a1, $v0       # store input in $a1   -> this is your code    
# Here goes the test & swap code, just before your original base label

bge $a0, $a1, noswap
move $a1, $a0   # Swap $a0 and $a1
move $a0, $v0   # if the second item is greater than the first one
noswap:

# Here continues your code
base:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top