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

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 square ten times.
  • square is 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: jal saves the return address in ra, square runs, and ret jumps back. a0 ends at 385.

Predict

If main kept the running sum in t0 instead of s0, what would a0 hold at the end?

Name it

The calling convention, in brief

RegistersRoleWho saves them
a0–a7arguments; a0 (and a1) return the resultthe caller, if it needs them after the call
t0–t6temporariesthe caller, if it needs them
s0–s11saved registersthe callee, if it uses them
rareturn address, set by jalthe callee, if it calls anything
spstack pointer, a multiple of 16must 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
  1. What t0 is. t0 is a temporary: the convention says a callee may change it without saving it, and square uses it as scratch.
  2. 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.
  3. The fix. Keeping the sum in s0 shifts the burden: if square wanted 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
  1. 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.
  2. Loop until zero. Repeat until x is 0, counting passes.
  3. Count the passes for 0xF0F0. It has 8 set bits, so 8 passes, against 32 for testing every bit.
  4. 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?

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.