S2.3.2 Where Is the Return Address

EasyImplement12 minIcarus Verilog · serverReview the topic

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

SignalWidthRole
question8input of each check
Full specification
The program
A jump target is an address. The instruction slli shifts left: by 1 bit, it doubles.
Address, in decimalInstruction
0addi a0, zero, 4
4jal ra, 28
8addi s0, a0, 0
12addi a0, zero, 6
16jal ra, 28
20add a0, a0, s0
24ebreak
28slli t0, a0, 1
32addi a0, t0, 1
36jalr zero, 0(ra)

Enter every value in decimal. For a yes or no question, enter 1 for yes and 0 for no.

EntryYour decision
RA_AFTER_FIRST_JALWhat does ra hold after the jal at address 4?
PC_AFTER_FIRST_JALWhat is the address of the instruction that runs after the jal at address 4?
A0_AFTER_FIRST_RETURNWhat does a0 hold after the first return?
PC_AFTER_FIRST_RETURNWhat is the address of the instruction that runs after the first return?
RA_AFTER_SECOND_JALWhat does ra hold after the jal at address 16?
PC_AFTER_SECOND_RETURNWhat is the address of the instruction that runs after the second return?
A0_AFTER_SECOND_RETURNWhat does a0 hold after the second return?
S0_AT_STOPWhat does s0 hold when the program stops?
A0_AT_STOPWhat does a0 hold when the program stops?
T0_AT_STOPWhat does t0 hold when the program stops?
MAY_CHANGE_T0Does the calling convention allow the function to change t0?
MAY_CHANGE_S0Would the convention allow the function to change s0 and not restore it?
INSTRUCTIONS_RUNHow many instructions run before the ebreak?
A0_FOR_KThe 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

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

RA AFTER FIRST JAL

Input
What does ra hold after the jal at address 4?
Expected output
matches = 1

A call writes the address of the instruction after itself: 4 plus 4.

Run samples executes all 3 public checks. Check all tests runs 14 checks on the server.

Icarus Verilog · server