0
0
ARM Architectureknowledge~10 mins

Preserving callee-saved registers in ARM Architecture - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Preserving callee-saved registers
Function Entry
Save callee-saved registers on stack
Execute function body
Restore callee-saved registers from stack
Return to caller
When a function starts, it saves certain registers on the stack to keep their values safe. After running, it restores them before returning.
Execution Sample
ARM Architecture
push {r4, r5, lr}
...function body...
pop {r4, r5, pc}
This code saves registers r4, r5, and the return address (lr) at the start, runs the function, then restores them before returning.
Analysis Table
StepActionRegisters SavedStack StateRegisters RestoredReturn Address
1Function EntryNoneEmptyNoneUnknown
2Push r4, r5, lrr4, r5, lrr4, r5, lr saved on stackNoneUnknown
3Execute function bodyr4, r5, lrr4, r5, lr saved on stackNoneUnknown
4Pop r4, r5, pcr4, r5, lrEmptyr4, r5 restoredReturn to caller
5ReturnNoneEmptyAll restoredReturned
💡 Function returns after restoring callee-saved registers and loading return address into pc
State Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
r4Original valueSaved on stackUnchangedRestored from stackOriginal value
r5Original valueSaved on stackUnchangedRestored from stackOriginal value
lrReturn addressSaved on stackUnchangedUsed to returnN/A (used)
StackEmptyr4, r5, lr pushedUnchangedPopped emptyEmpty
Key Insights - 3 Insights
Why do we save callee-saved registers at the start of the function?
Because these registers must keep their values after the function finishes, saving them on the stack protects their original values during the function execution (see execution_table step 2).
What happens if we forget to restore callee-saved registers before returning?
The caller might get wrong values in those registers, causing errors. The execution_table step 4 shows restoring them before return is essential.
Why is the link register (lr) saved and restored?
The lr holds the return address. Saving it ensures the function can return correctly after execution (see execution_table steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. Which registers are saved on the stack?
Ar4, r5, lr
Br0, r1, r2
Cpc, sp, lr
Dr6, r7, r8
💡 Hint
Check the 'Registers Saved' column at step 2 in the execution_table.
At which step does the function restore the saved registers?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Registers Restored' column in the execution_table.
If the function did not save the lr register, what would happen?
AThe stack would overflow.
BThe function would still return correctly.
CThe return address might be lost, causing incorrect return.
DRegisters r4 and r5 would be corrupted.
💡 Hint
Refer to the key_moments about why lr is saved and restored.
Concept Snapshot
Preserving callee-saved registers:
- Save registers like r4, r5, lr on function entry (push).
- Execute function code safely.
- Restore registers before returning (pop).
- Ensures caller's register values stay unchanged.
- lr holds return address; must be saved/restored.
Full Transcript
When a function starts in ARM architecture, it saves certain registers called callee-saved registers, such as r4, r5, and the link register (lr), onto the stack. This protects their values while the function runs. After the function finishes its work, it restores these registers from the stack before returning to the caller. This process ensures that the caller's register values remain unchanged and the function returns correctly using the saved return address in lr.