C8 · Lesson 14 · The stack and function calls
Calling a function and finding the way back
A function can be called from many places, so when it finishes it has to know where to go back to. RISC-V's answer is one instruction that jumps and remembers: jal puts the address of the next instruction in the register ra and then jumps. The function returns with ret, a jump to whatever ra holds. That works for one level. When a function calls another, the second call overwrites ra, so the first function has to save ra somewhere first, and the place is the stack: a region of memory that grows downward as calls nest and shrinks as they return. This program adds 3 + 2 + 1 with a function that calls itself.
Play
- Step through and watch sp and ra. Each call to
sumpushes a 16-byte frame holding ra and n. - The memory window shows the stack. Find each frame's saved n and saved return address.
- After the calls return, sp is back to 0x10000. Is the old data in memory gone?
Predict
While sum(0) runs, at the deepest point of the calls, how many 16-byte frames are on the stack?
Three: one each for sum(3), sum(2) and sum(1), each holding its n and its return address. sum(0) returns straight away without making a frame, because it calls nothing. At that point sp is 0x10000 − 3 × 16 = 0xFFD0. When the calls return, sp climbs back to 0x10000, but the old values stay in memory: popping a frame only moves sp.
Name it
Calls, returns and the calling convention
Call and return. jal ra, f (written call f) saves PC + 4, the address of the instruction after the call, in ra and jumps to f. ret is jalr x0, 0(ra): jump to the address in ra. (call is written as two instructions here, auipc and jalr, so that it can reach a function anywhere.)
The stack. sp holds the address of the top of the stack, which grows toward lower addresses. A function that needs to save registers makes a frame on entry and removes it before returning:
f: addi sp, sp, -16 # make room
sw ra, 12(sp) # save what must survive
...
lw ra, 12(sp) # restore it
addi sp, sp, 16 # give the room back
ret
The standard RISC-V convention keeps sp a multiple of 16, so frames come in steps of 16 bytes.
The calling convention is the agreement that lets separately written functions work together:
| Registers | Role | Who saves them |
|---|---|---|
| a0 to a7 | arguments; a0 (and a1) carry the result back | the caller, if it needs them after the call |
| t0 to t6 | temporaries | the caller, if it needs them after the call |
| ra | return address | a function that makes calls of its own |
| s0 to s11 | values kept across calls | the callee, before it uses them |
| sp | stack pointer | the callee leaves it as it found it |
A leaf function, one that calls nothing, often needs no frame at all.
Recursion works because every call gets its own frame, so each level keeps its own n and its own way back.
Worked example
sp is 0xFFF0 when f is called. f makes a 16-byte frame and saves ra at 12(sp) and s0 at 8(sp). At what addresses are they saved?
Show the solution
- Make the frame. f moves sp down 16 bytes:
- Save ra. It goes 12 bytes up:
- Save s0. It goes 8 bytes up:
Answer ra is saved at 0xFFEC and s0 at 0xFFE8.
Worked example
f calls g, and g calls h, which calls nothing. f and g each make a 16-byte frame and h makes none. How far below f's starting sp is sp while h runs?
Show the solution
- Add up the frames. f's frame and g's are 16 bytes each, 32 bytes together.
- h's frame. h is a leaf, so it needs no frame of its own.
Answer 32 bytes below f's starting sp.
Worked example
A function uses s0 and s1 and calls another function. Which registers must it save, and how big a stack frame does it need?
Show the solution
- Save ra. Its own call overwrites it.
- Save s0 and s1. The calling convention promises its caller those are preserved.
- Size the frame. That's 12 bytes, but the stack pointer must stay a multiple of 16, so the frame is 16 bytes.
addi sp, sp, -16
sw ra, 12(sp)
sw s0, 8(sp)
sw s1, 4(sp)
- Restore before returning. The mirror image comes before
ret. - Leave the temporaries. t0 to t6 need no saving: the caller has already assumed they'll be lost.
Answer it saves ra, s0 and s1, 12 bytes, in a 16-byte frame.
Check
sp holds 0xADF0. A chain of 1 nested calls each makes a 16-byte frame. What does sp hold at the deepest point?
Check
A function is about to call another function. Which register must it have saved on the stack first, if it wants to return to its own caller afterwards?
Check
Finish triple, a function that returns 3 × its argument. The main program passes n in a0 and calls it.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
Finish nine, which returns 9 × its argument by calling triple twice. The stack must be back where it started when the program stops.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Check
A recursive function makes a 32-byte frame on each call, and the stack has 1 KiB (1 KiB = 1024 bytes). How many calls deep can it go before the stack runs out?
Check
Match each register to its role in the calling convention.
- Athe first argument, and the return value
- Bwhere to go back to when the function returns
- Ca temporary that any call may overwrite
- Dthe address of the top of the stack
- Ekept across calls: a function that uses it saves it first
Check
A function uses 2 saved registers and makes calls if c = 1 (here c = 0).
(a)
How many registers must it save on the stack?
(b)
How big is its frame, keeping sp a multiple of 16?
Check
Calls nest d deep, each with an f-byte frame, from a stack of s bytes.
Step 1
How much stack is in use at the deepest point?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How deep can calls nest before the stack overflows?
Explain why
Why must a function that calls another function save ra, while a function that calls nothing can leave it alone?
ra holds the address the function must return to. A call puts a new value in ra, the address just after that call, so a function that makes a call loses its own return address unless it copied it somewhere first, usually its stack frame, and restores it before ret. A leaf function never executes a call, so nothing changes ra between its entry and its ret, and it can return through ra directly.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.