Name: Date:
HW10 solution
-
(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:
- You only the core RISC-V instructions on our class RISC-V green sheet. This is so you can put this code through your own assembler from Practical 1 and 2 later.
- 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_GCDto 1 in yourrelPrime.asmfile. That will tellrunteststo test yourgcd()procedure.
d. In the same file, add a RISC-V procedure equivalent to the C function relPrime() above.
- Test your
relPrimeimplementation the same way: Set the value ofTEST_RELPRIMEto 1 in your file to enable the tests.
e. Upload your completed relPrime.asm code to gradescope as a pdf, just like the other homeworks,
you are not uploading an asm file directly.
...