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.
- A01 = ?
- A02 = ?
- A03 = ?
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
- B01 = ?
- B02 = ?
- B03 = ?
Function Calls
The following pseudo instructions are available to call subroutines far from the current position:
call <symbol>
: call away subroutine
call <rd>, <symbol>
: call away subroutine
call <symbol>
, but <rd>
is used to save the return address instead.tail <symbol>
: tail call away subroutine
t1
is implicitly used as a scratch register.jump <symbol>, <rt>
: jump to away routine
tail <symbol>
, but <rt>
is used as the scratch register instead.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
is unknown)What should be in the blank on the line labeled if to make the assembly implementation match the C code?
C01 = ?
How many words will be written to the stack before the program makes each recursive call to the function binary_search? That is, number of words pushed onto stack before each recursive call?
C02 = ?
The program’s initial call to function binary_search occurs outside of the function definition via the instruction 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?
Current value of s0
?
C03 = ?
Current value of ra
?
C04 = ?
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 to address0xEB0
ret
instruction?
C05 = ?
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 = ?
2
registers needing saving (or "none")?
D02 = ?
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 = ?
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
is unknown)F01 = ?
Provide the hex values left in the registers below following the execution of the corrected code.
F02 = ?
F03 = ?
F04 = ?
G
Consider the C code below and its translation to RISC-V assembly code, shown in the later.
G01
is unknown.)What value should the 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 = ?
How many words will be written to the stack before the program makes each recursive call to the function f? That is, number of words pushed onto stack before each recursive call?
G02 = ?