The Stack Frame: Saving What a Call Would Destroy
A function that calls another has a problem: the call overwrites ra, and the registers run out. The cure is a small piece of memory that each function borrows and gives back. This page watches it grow and shrink.
You Will Be Able To
- Say what the stack pointer holds, and in which direction the stack grows.
- Lay out the frame of a function: what it must save, and at which offset from sp.
- Follow a nested call, and say what each word of the stack holds at each moment.
- Lesson
- S2.19
- Time
- 30 min
- Runs on
- Your browser
- Check
- 6 questions
In S2.3, main called add3 and all went well. Now let add3 call a third function. Its jal writes a new return address into ra, over the old one. Unless the way back to main was put somewhere safe first, the program can never get home.
Before You Start
You need calls and returns from S2.3: jal writes ra, jalr returns through it, and saved registers must come back unchanged.
You need loads and stores with an offset, such as sw ra, 12(sp), from S2.1.
| Question | |
|---|---|
| 1 | What does sp hold, and which way does the stack grow? |
| 2 | What must a function save before it calls another function? |
| 3 | What must a function do before it uses a saved register? |
| 4 | Why does the frame of a function vanish when the function returns? |
| 5 | What happens when a function calls another and does not save ra? |
The Idea: Borrow a Piece of Memory, and Give It Back
A function needs a safe place for a few values. They are its return address, and the old values of the saved registers it wants to use. Registers cannot be that place. The next call may take them.
Memory can. One region of memory is set aside for this, and it is called the stack. One register always says where its used part ends. That register is x2, and its name is sp, for stack pointer.
The stack grows toward lower addresses. A function takes room by subtracting from sp, and gives it back by adding the same number. The room it takes is its stack frame, or frame for short.
Frames pile up like plates. When main calls a function, a frame goes on top. When that function calls another, a second frame goes on top of the first. Each return takes the top frame away. The last one put on is the first one taken off.
Not every function needs a frame. A function that calls nothing, and uses no saved register, has nothing to protect. It is called a leaf function.
New Words
| Word | Meaning |
|---|---|
| Stack | A region of memory in which functions keep values while they run. It grows toward lower addresses. |
| Stack pointer | The register x2, named sp. It holds the lowest address of the stack that is in use. |
| Stack frame | The part of the stack that one function has taken. Frame for short. |
| Stack push | Putting a value on the stack: make room below sp, and store the value there. |
| Stack pop | Taking a value off the stack: load it, and give the room back. |
| Function entry code | The first instructions of a function: they make the frame and save registers into it. |
| Function exit code | The last instructions of a function: they restore the registers and give the frame back. |
| Leaf function | A function that calls no other function. It often needs no frame. |
| Nested call | A call made by a function that was itself called. |
| Stack overflow | The stack growing past the memory set aside for it, into other data. |
One Picture: A Frame Comes and Goes
- Processor
- Memory
- Control / FSM
- Before the call
The stack is empty. The stack pointer holds 8001_0000, one past the last byte of the SRAM.
- The call from main
The jal in main writes 8 into ra. That is the only copy of the way back to main.
- A frame is made
The function subtracts 16 from sp. The 16 bytes from 8000_fff0 up now belong to it.
- Three words are saved
It stores ra, s0 and s1 into its frame. Now it may call, and may use s0 and s1.
- The nested calls
Each call of inc overwrites ra. That does no harm: the old value is in memory. The function inc makes no frame.
- The frame is given back
The function loads ra, s0 and s1 from the frame and adds 16 to sp. Then it returns to address 8.
Read the diagram as text
- main: calls twice_inc; its sp is 8001_0000
- twice_inc: calls inc twice, so it needs a frame
- inc: calls nothing, and needs no frame
- Word at 8000_fffc: the saved ra: 8, the way back to main
- Word at 8000_fff8: the saved s0 of main
- Word at 8000_fff4: the saved s1 of main
- Word at 8000_fff0: not used: it keeps the frame at 16 bytes
- sp: 8001_0000, then 8000_fff0, then 8001_0000 again
- main → twice_inc: jal: ra = 8
- sp → Word at 8000_fff0: sp points here
- twice_inc → Word at 8000_fffc: saves ra
- twice_inc → Word at 8000_fff8: saves s0
- twice_inc → Word at 8000_fff4: saves s1
- twice_inc → inc: jal: ra is overwritten
A frame lives exactly as long as its function runs. After the return, sp is where it was.
The Smallest Example, Worked by Hand
Three functions in C. The function main calls twice_inc, and twice_inc calls inc two times.
int inc(int num) {
return num + 1;
}
int twice_inc(int num) {
int first = inc(num);
return first + inc(num);
}
int main(void) {
return twice_inc(5);
}Look at twice_inc as a compiler would. It makes calls, so ra will be overwritten. It needs num again after the first call, and first after the second. Those two values must survive a call, so they go into s0 and s1. But s0 and s1 belong to main until they are saved.
So the frame needs three words: for ra, s0 and s1. Three words are 12 bytes. The convention keeps sp at a multiple of 16, so the frame is 16 bytes.
| Offset from sp | Address | Holds |
|---|---|---|
| 12 | 8000_fffc | the ra of twice_inc: the way back to main |
| 8 | 8000_fff8 | the s0 of main |
| 4 | 8000_fff4 | the s1 of main |
| 0 | 8000_fff0 | nothing: it fills the frame up to 16 bytes |
Before the call, sp is 8001_0000. Inside twice_inc it is 8000_fff0, which is 16 less.
| Address | Assembly | What it does |
|---|---|---|
| 0c | addi sp, sp, -16 | makes the frame |
| 10 to 18 | sw ra, 12(sp); sw s0, 8(sp); sw s1, 4(sp) | saves three registers |
| 1c | addi s0, a0, 0 | keeps num in s0 |
| 20 | jal ra, 0x48 | calls inc; a0 already holds num |
| 24 | addi s1, a0, 0 | keeps first in s1 |
| 28 | addi a0, s0, 0 | the argument num again |
| 2c | jal ra, 0x48 | calls inc |
| 30 | add a0, s1, a0 | first plus the second result |
| 34 to 3c | lw ra, 12(sp); lw s0, 8(sp); lw s1, 4(sp) | restores the three registers |
| 40 | addi sp, sp, 16 | gives the frame back |
| 44 | jalr zero, 0(ra) | returns to main |
Addresses are in hex. The function inc is two instructions at 48: addi a0, a0, 1 and a return.
The Rules
Rule 1: sp holds the lowest address of the stack in use.
The stack grows toward lower addresses.
Rule 2: A function makes its frame by subtracting from sp, and gives it back by adding the same number.
The number is a multiple of 16.
Rule 3: A function that makes a call saves ra in its frame first.
It restores ra before its own return.
Rule 4: A function that uses a saved register saves the old value in its frame first.
It restores the value before it returns.
Rule 5: A function writes only into its own frame: from sp up to the sp it was given.
Memory below sp belongs to nobody. The next call will overwrite it.
Rule 6: A leaf function that uses only temporary and argument registers needs no frame.
It never touches sp.
Sources for These Rules
Every fact about the stack and the calling convention on this page is stated as commonly described. That covers the direction of growth, what sp points at, the multiple of 16, and who saves ra and the saved registers. The document that defines the convention is not in the reference library. So none of these facts is checked against it.
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.
The start of the stack at 8001_0000 is a choice of this page. It is one past the last byte of the 64 KiB of SRAM of ap_soc. The values 1111_1111 and 2222_2222 stand for whatever main keeps in s0 and s1.
Now Run It
The lab at the end of this page runs the three functions on the small model. Before the start, sp holds 8001_0000, and s0 and s1 hold two values of main that are easy to spot.
00000000 | addi a0, zero, 5 | a0 = 00000005
00000004 | jal ra, 0xc | ra = 00000008, next is 0000000c
0000000c | addi sp, sp, -16 | sp = 8000fff0
00000010 | sw ra, 12(sp) | mem[8000fffc] = 00000008
00000014 | sw s0, 8(sp) | mem[8000fff8] = 11111111
00000018 | sw s1, 4(sp) | mem[8000fff4] = 22222222
0000001c | addi s0, a0, 0 | s0 = 00000005
00000020 | jal ra, 0x48 | ra = 00000024, next is 00000048
00000048 | addi a0, a0, 1 | a0 = 00000006
0000004c | jalr zero, 0(ra) | next is 00000024
00000024 | addi s1, a0, 0 | s1 = 00000006
00000028 | addi a0, s0, 0 | a0 = 00000005
0000002c | jal ra, 0x48 | ra = 00000030, next is 00000048
00000048 | addi a0, a0, 1 | a0 = 00000006
0000004c | jalr zero, 0(ra) | next is 00000030
00000030 | add a0, s1, a0 | a0 = 0000000c
00000034 | lw ra, 12(sp) | ra = 00000008 from mem[8000fffc]
00000038 | lw s0, 8(sp) | s0 = 11111111 from mem[8000fff8]
0000003c | lw s1, 4(sp) | s1 = 22222222 from mem[8000fff4]
00000040 | addi sp, sp, 16 | sp = 80010000
00000044 | jalr zero, 0(ra) | next is 00000008
00000008 | ebreak | stop
a0=12 s0=11111111 s1=22222222 sp=80010000Rule 2 at work: rows 3 and 20. The stack pointer goes down by 16 to 8000_fff0, and comes back to 8001_0000.
Rules 3 and 4 at work: rows 4 to 6. The three stores land at 8000_fffc, 8000_fff8 and 8000_fff4, as in the table of the frame.
Now watch ra. Row 8 overwrites it with 24, and row 13 with 30. Neither is the way back to main.
Rule 3 again: row 17. The load brings 8 back from 8000_fffc. Row 21 then returns to address 8, in main.
Rule 6 at work: the four rows of inc, at 48 and 4c. They never mention sp.
Read the last row. The result is 12, and s0, s1 and sp hold what they held before the call. For main, nothing has happened but the result.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A stack: last in, first out | A fixed place in memory for each function | A function can call itself, and memory is used only while a function runs. The depth must be planned. |
| One subtraction for the whole frame | A separate push for each value | The offsets from sp stay fixed inside the function. Some bytes of the frame may go unused. |
| The callee saves the saved registers | The caller saves everything it needs | A register is saved once per function, not once per call. A function that needs none saves none. |
| The stack grows downward | Upward | Program data can grow up from the other end of the same memory. Nothing stops the two from meeting. |
| sp kept at a multiple of 16 | Any multiple of 4 | Larger values can sit at addresses that are multiples of their size. Small frames waste a few bytes. |
The Trap: The Function That Could Never Get Home
A programmer writes a small function by hand, called add_two. It calls inc two times in a row. It uses no saved register, so it seems to need no frame at all.
int add_two(int num) {
return inc(inc(num));
}00000000 | addi a0, zero, 5 | a0 = 00000005
00000004 | jal ra, 0xc | ra = 00000008, next is 0000000c
0000000c | jal ra, 0x18 | ra = 00000010, next is 00000018
00000018 | addi a0, a0, 1 | a0 = 00000006
0000001c | jalr zero, 0(ra) | next is 00000010
00000010 | jal ra, 0x18 | ra = 00000014, next is 00000018
00000018 | addi a0, a0, 1 | a0 = 00000007
0000001c | jalr zero, 0(ra) | next is 00000014
00000014 | jalr zero, 0(ra) | next is 00000014
00000014 | jalr zero, 0(ra) | next is 00000014
00000014 | jalr zero, 0(ra) | next is 00000014
00000014 | jalr zero, 0(ra) | next is 00000014
still running after 12 instructions
a0=7 ra=00000014Read row 2. The call from main writes 8 into ra. That is the way back to main.
Read rows 3 and 6. Each call of inc writes ra again: first 10, then 14. The 8 is gone.
Read row 9. The return of add_two goes to the address in ra, which is 14. That is the return instruction itself. So it returns to itself, for ever. The answer 7 sits in a0, and main never sees it.
This is rule 3. A function that makes even one call must save ra. Only a leaf function may skip the frame.
At Scale
This section looks ahead. On a real chip the stack is one of the first things set up, and one of the first things to go wrong.
Who sets sp
Nothing sets sp at reset. The start-up code of the chip must do it before the first call. Module S6 covers that code.
Stack overflow
Nothing checks the stack. Too many nested calls move sp down into other data. Memory protection can catch that: S3.25 sets a trap below a stack.
Local variables
Arrays and other variables that do not fit in registers live in the frame as well. Frames then grow well past 16 bytes.
One stack for each task
An operating system gives every task a stack of its own. A switch of task is, in essence, a switch of sp. Common practice.
Try It
Run the lab. Mark the rows where sp changes, and the rows that write or read the frame.
Remove the three rows that restore ra, s0 and s1. Predict the last row, and where the program ends up.
Change inc so that it uses s0 as scratch, without saving it. Which value in the last row is now wrong, and whose fault is it?
00000000 | addi a0, zero, 5 | a0 = 00000005 00000004 | jal ra, 0xc | ra = 00000008, next is 0000000c 0000000c | addi sp, sp, -16 | sp = 8000fff0 00000010 | sw ra, 12(sp) | mem[8000fffc] = 00000008 00000014 | sw s0, 8(sp) | mem[8000fff8] = 11111111 00000018 | sw s1, 4(sp) | mem[8000fff4] = 22222222 0000001c | addi s0, a0, 0 | s0 = 00000005 00000020 | jal ra, 0x48 | ra = 00000024, next is 00000048 00000048 | addi a0, a0, 1 | a0 = 00000006 0000004c | jalr zero, 0(ra) | next is 00000024 00000024 | addi s1, a0, 0 | s1 = 00000006 00000028 | addi a0, s0, 0 | a0 = 00000005 0000002c | jal ra, 0x48 | ra = 00000030, next is 00000048 00000048 | addi a0, a0, 1 | a0 = 00000006 0000004c | jalr zero, 0(ra) | next is 00000030 00000030 | add a0, s1, a0 | a0 = 0000000c 00000034 | lw ra, 12(sp) | ra = 00000008 from mem[8000fffc] 00000038 | lw s0, 8(sp) | s0 = 11111111 from mem[8000fff8] 0000003c | lw s1, 4(sp) | s1 = 22222222 from mem[8000fff4] 00000040 | addi sp, sp, 16 | sp = 80010000 00000044 | jalr zero, 0(ra) | next is 00000008 00000008 | ebreak | stop a0=12 s0=11111111 s1=22222222 sp=80010000Check Yourself
Common Mistakes
Wrong Call another function without saving ra.
The jal overwrites ra. The way back to your own caller is lost.
Fix Save ra in the frame before the first call, and restore it before the return.
Wrong Store below sp without moving sp.
Memory below sp belongs to nobody. The next callee puts its frame there.
Fix Subtract from sp first. Then store at sp and above.
Wrong Give back fewer bytes than were taken.
The caller then finds a wrong sp, and reads its own saved values from wrong places.
Fix Add back exactly what was subtracted, on every path to the return.
Remember
- The stack grows toward lower addresses. sp holds the lowest address in use.
- A frame is made by subtracting from sp and given back by adding the same number.
- A function that calls saves ra. A function that uses a saved register saves the old value.
- A leaf function that uses only temporary and argument registers needs no frame.