Why subroutines enable modular assembly code in ARM Architecture - Performance Analysis
When using subroutines in ARM assembly, we want to understand how the program's running time changes as we reuse code blocks.
We ask: How does calling subroutines affect the total steps the processor takes?
Analyze the time complexity of this ARM assembly code using a subroutine.
MOV R0, #5
BL subroutine
MOV R1, R0
subroutine:
ADD R0, R0, #1
BX LR
This code calls a subroutine that adds 1 to a value and returns it.
Look for repeated instructions or calls.
- Primary operation: The subroutine call (BL) and return (BX LR).
- How many times: Once in this example, but could be many times if called repeatedly.
If the subroutine is called multiple times, the total steps grow with the number of calls.
| Input Size (calls) | Approx. Operations |
|---|---|
| 10 | About 10 subroutine calls plus main instructions |
| 100 | About 100 subroutine calls plus main instructions |
| 1000 | About 1000 subroutine calls plus main instructions |
Pattern observation: The total steps increase roughly in direct proportion to how many times the subroutine is called.
Time Complexity: O(n)
This means the total running time grows linearly with the number of subroutine calls.
[X] Wrong: "Calling a subroutine adds no extra time because it's just a jump."
[OK] Correct: Each call and return takes extra steps, so more calls mean more total time.
Understanding how subroutines affect execution time helps you write clear, reusable code while knowing its performance impact.
"What if the subroutine called itself recursively? How would that change the time complexity?"