0
0
ARM Architectureknowledge~10 mins

Branch and link (BL) for subroutines in ARM Architecture - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Branch and link (BL) for subroutines
Start Execution
Encounter BL instruction
Save return address in LR
Jump to subroutine address
Execute subroutine instructions
Return using LR
Continue main program
The BL instruction saves the return address, jumps to the subroutine, and later returns to continue the main program.
Execution Sample
ARM Architecture
BL subroutine_address
...
subroutine_address:
  ; subroutine code
  BX LR
This code calls a subroutine using BL, which saves the return address and jumps to the subroutine, then returns using BX LR.
Analysis Table
StepInstructionActionLR (Link Register)PC (Program Counter)Notes
1BL subroutine_addressSave return address (PC+4) to LR, jump to subroutinePC+4subroutine_addressPrepare to call subroutine
2subroutine instructionsExecute subroutine codePC+4subroutine_address + offsetRunning subroutine
3BX LRBranch to address in LR (return)PC+4PC+4Return to instruction after BL
4Next instruction after BLContinue main programPC+4PC+8Execution resumes
💡 Return to main program after subroutine finishes using LR
State Tracker
RegisterStartAfter BLDuring SubroutineAfter BX LR
LRundefinedAddress of next instruction after BLunchangedunchanged
PCstart addresssubroutine_addressadvances through subroutinereturn address (after BL)
Key Insights - 3 Insights
Why does BL save PC-4 in LR instead of PC?
BL saves PC+4 because PC points to the current instruction plus 8 bytes in ARM, so PC+4 points to the next instruction after BL, ensuring correct return (see execution_table step 1).
How does the program know where to return after the subroutine?
The return address is stored in LR by BL, and the subroutine uses BX LR to jump back (see execution_table steps 1 and 3).
What happens if BX LR is missing in the subroutine?
Without BX LR, the program won't return to the main code, causing incorrect execution flow or crash (no return in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does LR hold immediately after the BL instruction?
AThe address of the instruction after BL (PC+4)
BThe address of the subroutine
CThe current PC value
DZero
💡 Hint
Check execution_table row 1 under LR column.
At which step does the program jump back to the main code after the subroutine?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for the BX LR instruction in execution_table.
If the BL instruction did not save the return address in LR, what would happen?
AThe subroutine would execute and return normally
BThe program would jump to a random address on return
CThe program would continue sequentially without jumping
DThe subroutine would never be called
💡 Hint
Consider the role of LR in execution_table steps 1 and 3.
Concept Snapshot
BL instruction saves the return address (next instruction) in LR.
It then jumps to the subroutine address.
Subroutine executes and returns using BX LR.
This allows the program to resume after the call.
BL is essential for function calls in ARM.
Full Transcript
The Branch and Link (BL) instruction in ARM saves the address of the instruction following BL into the Link Register (LR). It then jumps to the subroutine address specified. The subroutine executes its instructions and returns control to the main program by branching to the address stored in LR using the BX LR instruction. This mechanism allows the program to call functions or subroutines and return correctly to continue execution. The LR holds the return address, and the PC changes to the subroutine address during the call. Without saving the return address, the program would not know where to return after the subroutine finishes.