Bird
Raised Fist0
ARM Architectureknowledge~10 mins

Link register (R14/LR) in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Link register (R14/LR)
Function Call Instruction
↓
Store Return Address in LR
↓
Jump to Function
↓
Function Executes
↓
Return Instruction
↓
Branch to Address in LR
↓
Resume Execution After Call
When a function is called, the processor saves the return address in the link register (R14/LR). After the function finishes, it uses LR to return to the correct place in the code.
Execution Sample
ARM Architecture
BL subroutine
...subroutine:
  ...
  MOV PC, LR
Calls a subroutine, saves return address in LR, then returns by moving LR back to PC.
Analysis Table
StepInstructionActionLR ValuePC ValueNotes
1BL subroutineSave return address (next instruction) to LR, jump to subroutineAddress of next instructionAddress of subroutineLR holds return address
2Execute subroutine instructionsRun subroutine codeAddress of next instructionSubroutine instructionsLR unchanged
3MOV PC, LRSet PC to LR to returnAddress of next instructionReturn addressReturn to caller
4Continue after BLResume normal executionReturn addressNext instruction after BLFunction call complete
💡 Return address in LR used to branch back after subroutine finishes
State Tracker
RegisterStartAfter Step 1After Step 2After Step 3Final
LRUndefinedReturn address savedReturn address savedReturn address savedReturn address saved
PCStart addressSubroutine addressSubroutine addressReturn addressNext instruction after call
Key Insights - 3 Insights
Why does LR hold the return address instead of the PC?
LR is a special register used to store the return address when a function is called, so the PC can jump to the function without losing where to return. See execution_table step 1 where LR saves the return address.
What happens if the LR value is changed inside the subroutine?
If LR is changed, the return address is lost or incorrect, so the program may jump to the wrong place after return. This is why preserving LR is important if the subroutine calls other functions.
How does the MOV PC, LR instruction return from the function?
MOV PC, LR sets the program counter to the address stored in LR, effectively jumping back to the instruction after the original function call, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 1. What value does LR hold after the BL instruction?
AThe address of the next instruction after BL
BThe address of the subroutine
CZero
DThe current PC value before BL
💡 Hint
Check the LR Value column in step 1 of the execution_table.
At which step does the program counter (PC) get set back to the return address?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the PC Value and Action columns in the execution_table.
If the LR register is modified inside the subroutine, what is the likely effect?
AThe program returns correctly to the caller
BThe subroutine runs twice
CThe program may jump to an incorrect address after return
DThe PC is reset to zero
💡 Hint
Refer to the key_moments about LR modification and its effect.
Concept Snapshot
Link Register (R14/LR) stores the return address during function calls.
BL instruction saves next instruction address to LR and jumps to function.
MOV PC, LR returns by jumping to saved address in LR.
Modifying LR inside functions can cause incorrect returns.
LR helps keep track of where to continue after a subroutine.
Full Transcript
The link register, also called R14 or LR, is a special register in ARM processors. When a function call happens using the BL instruction, the processor saves the address of the next instruction into LR. Then it jumps to the function's code. Inside the function, LR holds this return address. When the function finishes, it uses MOV PC, LR to jump back to the saved address, resuming normal execution. If LR is changed inside the function, the return address can be lost, causing the program to jump to the wrong place. This trace shows how LR and PC change step-by-step during a function call and return.

Practice

(1/5)
1. What is the primary purpose of the link register (R14/LR) in ARM architecture?
easy
A. To manage stack pointer operations
B. To hold the current instruction being executed
C. To store general-purpose data temporarily
D. To store the return address during function calls

Solution

  1. Step 1: Understand the role of the link register

    The link register (R14/LR) is specifically designed to hold the return address when a function call is made.
  2. Step 2: Differentiate from other registers

    Other registers like the program counter hold the current instruction, and the stack pointer manages the stack, but LR stores the return address.
  3. Final Answer:

    To store the return address during function calls -> Option D
  4. Quick Check:

    Link register = return address storage [OK]
