Link register (R14/LR) in ARM Architecture - Time & Space Complexity
When working with the link register (R14/LR) in ARM assembly, it's important to understand how its use affects the flow of a program.
We want to see how the number of instructions executed grows when using the link register for function calls and returns.
Analyze the time complexity of the following ARM code snippet using the link register.
BL function ; Branch with link to function
...
function:
; function body
BX LR ; Return using link register
This code calls a function using the link register to store the return address, then returns back to the caller.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Each function call uses the link register to jump and return.
- How many times: The number of calls depends on how many times the function is invoked.
Each function call adds a fixed number of instructions for branching and returning.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 instructions (10 calls and 10 returns) |
| 100 | About 200 instructions |
| 1000 | About 2000 instructions |
Pattern observation: The total instructions grow linearly with the number of function calls.
Time Complexity: O(n)
This means the time taken grows directly in proportion to how many times the function is called.
[X] Wrong: "Using the link register makes function calls take constant time regardless of how many calls happen."
[OK] Correct: Each call and return still uses instructions, so total time grows with the number of calls.
Understanding how the link register works helps you explain how function calls manage return addresses efficiently in ARM assembly.
"What if the function called itself recursively many times? How would that affect the time complexity related to the link register usage?"
Practice
Solution
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.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.Final Answer:
To store the return address during function calls -> Option DQuick Check:
Link register = return address storage [OK]
- Confusing LR with the program counter
- Thinking LR stores general data
- Mixing LR with stack pointer
Solution
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.Step 2: Understand other instructions
MOV moves data, ADD adds values, and STR stores data to memory; none save return addresses.Final Answer:
BL -> Option AQuick Check:
BL saves return address in LR [OK]
- Choosing MOV as it moves data
- Confusing STR with saving return address
- Thinking ADD saves addresses
BL function
...
function:
MOV R0, #5
BX LRWhat happens when the instruction
BX LR executes?Solution
Step 1: Understand what BL does
BL saves the return address (next instruction after BL) into LR and jumps to 'function'.Step 2: Analyze BX LR instruction
BX LR branches to the address stored in LR, which is the return address after BL.Final Answer:
The program jumps back to the instruction after BL -> Option AQuick Check:
BX LR returns to caller address [OK]
- Thinking BX LR jumps to function start
- Assuming program halts on BX LR
- Believing BX LR jumps to random address
BL function
...
function:
MOV R0, #10
MOV PC, R0Solution
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.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.Final Answer:
MOV PC, R0 does not return to caller correctly -> Option BQuick Check:
Return must use LR, not arbitrary register [OK]
- Thinking BL is invalid
- Assuming MOV R0, #10 is wrong
- Ignoring the need to use LR for return
Solution
Step 1: Understand function call with BL
BL saves the return address in LR and branches to the called function.Step 2: Understand correct return instruction
Returning is done by branching to LR using BX LR, which jumps back to the saved return address.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.Final Answer:
Use BL to call the function, then return with BX LR -> Option CQuick Check:
BL + BX LR = correct call and return [OK]
- Using B instead of BL for calls
- Returning with MOV PC, R0 instead of BX LR
- Confusing BX R14 and BX LR usage
Ready to prove it?
Pass the test and this pattern moves unknown
