Be Systematic
Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
It pays to be systematic when translating C code to assembly, so when the programs get complicated, you know how to structure them and make them less tangled and messy. In this exercise, you will be writing something like two nested for loops. You definitely want to be systematic.
Before working on this exercise, make sure to carefully review lecture 5 slides on "Compiling High-Level Languages into RISC-V Programs," especially on "Compiling Loops."
Note that the symbol ^ here refers to the XOR operator.
Write this function in assembly. At the start of your function, a0 will be set to the value of seed (the function argument). At the end of the function, you should make sure to put the answer in a0 before returning with ret. You may use a0-a7 and t0-t6 registers.
int nested_loops(int seed) {
int ans = seed;
for (int a = 5; a < 27; a++) {
for (int b = 42; b >= 10; b--) {
if ((a+b) & 1) {
ans += a;
ans ^= b;
} else {
ans += b;
ans ^= a;
}
ans ^= b-a;
}
}
return ans;
}