Skip to content
BIASED

Procedures, the stack and calling conventions

`jal`/`jalr` and `ret`, caller- and callee-saved ABI registers, 16-byte-aligned stack frames, recursive factorial, and the cost of naive Fibonacci recursion.

`jal` and `jalr`

Loops and memory were enough for one routine that called nothing else. A procedure must find arguments, return to any caller, deliver a result, and leave the caller’s values intact.

Baseline. Sharing code with a fixed j back works for one caller only. Store the return address in a register — then a nested call overwrites it. Hence per-call storage and rules about who saves what.

jal rd, label: x[rd]←pc+4x[rd]\leftarrow pc+4, pc←pc\leftarrow label (±1 MiB, J format). jalr rd, rs1, imm: jump to x[rs1]+sext⁡(imm)x[rs1]+\operatorname{sext}(imm) (bit 0 cleared), link into rd (I format). Pseudos: jal label / call (auipc+jalr for far targets), ret = jalr zero, ra, 0, jr rs = jalr zero, rs, 0.

Example. jal at 0x00400004 → ra=0x00400008, pc=fact at 0x00400050. Recursive jal at 0x0040006c → every recursive return lands at 0x00400070.

The calling convention

Key idea. One ABI contract (RISC-V psABI), not hardware: args in a0–a7, result in a0/a1. Caller saves any live t*, a*, ra. Callee must restore s0–s11 and sp. Extra args beyond the eighth sit on the stack at 0(sp), 4(sp), … at the call. Split registers so tiny callees are cheap and long-lived values pay once in s*.

If callers saved everything, a call to a three-instruction leaf would still cost many stores. If callees saved everything, a caller with nothing to keep would pay anyway. Values that must live across calls go in s*; short-lived ones in t*/a*.

The stack and frames

Stack grows to lower addresses; sp points at the lowest used byte (RARS start 0x7fffeffc). Prologue: addi sp, sp, -N (NN multiple of 16 — the ABI requires 16-byte alignment at every call), sw ra / sw s*. Epilogue restores in reverse and addi sp, sp, N then ret. The frame also holds locals and outgoing overflow args. Bytes below sp are fair game for anyone — never leave live data there.

proc:  addi sp, sp, -16
       sw   ra, 12(sp)
       sw   s0, 8(sp)
       ...
       lw   s0, 8(sp)
       lw   ra, 12(sp)
       addi sp, sp, 16
       ret

Recursion: factorial

Each active call has its own frame — recursion needs no special hardware.

fact keeps nn in s0 across jal fact, saves caller’s s0 and ra. For fact(3): deepest sp=0x7fffefcc; returns 1, 2, 6. Depth 16n16n bytes with a 16-byte frame. 13!13! already overflows 32-bit mul (6,227,020,800>231−16{,}227{,}020{,}800 > 2^{31}-1).

Naive fib(n): 2Fn+1−12F_{n+1}-1 calls → 339,303 instructions at n=20n=20 versus 105 for a two-register loop. Recursion is clear; cost depends on the call tree. Tail calls can reuse a frame when the language and ABI allow — C compilers sometimes turn them into jumps.

Leaf and non-leaf

A leaf calls nobody: ra survives; with only t/a it needs no frame. A non-leaf must save ra and keep post-call values in s* (saved) or in the frame. Classic bug: allocate a frame, forget to store ra, call something, then ret — you return into the callee’s leftovers or crash. Another: write below sp thinking the red zone is yours (x86-64 System V has a 128-byte red zone; RISC-V does not).

Assembly half of the course is complete enough to run structured programs. Next: Caches — why memory is not uniform cost, and how 64-byte lines and locality change CPI.

Frame sizes are multiples of 16 even when you only need 8 bytes of saves: the unused slots are padding for ABI alignment. On entry to main in a hosted environment the loader has already built an initial frame with argc/argv; in RARS you often write main as an ordinary label and exit via ecall. Recursive depth limits are practical, not theoretical: a 1 MiB stack at 16 bytes per fact frame admits about 65,000 nested calls before smashing into the heap or mapped guards — far above fact(12), far below a naïve fib tree that would exhaust time first.

When reading compiler output at -O2, leaf functions often keep everything in a*/t* and have no prologue; non-leaves show paired addi sp / sd/ld (RV64) around the body. Matching those stores to the ABI register classes is the fastest way to see whether a bug is “callee clobbered my value” (you left it in t0 across a call) or “I clobbered the caller’s s0” (you used s0 without saving). The next topic’s caches make frame traffic concrete: every sw/lw of ra is a memory operation that hits L1 when the stack is hot, and misses when you have just cold-started a deep recursion.

Varargs and arguments past a7 push the convention further: the caller allocates stack slots and the callee reads them at known offsets from sp (or from fp if a frame pointer is maintained). Debugging “wrong 9th argument” almost always means a mismatched type size or forgotten slot between the two sides of a handwritten call.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.