In: Computer Science
1. For the following C statement, write the corresponding RISC-V assembly code. Assume that the C variables a, b, and c, have already been placed in registers x10, x11, and x12 respectively. Use a minimal number of RISC-V assembly instructions. a = b + (c − 2);
2. Write a single C statement that corresponds to the two RISC-V assembly instructions below. add e, f, g add e, h, e
3. Assume that registers x5 and x6 hold the values 0xA000000000000000 and 0x2000000000000000, respectively.
(1) What is the value of x30 for the following assembly code? add x30, x5, x6
(2) For the contents of registers x5 and x6 as specified above, what is the value of x30 for the following assembly code? sub x30, x5, x6
(3) For the contents of registers x5 and x6 as specified above, what is the value of x30 for the following assembly code? add x30, x5, x6 add x30, x30, x6
1.
We can write the required C expression in two steps as :
a = b + c;
a = a + (-2);
Converting the individual broken-up C-statements, we get the followign sequence of assembly-instructions :
add x10, x11, x12
addi x10, x10, -2
2.
The given two assembly instructions can be individually converted as :
add e, f, g
=> e = f + g;
add e, h, e
=> e = h + e;
Expanding the second result, we get the single C-statement as :
=> e = h + e
=> e = h + (f + g);
=> e = f + g + h;
3.
(1)
add x30, x5, x6
=> x30 = x5 + x6
=> x30 = 0xA000000000000000 + 0x2000000000000000
=> x30 = 0xC000000000000000
(2)
sub x30, x5, x6
=> x30 = x5 - x6
=> x30 = 0xA000000000000000 - 0x2000000000000000
=> x30 = 0x8000000000000000
(3)
add x30, x5, x6
=> x30 = x5 + x6
=> x30 = 0xA000000000000000 + 0x2000000000000000
=> x30 = 0xC000000000000000
add x30, x30, x6
=> x30 = x30 + x6
=> x30 = 0xC000000000000000 + 0x2000000000000000
=> x30 = 0xE000000000000000