From C to Machine Code: Calls, Stack and the ABI
A C program is made of functions that call each other. The processor has no idea what a function is. A call is two jumps and an agreement about registers, and this page follows one.
You Will Be Able To
- Follow a call and a return through jal, jalr and the register ra.
- Say where the arguments and the result of a function travel.
- Decide which registers may hold a value across a call, and which may not.
- Lesson
- S2.3
- Time
- 28 min
- Runs on
- Your browser
- Check
- 6 questions
Two engineers write two halves of one program. One writes main, the other writes add3, and they never meet. The halves are joined, and the program must still work. For that, main must know where add3 reads its numbers, and add3 must know how to get back.
Before You Start
You need the instruction groups of S2.1. You need jal from S2.18: a jump that also writes a register.
You need to read a few lines of C: a function with arguments, and a return value.
| Question | |
|---|---|
| 1 | Which two instructions make a call and a return? |
| 2 | What does the register ra hold during a call? |
| 3 | Where do the arguments go, and where does the result come back? |
| 4 | Which registers may a callee change freely? |
| 5 | Where must a caller keep a value that it needs after a call? |
The Idea: Two Jumps and an Agreement
A function is called from many places, and must return to the right one each time. A plain jump cannot do that. A jump goes somewhere and forgets where it came from.
So a function call is a jump that remembers. The instruction jal writes the address of the instruction after itself into a register, and then jumps. That saved address is the return address. The function ends with a second jump, to whatever address that register holds.
The function that makes the call is the caller. The function that is called is the callee. The same function can be both: it is called by one function and calls another.
Two jumps are not enough. The caller and the callee must also agree where the arguments are, and which registers the callee may overwrite. That agreement is the calling convention. Every compiler for RISC-V follows the same one.
The convention gives each register a job and a second name. Register x1 gets a name of two letters for its job, the return address: it is called ra. Registers x10 to x17 hold arguments, and are called a0 to a7. From here on, this module uses those names.
New Words
| Word | Meaning |
|---|---|
| Function call | A jump to a function that also keeps the address to come back to. |
| Caller | The function that makes a call. |
| Callee | The function that is called. |
| Return address | The address of the instruction after the call. The callee jumps to it when it is done. |
| Calling convention | The agreement on where arguments, results and return addresses are, and who may change which register. |
| ABI | Application binary interface. The full set of such agreements for one ISA. The calling convention is its main part. |
ra | Register x1 under its second name. It holds the return address. |
jalr | Jump and link register. A jump to the address in a register plus an immediate. It can write a return address too. |
| Argument register | One of a0 to a7. Arguments go in, in order. The result comes back in a0. |
| Temporary register | One of t0 to t6. A callee may change it without asking. |
| Saved register | One of s0 to s11. A callee must hand it back with the value it had. |
| Stack | A region of memory where functions keep values while they run. The next lesson covers it. |
| Hand-compiled | Turned from C into assembly by a person, not by a compiler. All assembly on this page is hand-compiled. |
One Picture: A Call and a Return
- Processor
- Bus
- Control / FSM
- The arguments
The caller puts 1, 2 and 3 into a0, a1 and a2. The callee knows to look there, because both obey one convention.
- The call
One instruction does two things. It writes the address of the instruction after itself into ra. And it writes the address of add3 into the PC.
- The work
The callee reads its arguments and puts the sum into a0. It does not know who called it.
- The return
The callee ends with one instruction that copies ra into the PC. The caller goes on where it left off.
- The result
Back in the caller, a0 holds the result. The caller may use it at once.
Read the diagram as text
- The caller: main: the function that makes the call
- a0, a1, a2: the arguments go out; a0 brings the result back
- The callee: add3: the function that is called
- ra: holds the address to come back to
- PC: the address of the next instruction
- The caller: main → a0, a1, a2: writes the arguments
- a0, a1, a2 → The callee: add3: reads them
- PC → ra: jal: PC + 4
- The caller: main → PC: jal: the address of add3
- The callee: add3 → a0, a1, a2: writes the result into a0
- ra → PC: jalr: the PC takes ra
The callee never names its caller. The return address in ra is the only link back.
| Name | Register | Job | After a call, the caller finds it |
|---|---|---|---|
| zero | x0 | always 0 | unchanged: it cannot change |
| ra | x1 | the return address | changed by its own call |
| sp | x2 | a pointer into the stack: the next lesson | unchanged |
| gp, tp | x3, x4 | two pointers set once at start-up | unchanged |
| t0 to t2 | x5 to x7 | temporary values | perhaps changed |
| s0, s1 | x8, x9 | saved values | unchanged |
| a0 to a7 | x10 to x17 | arguments; a0 also carries the result | perhaps changed |
| s2 to s11 | x18 to x27 | saved values | unchanged |
| t3 to t6 | x28 to x31 | temporary values | perhaps changed |
Count the rows: 1 + 1 + 1 + 2 + 3 + 2 + 8 + 10 + 4 is 32. The hardware treats x1 to x31 alike. Only the convention tells them apart.
The Smallest Example, Worked by Hand
Here is a program of two functions in C. The function main calls add3 with 1, 2 and 3, and adds 10 to what comes back.
int add3(int a, int b, int c) {
return a + b + c;
}
int main(void) {
return add3(1, 2, 3) + 10;
}Below is the same program in assembly, compiled by hand. The function main starts at address 0, and add3 at address 18 in hex.
| Address | Assembly | From the C |
|---|---|---|
| 00 | addi a0, zero, 1 | the first argument |
| 04 | addi a1, zero, 2 | the second argument |
| 08 | addi a2, zero, 3 | the third argument |
| 0c | jal ra, 0x18 | the call of add3 |
| 10 | addi a0, a0, 10 | + 10, into the place of the return value of main |
| 14 | ebreak | the end of main: the lab stops here |
| 18 | add a0, a0, a1 | add3: a + b |
| 1c | add a0, a0, a2 | + c, into the place of the return value |
| 20 | jalr zero, 0(ra) | return |
Addresses are in hex. A real main returns to its own caller. Here it stops at an ebreak.
Follow the call. The jal is at address c. The instruction after it is at 10. So the jal writes 10 into ra, and the PC goes to 18.
Follow the return. The instruction jalr zero, 0(ra) adds 0 to ra and puts the sum into the PC. So the PC goes to 10. Its destination register is zero, which means: keep no return address this time.
The Rules
Rule 1: A call is jal ra, with the address of the function.
It writes the address of the next instruction into ra, and jumps.
Rule 2: A return is jalr zero, 0(ra).
The PC takes the value of ra. Assemblers accept the short form ret.
Rule 3: Arguments travel in a0 to a7, in order.
The result comes back in a0.
Rule 4: A callee may change every temporary and every argument register.
That is t0 to t6 and a0 to a7. A call also changes ra.
Rule 5: A callee must hand back s0 to s11 and sp as it got them.
If it uses one, it must save the old value first and restore it at the end.
Rule 6: A value that the caller needs after a call belongs in a saved register.
Or in memory. Never in a temporary or an argument register.
Sources for These Rules
Every fact about the calling convention on this page is stated as commonly described. That covers the names and jobs of the registers, and which of them a callee may change. It also covers the use of jal and jalr for a call and a return. The document that defines the convention is not in the reference library. So none of these facts is checked against it. The register names were compared with the tables of the assembler of this track.
The plan of this track points to two textbooks for this material. They are Patterson and Hennessy, section 2.8, and Harris and Harris, section 6.3. Those places are not checked either.
Now Run It
The lab at the end of this page runs the hand-compiled program on the small model. The rows now show the registers under their second names.
00000000 | addi a0, zero, 1 | a0 = 00000001
00000004 | addi a1, zero, 2 | a1 = 00000002
00000008 | addi a2, zero, 3 | a2 = 00000003
0000000c | jal ra, 0x18 | ra = 00000010, next is 00000018
00000018 | add a0, a0, a1 | a0 = 00000003
0000001c | add a0, a0, a2 | a0 = 00000006
00000020 | jalr zero, 0(ra) | next is 00000010
00000010 | addi a0, a0, 10 | a0 = 00000010
00000014 | ebreak | stop
a0=16 ra=00000010Rule 3 at work: rows 1 to 3 load the arguments. Rows 5 and 6 are the callee, and they read a0, a1 and a2.
Rule 1 at work: row 4. The jal at c writes 10 into ra, and the next instruction is at 18.
Rule 2 at work: row 7. The jalr sends the PC to 10, the value of ra. Row 8 is main again.
Rule 3 again: row 8 reads a0, which holds the 6 from add3, and adds 10. The last row shows 16.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| The return address in a register | The call writes it to memory | A function that calls nothing touches no memory at all. A function that does call must save ra itself. |
| Arguments in registers | Arguments in memory | No loads and stores for most calls. Only 8 registers: more arguments go to memory after all. |
| Two kinds of register: temporary and saved | The caller saves everything, or the callee does | Each side saves only what it uses. Every programmer and compiler must know the two lists. |
| One convention for everybody | Each compiler chooses | Code from any compiler, and hand-written assembly, can be joined. The convention can never change. |
| The convention outside the hardware | A call instruction that saves registers itself | The hardware stays simple: jal and jalr are ordinary jumps. Nothing checks that the convention is obeyed. |
The Trap: The Temporary That Was Lost in a Call
A programmer writes main by hand. It keeps a number k, which is 5, and calls a function named twice with 3. Then it adds k to the result. The answer should be 6 plus 5, which is 11.
A value needs a register, and t0 is free, so k goes into t0. The function twice was written by someone else. It uses t0 as scratch space, which the convention allows.
00000000 | addi t0, zero, 5 | t0 = 00000005
00000004 | addi a0, zero, 3 | a0 = 00000003
00000008 | jal ra, 0x14 | ra = 0000000c, next is 00000014
00000014 | add t0, a0, a0 | t0 = 00000006
00000018 | addi a0, t0, 0 | a0 = 00000006
0000001c | jalr zero, 0(ra) | next is 0000000c
0000000c | add a0, a0, t0 | a0 = 0000000c
00000010 | ebreak | stop
a0=12Read row 4. The callee writes 6 into t0. The 5 of main is gone, and nothing reported it.
Read the last two rows. Back in main, the add reads t0 and gets 6. The result is 12, not 11.
Both functions are correct by themselves. The bug is in main: it broke rule 6. With k in the saved register s0, the same program gives the right answer:
0000000c | add a0, a0, s0 | a0 = 0000000b
00000010 | ebreak | stop
a0=11This main may use s0 freely, because nothing called it. A function with a caller must save s0 first. The next lesson shows how.
At Scale
This section looks ahead. The convention is what lets millions of functions from different authors run as one program.
Calls inside calls
A callee that calls another function overwrites ra. It must keep the old value somewhere: the next lesson.
Calls that are far away
A jal reaches about 1 MiB. A call beyond that is an auipc and a jalr: this is why jalr adds an immediate to a register.
Calls through a pointer
When the address of a function is known only while the program runs, the call is a jalr through a register.
Interrupts
An interrupt arrives between any two instructions and obeys no convention. Its handler must save every register it touches. Module S6 covers that.
Try It
Run the lab. Find the row that writes ra, and the row that reads it.
Change the program so that main computes add3(1, 2, 3) + add3(4, 5, 6). Where must the first result wait?
Replace the return of add3 with a jump to address 10 in hex. It works. Why is that still wrong for a function?
00000000 | addi a0, zero, 1 | a0 = 00000001 00000004 | addi a1, zero, 2 | a1 = 00000002 00000008 | addi a2, zero, 3 | a2 = 00000003 0000000c | jal ra, 0x18 | ra = 00000010, next is 00000018 00000018 | add a0, a0, a1 | a0 = 00000003 0000001c | add a0, a0, a2 | a0 = 00000006 00000020 | jalr zero, 0(ra) | next is 00000010 00000010 | addi a0, a0, 10 | a0 = 00000010 00000014 | ebreak | stop a0=16 ra=00000010Check Yourself
Common Mistakes
Wrong Keep a value in t0 or a1 across a call.
A callee may change every temporary and argument register.
Fix Keep it in a saved register, s0 to s11, or in memory.
Wrong Return with a jump to a fixed address.
The function then works for one caller only.
Fix Return with jalr zero, 0(ra), which goes back to whoever called.
Wrong Use a saved register in a callee without saving it.
The caller expects its value back. The caller then computes with a wrong number.
Fix Save the old value first, and restore it before the return.
Remember
- A call is jal ra: it keeps the address of the next instruction in ra. A return is jalr zero, 0(ra).
- Arguments travel in a0 to a7. The result comes back in a0.
- A callee may change t0 to t6, a0 to a7 and ra. It must hand back s0 to s11 and sp.
- A value that must survive a call lives in a saved register or in memory.