Name: Date:
HW10
-
(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
.
- PUT YOUR NAME IN THIS FILE!
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:
- Follow the RISC-V Procedure Calling conventions.
- Even though the C code is inefficient, DO NOT CHANGE THE LOGIC OF THE CODE. Your job is to translate this code directly.
- To test your code for
gcd
, set the value ofTEST_GCD
to 1 in yourrelPrime.asm
file. That will tellruntests
to test yourgcd()
procedure.
d. In the same file, add a RISC-V procedure equivalent to the C function relPrime()
above.
- Test your
relPrime
implementation the same way: Set the value ofTEST_RELPRIME
to 1 in your file to enable the tests.
e. Upload your completed relPrime.asm
code to gradescope.