Name: Date:

HW10

  1. (20 points) Consider the following C code:

    // Find m that is relatively prime to n.
    int relPrime(int n)
    {
     int m;
    
     m = 2;
    
     while (gcd(n, m) != 1) {  // n is the input from the outside world
       m = m + 1;
     }
    
     return m;
    }

     

    // The following method determines the Greatest Common Divisor of a and b 
    // using Euclid's algorithm.
    int gcd(int a, int b)
    {
    if (a == 0) {
      return b;
    }
    
    while (b != 0) {
      if (a > b) {
        a = a - b;
      } else {
        b = b - a;
      }
    }
    
    return a;
    }

a. Download this template file, relPrime.asm.

b. Also download the tests for this homework here.

c. In relPrime.asm, create a RISC-V procedure that is equivalent to the gcd() function above. Important notes:

d. In the same file, add a RISC-V procedure equivalent to the C function relPrime() above.

e. Upload your completed relPrime.asm code to gradescope.