Solutions
A
For each RISC-V instruction sequence below, provide the hex values of the specified registers after each sequence has been executed. Assume that all registers are initialized to 0 prior to each instruction sequence. Each instruction sequence begins with the line (. = 0x0
) which indicates that the first instruction of each sequence is at address 0. Assume that each sequence execution ends when it reaches the UNIMPL
instruction.
Solutions:
0x4
0x8
0x10
Illustrations:
pc = 0x0: jal x5 L1
sets x5 to the address of the instruction after jal L1, x5
, which is 4, and then jumps to L1pc = 0x8: j L2
jumps to L2pc = 0x10: jr x5
jumps to the instruction at address 4, which is jal x6, end
pc = 0x4: jal x6, end
sets x6 to the address of the instruction after that jal x6, end
, which is 8pc = 0x14:
the program stops0x10
because it is 4 words after the first word, which is at 0x0
, so it is .B
Pseudo-instructions
– shorthand syntax for common assembly idioms
– mv rd, rs
= addi rd, rs, 0
– li rd, 13
= addi rd, x0, 13
RISC-V sequence B refers to certain locations in memory. Provide the hex values of the specified registers after each sequence has been executed. Assume that the first 5 memory locations starting from address 0x600 have been initialized with the following 5 words. Assume that each sequence execution ends when it reaches the UNIMPL
instruction.
Sequence B
Solutions:
0x87654321
0x608
0x12345678
Illustrations:
0x604
.0x87654321
is less than 0 because its most significant bit is set in two’s complement, but 0x12345678
is not, so the program stops looping when x8 = 0x608
and x9 = 0x12345678
.0x600
until the last line, but loading 0x600
at the end gives 0x87654321
because the first iteration of the loop overwrote it.C
Below is the C code for a recursive implementation of binary search, which finds the index at which an element should be inserted into a sorted array to ensure that the array is still sorted after the insertion. The roughly translated RISC-V assembly follows the C code.
C01 =
a2, t1, else_
C02 =
2
call binary_search
. The program is interrupted during a recursive call to binary_search
, just prior to the execution of addi sp, sp, 8
at label L1. The diagram on the right shows the contents of a region of memory. All addresses and data values are shown in hex. The current value in the SP register is 0xEB0
and points to the location shown in the diagram CONTENT. What are the hex values in the following registers right when the execution of binary_search is interrupted?C03 =
0x6
Current value of ra?
C04 =
0xC4
Memory address | Data content |
---|---|
0xEA4 | 0x0 |
0xEA8 | 0x5 |
0xEAC | 0xC4 |
SP→ 0xEB0 | 0x6 |
0xEB4 | 0xC4 |
0xEB8 | 0x6 |
0xEBC | 0xC4 |
0xEC0 | 0xA |
0xEC4 | 0xC4 |
0xEC8 | 0x3E |
0xECC | 0xCA4 |
0xED0 | 0xCED |
diagram: CONTENT,
sp
points to0xEB0
.
ret
instruction?C05 =
0xD8
0xC4
is address of mv s0, a0
. Add 4 for each instruction until you reach ret which is at 0xD8
.
D
The following functions are missing code to save/restore registers to/from the stack. Identify the registers that need to be saved to the stack at any point in the function. You do not need to provide the missing code. If the RISC-V calling convention does not require any registers to be saved to / restored from the stack, write none
.
1
registers needing saving (or "none")?
D01 =
s0, ra
2
registers needing saving (or "none")?
D02 =
s0, s1
E
Below is the C code and translated RISC-V assembly.
X
is unknown.)What is the minimum value of X
, the number of bytes needed on the stack?
E01 = 8
F
You are given the following C code along with an incomplete translation of this code into RISC-V assembly. Assume that execution ends when it reaches the UNIMPL
instruction.
F01 =
slli t0, t0, 2
Provide the hex values left in the registers below following the execution of the corrected code.
F02 =
0x808
F03 =
0x4
F04 =
0x2
G
Consider the C procedure below and its translation to RISC-V assembly code, shown in the later.
G01
is unknown.)G01
term in the C code and the assembly be replaced with to make the if statement correctly check if the variable ‘c’ is a multiple of 4?
G01 =
3
AND with 0b11 (or 0x3 or just 3) to check if last 2 bits are 0, indicating a multiple of 4
G02 =
2