0
0
ARM Architectureknowledge~10 mins

Why subroutines enable modular assembly code in ARM Architecture - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why subroutines enable modular assembly code
Start main program
Call subroutine
Save return address
Execute subroutine code
Return to main program
Continue main program
The main program calls a subroutine, which saves where to return, runs its code, then returns control back to the main program.
Execution Sample
ARM Architecture
BL subroutine
...
subroutine:
  PUSH {LR}
  ; do work
  POP {LR}
  BX LR
This code calls a subroutine, saves the return address, does some work, then returns to the caller.
Analysis Table
StepActionRegister LR (Link Register)Program Counter (PC)Description
1Main program runsLR=unknownPC=main_startProgram starts running main code
2BL subroutine calledLR=address after BLPC=subroutine_startBranch with link saves return address in LR and jumps to subroutine
3PUSH {LR}LR=address after BLPC=subroutine_start+4Subroutine saves LR on stack to preserve return address
4Subroutine workLR=address after BLPC=subroutine_workSubroutine executes its instructions
5POP {LR}LR=address after BLPC=subroutine_endSubroutine restores LR from stack
6BX LRLR=address after BLPC=address after BLBranch back to the instruction after BL in main program
7Main program continuesLR=address after BLPC=address after BLMain program resumes after subroutine call
💡 Execution stops after returning from subroutine and continuing main program
State Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
LR (Link Register)unknownaddress after BLaddress after BLaddress after BLaddress after BLaddress after BL
PC (Program Counter)main_startsubroutine_startsubroutine_start+4subroutine_endaddress after BLaddress after BL
Key Insights - 3 Insights
Why does the BL instruction save the return address in LR?
BL saves the address of the next instruction in LR so the program knows where to return after the subroutine finishes, as shown in step 2 of the execution_table.
Why do we push and pop LR in the subroutine?
Pushing LR saves the return address on the stack to protect it if the subroutine calls other functions; popping LR restores it before returning, as seen in steps 3 and 5.
How does the program know where to continue after the subroutine?
The BX LR instruction branches back to the saved return address in LR, returning control to the main program at the correct spot, shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What value does LR hold after the BL instruction?
AZero
BThe address of the subroutine start
CThe address of the instruction after BL
DThe current PC value
💡 Hint
Check the LR column at step 2 in the execution_table.
At which step does the subroutine restore the LR value from the stack?
AStep 5
BStep 3
CStep 4
DStep 6
💡 Hint
Look for the POP {LR} action in the execution_table.
If the subroutine did not push LR before calling another subroutine, what could happen?
AThe program would run faster
BThe return address might be lost or overwritten
CLR would hold the correct return address anyway
DThe subroutine would not execute
💡 Hint
Refer to the key_moments explanation about saving LR on the stack.
Concept Snapshot
Subroutines let assembly code be modular by allowing code reuse.
The BL instruction calls a subroutine and saves return address in LR.
Subroutines save LR on stack if they call others to protect return address.
BX LR returns control to the caller using saved address.
This enables clear, reusable, and maintainable code blocks.
Full Transcript
In ARM assembly, subroutines enable modular code by allowing the main program to call separate code blocks. The BL instruction saves the return address in the Link Register (LR) and jumps to the subroutine. The subroutine saves LR on the stack to protect it if it calls other subroutines. After completing its work, it restores LR and uses BX LR to return to the main program. This process allows code to be reused and organized in modules, making programs easier to write and maintain.