Course: Computer Architectures
Source: R. Ferrero, Politecnico di Torino, 9_subroutines.pdf, 18 PDF pages. The local source remains in Downloads/Architecture/9_subroutines.pdf; explanations below distinguish source material from added traces.
Why this lesson matters
A subroutine lets a program reuse code, but a call must preserve two things: the address to return to and any register values the caller still needs. ARM’s BL, LR, PUSH, POP, and procedure-call convention form one system for making that safe.
Learning route
- Start with ARM Subroutine Calls and Nested Returns for labels,
BL/BLX,LR,PC, and nested calls. - Continue with ARM Parameters and AAPCS for register, reference, and stack parameters, preserved registers, and return values.
- Revisit ARM Stack Conventions and PUSH POP to trace saved registers and stack offsets.
Source coverage by PDF page
| PDF pages | Material |
|---|---|
| 1-2 | Defining a subroutine and returning through LR |
| 3-5 | BL/BLX, nested calls, saving LR and registers |
| 6-13 | Parameters in registers, by reference, and on the stack |
| 14-18 | AAPCS register roles, caller/callee obligations and return values |
Core call pattern
subroutine:
PUSH {r4, LR}
; work, possibly including another BL
POP {r4, PC}BL writes the return address into LR and jumps by changing PC. A nested BL overwrites LR, so a non-leaf subroutine must save its incoming LR before making another call.
Stack parameter trace
Added trace based on the source’s example: after the caller executes PUSH {r0, r1, r2}, a callee that executes PUSH {r4, r5, r6, LR} has moved SP by 16 bytes. The caller’s values are therefore at [SP,#16], [SP,#20], and [SP,#24]. The offsets come from the callee’s four saved words, not from the original register names.
Review questions
- What does
BLwrite toLR, and where does it jump? - Why must a subroutine save
LRbefore a nestedBL? - Which registers carry the first four AAPCS arguments?
- Why do stack-parameter offsets change after the callee saves registers?