C8 · Lesson 23 · Assembly functions lab
Writing real functions in RISC-V assembly
The assembly lab (lesson 19) had you write loops and arithmetic. Real programs are made of functions: searching, counting, copying, and functions that call other functions, including themselves. Each of the seven exercises here is a small function you'd find in any C library or firmware, written by hand. They're graded by running them, and the last two also check that you followed the calling convention, because a function that works alone but wrecks its caller's registers or stack isn't finished.
- The main program adds up the squares of 1 to 10 by calling
squareten times. squareis a normal function: it may use any temporary register, and it does, overwriting t0 and t1. That's why the running sum lives in s0 and the counter in s1, which callees must preserve.- Step through one call:
jalsaves the return address in ra,squareruns, andretjumps back. a0 ends at 385.
Predict
If main kept the running sum in t0 instead of s0, what would a0 hold at the end?
- Each call to
squarecopies its argument into t0, so whatever sum was there is lost. After the last call t0 holds 10, and adding the returned 100 gives 110. The rule is simple: anything that must survive a call goes in a saved register or on the stack.
Name it
The calling convention, in brief
| Registers | Role | Who saves them |
|---|---|---|
| a0–a7 | arguments; a0 (and a1) return the result | the caller, if it needs them after the call |
| t0–t6 | temporaries | the caller, if it needs them |
| s0–s11 | saved registers | the callee, if it uses them |
| ra | return address, set by jal | the callee, if it calls anything |
| sp | stack pointer, a multiple of 16 | must be back where it started at ret |
A leaf function, one that calls nothing, can often work entirely in temporaries and needs no stack frame. A function that calls others saves ra, plus any saved registers it uses, in a frame it makes by moving sp down, and restores them before ret.
Recursion
A recursive function is just a function that calls itself, so it follows the same rules: every call gets its own frame holding its own ra and whatever it needs after the inner call returns. The stack grows by one frame per level.
Testing a function
The graders below put arguments in registers or memory, run your code to ebreak, and check results. The last two also check that sp ends where it started, which catches unbalanced frames.
Worked example
Why does the predict question's version lose the sum, even though square never touches it on purpose?
Show the solution
- What t0 is. t0 is a temporary: the convention says a callee may change it without saving it, and
squareuses it as scratch. - What the caller must assume. The caller can't know which temporaries a callee uses (it might be in a library compiled separately), so it must assume all of them are destroyed by any call.
- The fix. Keeping the sum in s0 shifts the burden: if
squarewanted s0, it would have to save and restore it.
Answer square is allowed to overwrite the temporary t0, and it does; keeping the sum in s0 makes square responsible for preserving it.
Worked example
Counting the 1 bits of a word bit by bit takes 32 passes. How can a loop take one pass per 1 bit instead, and how many passes does 0xF0F0 need?
Show the solution
- Clear the lowest set bit.
x & (x − 1)does it: subtracting 1 turns the lowest 1 into 0 and the 0s below it into 1s, and the AND keeps everything above. - Loop until zero. Repeat until x is 0, counting passes.
- Count the passes for 0xF0F0. It has 8 set bits, so 8 passes, against 32 for testing every bit.
- Write the loop.
loop:
beqz a0, done
addi t0, a0, -1
and a0, a0, t0
addi a1, a1, 1
j loop
done:
Answer clear the lowest set bit with x & (x − 1) on each pass; 0xF0F0 needs 8 passes.
Check
a0 points to an array of a1 signed words (here 5). Return the largest in a0.
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
a0 points to the string "hello, world" (ending in a zero byte), and a1 holds a character code. Return in a0 how many times that character appears.
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
Count the 1 bits in a0 and return the count in a1.
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
Copy a2 words from the address in a0 to the address in a1 (here 4 words, from src to dst).
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
Return in a0 the greatest common divisor of a0 and a1, using Euclid's algorithm with remu.
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
square (provided) returns a0² and clobbers t0 and t1. With n in a0, call it for each i from 1 to n and return the sum of the squares in a0.
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
Write sum recursively: sum(0) = 0 and sum(n) = n + sum(n − 1). Main calls it with n in a0. Each level must save what it needs on the stack, and sp must end where it started.
RISC-V RV32IM (base integer plus multiply and divide). End your program with ebreak. Tab indents; press Esc and then Tab to move on.
Explain why
In the recursive sum, why must each call save ra on the stack, and why can't it keep n in s0 without saving s0 first?
Each jal overwrites ra with its own return address, so a function that makes a call loses the address it was supposed to return to unless it saved ra first; with recursion every level makes a call, so every level must save its own ra in its own frame. n is needed after the inner call returns, so it must be somewhere the inner call won't destroy. A saved register would survive, but the inner call is the same function, which would also put its n in s0. The convention says a function that uses s0 must save the caller's value and restore it, so each level would have to push s0 anyway. Either way, each level's n ends up in its own stack frame.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.