C814 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 sum pushes 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?

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:

RegistersRoleWho saves them
a0 to a7arguments; a0 (and a1) carry the result backthe caller, if it needs them after the call
t0 to t6temporariesthe caller, if it needs them after the call
rareturn addressa function that makes calls of its own
s0 to s11values kept across callsthe callee, before it uses them
spstack pointerthe 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
  1. Make the frame. f moves sp down 16 bytes: 0xFFF0−16=0xFFE0
  2. Save ra. It goes 12 bytes up: 0xFFE0+12=0xFFEC
  3. Save s0. It goes 8 bytes up: 0xFFE0+8=0xFFE8

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
  1. Add up the frames. f's frame and g's are 16 bytes each, 32 bytes together.
  2. 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
  1. Save ra. Its own call overwrites it.
  2. Save s0 and s1. The calling convention promises its caller those are preserved.
  3. 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)
  1. Restore before returning. The mirror image comes before ret.
  2. 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.

  1. Athe first argument, and the return value
  2. Bwhere to go back to when the function returns
  3. Ca temporary that any call may overwrite
  4. Dthe address of the top of the stack
  5. 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°.

Explain why

Why must a function that calls another function save ra, while a function that calls nothing can leave it alone?

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.