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

  1. Start with ARM Subroutine Calls and Nested Returns for labels, BL/BLX, LR, PC, and nested calls.
  2. Continue with ARM Parameters and AAPCS for register, reference, and stack parameters, preserved registers, and return values.
  3. Revisit ARM Stack Conventions and PUSH POP to trace saved registers and stack offsets.

Source coverage by PDF page

PDF pagesMaterial
1-2Defining a subroutine and returning through LR
3-5BL/BLX, nested calls, saving LR and registers
6-13Parameters in registers, by reference, and on the stack
14-18AAPCS 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 BL write to LR, and where does it jump?
  • Why must a subroutine save LR before a nested BL?
  • Which registers carry the first four AAPCS arguments?
  • Why do stack-parameter offsets change after the callee saves registers?