Hint: LR always holds return address after a function call [OK]
Common Mistakes:
  • Confusing LR with the program counter
  • Thinking LR stores general data
  • Mixing LR with stack pointer
2. Which ARM instruction saves the return address into the link register (LR)?
easy
A. BL
B. ADD
C. MOV
D. STR

Solution

  1. Step 1: Identify the instruction for function calls

    The BL (Branch with Link) instruction branches to a function and saves the return address in LR.
  2. Step 2: Understand other instructions

    MOV moves data, ADD adds values, and STR stores data to memory; none save return addresses.
  3. Final Answer:

    BL -> Option A
  4. Quick Check:

    BL saves return address in LR [OK]
Hint: BL means branch and link (save return) [OK]
Common Mistakes:
  • Choosing MOV as it moves data
  • Confusing STR with saving return address
  • Thinking ADD saves addresses
3. Consider this ARM code snippet:
BL function
...
function:
MOV R0, #5
BX LR

What happens when the instruction BX LR executes?
medium
A. The program jumps back to the instruction after BL
B. The program jumps to the start of function
C. The program halts execution
D. The program jumps to an undefined address

Solution

  1. Step 1: Understand what BL does

    BL saves the return address (next instruction after BL) into LR and jumps to 'function'.
  2. Step 2: Analyze BX LR instruction

    BX LR branches to the address stored in LR, which is the return address after BL.
  3. Final Answer:

    The program jumps back to the instruction after BL -> Option A
  4. Quick Check:

    BX LR returns to caller address [OK]
Hint: BX LR returns to saved return address [OK]
Common Mistakes:
  • Thinking BX LR jumps to function start
  • Assuming program halts on BX LR
  • Believing BX LR jumps to random address
4. In this ARM code, what is the error?
BL function
...
function:
MOV R0, #10
MOV PC, R0
medium
A. BL instruction is invalid
B. MOV PC, R0 does not return to caller correctly
C. MOV R0, #10 is not allowed
D. Function label is missing

Solution

  1. Step 1: Check the return method

    To return from a function, the program must jump to the address in LR, not to a fixed value in R0.
  2. Step 2: Identify the problem with MOV PC, R0

    MOV PC, R0 sets the program counter to 10, which is not a valid return address, causing incorrect behavior.
  3. Final Answer:

    MOV PC, R0 does not return to caller correctly -> Option B
  4. Quick Check:

    Return must use LR, not arbitrary register [OK]
Hint: Return by jumping to LR, not arbitrary register [OK]
Common Mistakes:
  • Thinking BL is invalid
  • Assuming MOV R0, #10 is wrong
  • Ignoring the need to use LR for return
5. You want to write a function in ARM that calls another function and then returns correctly. Which sequence correctly uses the link register (LR)?
hard
A. Use B to call the function, then return with BX R14
B. Use B to call the function, then return with MOV PC, LR
C. Use BL to call the function, then return with BX LR
D. Use BL to call the function, then return with MOV PC, R0

Solution

  1. Step 1: Understand function call with BL

    BL saves the return address in LR and branches to the called function.
  2. Step 2: Understand correct return instruction

    Returning is done by branching to LR using BX LR, which jumps back to the saved return address.
  3. Step 3: Evaluate other options

    B does not save return address in LR; MOV PC, R0 returns to wrong place; BX R14 is equivalent to BX LR but using B (not BL) means LR is not saved.
  4. Final Answer:

    Use BL to call the function, then return with BX LR -> Option C
  5. Quick Check:

    BL + BX LR = correct call and return [OK]
Hint: Call with BL, return with BX LR always [OK]
Common Mistakes:
  • Using B instead of BL for calls
  • Returning with MOV PC, R0 instead of BX LR
  • Confusing BX R14 and BX LR usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard