A function is called from two places. Follow ra, the PC and a0 through both calls.
Hardware context: Two hand-compiled functions on a small model of an RV32I processor: a caller that passes three arguments and a callee that returns their sum. Reading compiled code in a trace and writing assembly that can be called from C
The program below starts at address 0, with 0 in every register. The function at address 28 doubles its argument and adds 1. It is called twice. Addresses in the table are in decimal here, so that you can enter them as they are.
This worksheet uses named constants. The first three entries show how to record your decisions.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
question | 8 | input of each check |
Full specification
| Address, in decimal | Instruction |
|---|---|
| 0 | addi a0, zero, 4 |
| 4 | jal ra, 28 |
| 8 | addi s0, a0, 0 |
| 12 | addi a0, zero, 6 |
| 16 | jal ra, 28 |
| 20 | add a0, a0, s0 |
| 24 | ebreak |
| 28 | slli t0, a0, 1 |
| 32 | addi a0, t0, 1 |
| 36 | jalr zero, 0(ra) |
Enter every value in decimal. For a yes or no question, enter 1 for yes and 0 for no.
| Entry | Your decision |
|---|---|
| RA_AFTER_FIRST_JAL | What does ra hold after the jal at address 4? |
| PC_AFTER_FIRST_JAL | What is the address of the instruction that runs after the jal at address 4? |
| A0_AFTER_FIRST_RETURN | What does a0 hold after the first return? |
| PC_AFTER_FIRST_RETURN | What is the address of the instruction that runs after the first return? |
| RA_AFTER_SECOND_JAL | What does ra hold after the jal at address 16? |
| PC_AFTER_SECOND_RETURN | What is the address of the instruction that runs after the second return? |
| A0_AFTER_SECOND_RETURN | What does a0 hold after the second return? |
| S0_AT_STOP | What does s0 hold when the program stops? |
| A0_AT_STOP | What does a0 hold when the program stops? |
| T0_AT_STOP | What does t0 hold when the program stops? |
| MAY_CHANGE_T0 | Does the calling convention allow the function to change t0? |
| MAY_CHANGE_S0 | Would the convention allow the function to change s0 and not restore it? |
| INSTRUCTIONS_RUN | How many instructions run before the ebreak? |
| A0_FOR_K | The first instruction becomes addi a0, zero, K. Give a0 at the ebreak, as an expression in K. |
Examples
Example 1
RA AFTER FIRST JAL- Input
What does ra hold after the jal at address 4?
- Output
matches = 1
A call writes the address of the instruction after itself: 4 plus 4.
Example 2
PC AFTER FIRST JAL- Input
What is the address of the instruction that runs after the jal at address 4?
- Output
matches = 1
The jal jumps to its target, the function at 28.
Example 3
A0 AFTER FIRST RETURN- Input
What does a0 hold after the first return?
- Output
matches = 1
The function doubles 4 and adds 1.
Constraints and Contract
- Keep the parameter and named constants. Use the numeric codes and units on this page.
- For A0_FOR_K, enter a constant expression using K. Only this entry changes with the parameter. The supported integer range is 4 through 120, inclusive.
Hints
Hint 1
For each jal, write down two numbers: the address after it, and its target.
Hint 2
For each jalr, read ra. That is where the PC goes.
Hint 3
Mark each register as temporary, argument or saved before you follow it across a call.
What the grader checks: Each check compares the full eight-bit answer. Parameter trials change one condition and keep the other decisions fixed.
14 checks on Icarus Verilog · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your source only; samples require the simulation service. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 2 problems on this path