Question

Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result
  • "Euclid's Greatest Common Divisor Algorithm"
    GCD(M,N) = M                      (if N is 0)
    GCD(M,N) = GCD(N, M % N)   (if N > 0)
  • you may assume that inputs are non-negative
  • name your assembly language file
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Given below is the code for the question. Please do rate the answer if it helped. Thank you.

.data
   inMsg: .asciiz "Enter a number: "
   outMsg: .asciiz "GCD is "
.text

   #print string
   la $a0, inMsg
   li $v0, 4
   syscall
  
   #read int
   li $v0, 5
   syscall
   move $t0, $v0
  
  
   #print string
   la $a0, inMsg
   li $v0, 4
   syscall
  
   #read int
   li $v0, 5
   syscall
   move $t1, $v0
  
   #set up args and call function gcd
   move $a0, $t0
   move $a1, $t1
   jal gcd
   move $t0, $v0 #store result from v0 into t0
  
   #print string
   la $a0, outMsg
   li $v0, 4
   syscall
  
   #print int
   move $a0, $t0
   li $v0, 1
   syscall
  
   #exit
   li $v0, 10
   syscall
  

gcd:
   #save register on stack
   sub $sp, $sp, 12
   sw $ra, 0($sp)
   sw $a0, 4($sp)
   sw $a1, 8($sp)
  
   beqz $a1, base_case
   div $a0, $a1 #will leave remainder in HI
   move $a0, $a1 #set M =N for next recursive call
   mfhi $a1 #set N = remainder of division
   jal gcd
   b end_gcd
  
base_case:
   move $v0, $a0

end_gcd:
  
   #restore registers
   lw $ra, 0($sp)
   lw $a0, 4($sp)
   lw $a1, 8($sp)
   add $sp, $sp, 12
   jr $ra


output
---
Enter a number: 12
Enter a number: 15
GCD is 3
-- program is finished running --

Enter a number: 3
Enter a number: 5
GCD is 1
-- program is finished running --

Add a comment
Know the answer?
Add Answer to:
Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest...

    (Recursive Greatest Common Divisor) The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x, y) is x; otherwise gcd(x, y) is gcd(y, x % y), where % is the remainder operator. c programming need the whole...

  • Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and...

    Consider the problem of finding the Greatest Common Divisor (GCD) of two positive integers a and b. It can be mathematically proved that if b<=a GCD(a, b) = b, if (a mod b) = 0; GCD(a, b) = GCD(b, a mod b), if (a mod b) != 0. Write a recursive function called GCD with signature “public static int GCD(int a, int b)” that returns the greatest common divisor of two positive integers a and b with b <= a....

  • Write a C++ program that reads in two integers and then computes the greatest common divisor...

    Write a C++ program that reads in two integers and then computes the greatest common divisor GCD using recursion. DO NOT USE A BUILT-IN FUNCTION SUCH AS "gcd" to do this.

  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • Write a complete C++ program to ask the user to inter two integers and the program...

    Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following: Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following: Enter two integers 18 27 The GCD is = 9

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change...

    Euclid’s Elements (300 BC) shows that the greatest common divisor of two integers does not change if the smaller is subtracted from the larger. Write a program (in MIPS assembly) that implements this algorithm to find the GCD of two integers. Follow this algorithm: 1. Call the two integers a and b. 2. If a and b are equal: stop: a is the GCD. 3. Subtract the smaller from the larger. Replace the larger with the result 4. Repeat steps...

  • PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...

    PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2...

  • Write a complete C++ program to ask the user to inter two integers and the program...

    Write a complete C++ program to ask the user to inter two integers and the program finds and display the greatest common divisor (GCD) of the two integers. Typical output screen should be as following: Enter two integers 18 27 The GCD is = 9

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT