Name: Date:
HW2 solution
-
(20 points) Convert the following C code to RISC-V assembly instructions. Use the minimum number of instructions necessary. Assume that variables f, g and h are 32- bit integers stored in registers
x5
,x6
andx7
respectively and that the base address of arrays A and B are in registersx8
andx9
respectively. A and B are arrays of 4-byte integers (this is important).Be careful not to modify the variables unintentionally. If you need to store temporary values, use one of the other registers.
a.
f = g + h;
add x5, x6, x7
b.
f = g - (h - 5);
addi x5, x7, -5 sub x5, x6, x5
c. Remember, the elements of A are 4-bytes big!
f = A[3] - g;
lw x5, 12(x8) sub x5, x5, x6
d.
A[0] = f + B[1];
lw x10, 4(x9) add x10, x10, x5 sw x10, 0(x8)