0
0
ARM Architectureknowledge~10 mins

Stack frame setup in ARM Architecture - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Stack frame setup
Function call
Push return address (LR) onto stack
Push previous frame pointer (FP) onto stack
Set new FP to current SP
Allocate space for local variables by adjusting SP
Function body executes
Restore SP from FP
Pop FP from stack
Pop return address (LR) from stack
Return to caller
This flow shows how a function sets up its stack frame on ARM: saving return address and frame pointer, allocating space, then restoring on return.
Execution Sample
ARM Architecture
push {lr}
push {fp}
add fp, sp, #0
sub sp, sp, #16
; function body
add sp, fp, #0
pop {fp}
pop {pc}
ARM assembly instructions to set up and tear down a stack frame during a function call.
Analysis Table
StepActionSP (Stack Pointer)FP (Frame Pointer)Stack Content (Top to Bottom)
1Function call starts0x10000x0000Empty
2Push LR (return address)0x0FFC0x0000LR
3Push FP (previous frame pointer)0x0FF80x0000FP, LR
4Set FP = SP0x0FF80x0FF8FP, LR
5Allocate locals (sub SP by 16)0x0FE80x0FF8Local vars space, FP, LR
6Function body executes0x0FE80x0FF8Local vars space, FP, LR
7Restore SP from FP0x0FF80x0FF8FP, LR
8Pop FP0x0FFC0x0000LR
9Pop LR (return address)0x10000x0000Empty
10Return to caller0x10000x0000Empty
💡 Function returns; SP and FP restored to original values; stack cleaned.
State Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 7After Step 8Final
SP0x10000x0FFC0x0FF80x0FF80x0FE80x0FF80x0FFC0x1000
FP0x00000x00000x00000x0FF80x0FF80x0FF80x00000x0000
Stack ContentEmptyLRFP, LRFP, LRLocal vars, FP, LRFP, LRLREmpty
Key Insights - 3 Insights
Why do we push the LR (return address) onto the stack at the start?
Because the function may call other functions and overwrite LR, saving it on the stack preserves the return address (see Step 2 in execution_table).
What is the purpose of setting FP = SP after pushing FP?
Setting FP to SP marks the base of the current stack frame, so local variables and parameters can be accessed relative to FP (see Step 4).
Why do we restore SP from FP before popping FP and LR?
Restoring SP from FP deallocates local variables, returning SP to the frame base before popping saved registers (see Step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5, what is the SP value after allocating space for local variables?
A0x0FF8
B0x0FFC
C0x0FE8
D0x1000
💡 Hint
Check the SP column at Step 5 in the execution_table.
At which step is the FP set to the current SP?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look for the action 'Set FP = SP' in the execution_table.
If the function did not push FP onto the stack, what would be missing after Step 8?
APrevious frame pointer (FP)
BLocal variables space
CReturn address (LR)
DStack pointer (SP)
💡 Hint
See what is popped at Step 8 in the execution_table.
Concept Snapshot
Stack frame setup in ARM:
- Push LR (return address) to stack
- Push FP (previous frame pointer)
- Set FP = SP (new frame base)
- Allocate locals by subtracting from SP
- On return, restore SP from FP
- Pop FP and LR
- Return to caller
Full Transcript
When a function is called in ARM architecture, it sets up a stack frame to manage local variables and return information. First, it pushes the return address (LR) onto the stack to save where to return after finishing. Then it pushes the previous frame pointer (FP) to keep track of the caller's frame. The FP is updated to the current SP to mark the new frame base. The stack pointer (SP) is then moved down to allocate space for local variables. After the function body runs, SP is restored from FP to deallocate locals. Finally, FP and LR are popped off the stack to restore the caller's context, and the function returns. This process ensures each function call has its own safe workspace on the stack.