0
0
ARM Architectureknowledge~10 mins

Recursive function in assembly in ARM Architecture - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Recursive function in assembly
Call recursive function
Check base case?
YesReturn base value
No
Save current state (push registers)
Prepare arguments for recursive call
Call recursive function again
Receive return value
Restore state (pop registers)
Compute result
Return result
End
The recursive function calls itself after checking a base case, saving state before the call and restoring it after, then returns the computed result.
Execution Sample
ARM Architecture
factorial:
  CMP r0, #1
  BLE end_factorial
  PUSH {r0, lr}
  SUB r0, r0, #1
  BL factorial
  POP {r1, lr}
  MUL r0, r0, r1
end_factorial:
  BX lr
This ARM assembly code computes factorial recursively by calling itself with decremented argument until base case.
Analysis Table
StepInstructionRegister r0 (n)ActionStack (LR) StateReturn Value
1CMP r0, #13Check if n <= 1EmptyN/A
2BLE end_factorial3No branch, n > 1EmptyN/A
3PUSH {r0, lr}3Save return address and nLR pushedN/A
4SUB r0, r0, #12Decrement nLR pushedN/A
5BL factorial2Recursive call with n=2LR pushedN/A
6CMP r0, #12Check if n <= 1LR pushedN/A
7BLE end_factorial2No branch, n > 1LR pushedN/A
8PUSH {r0, lr}2Save return address and nLR pushed twiceN/A
9SUB r0, r0, #11Decrement nLR pushed twiceN/A
10BL factorial1Recursive call with n=1LR pushed twiceN/A
11CMP r0, #11Check if n <= 1LR pushed twiceN/A
12BLE end_factorial1Branch taken, base caseLR pushed twiceN/A
13BX lr1Return from base caseLR pushed twiceReturn 1
14POP {r1, lr}1Restore saved n and return addressLR pushed onceReturn 1
15MUL r0, r0, r12Multiply n * factorial(n-1)LR pushed onceReturn 2
16BX lr2Return from recursion levelLR pushed onceReturn 2
17POP {r1, lr}2Restore saved n and return addressEmptyReturn 2
18MUL r0, r0, r16Multiply n * factorial(n-1)EmptyReturn 6
19BX lr6Return from initial callEmptyReturn 6
20----Execution ends, factorial(3) = 6
💡 Execution stops after returning from the initial call with factorial(3) computed as 6.
State Tracker
VariableStartAfter Step 4After Step 9After Step 13After Step 15After Step 18Final
r0 (n)3211266
Stack LREmptyLR pushedLR pushed twiceLR pushed twiceLR pushed onceEmptyEmpty
Return ValueN/AN/AN/A1266
Key Insights - 3 Insights
Why do we push and pop the LR register during recursion?
Because each recursive call needs to remember where to return after finishing. Pushing LR saves the return address before the call (see steps 3 and 8), and popping restores it after the call (steps 14 and 17).
How does the function know when to stop calling itself?
It checks if the input n is less than or equal to 1 (step 1 and 11). If yes, it returns immediately without further recursion (step 12). This is the base case.
Why do we decrement r0 before the recursive call?
Because factorial(n) calls factorial(n-1). The SUB instruction (steps 4 and 9) prepares the argument for the next recursive call.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 9, what is the value of r0?
A3
B2
C1
D0
💡 Hint
Check the 'Register r0 (n)' column at step 9 in the execution_table.
At which step does the base case condition become true and recursion stops?
AStep 6
BStep 12
CStep 18
DStep 3
💡 Hint
Look for the step where BLE branch is taken indicating n <= 1.
If we did not push LR before the recursive call, what would happen?
AThe return address would be lost, causing incorrect return.
BThe base case would never be reached.
CThe function would return correctly.
DThe stack would overflow.
💡 Hint
Refer to key_moments about why LR is pushed and popped during recursion.
Concept Snapshot
Recursive function in ARM assembly:
- Check base case with CMP and branch
- Save LR (return address) with PUSH before recursive call
- Decrement argument (r0) before calling recursively
- After recursive call, restore LR with POP
- Compute result (e.g., multiply for factorial)
- Return with BX lr
Base case stops recursion to avoid infinite calls.
Full Transcript
This visual execution trace shows how a recursive function works in ARM assembly. The function first compares the input value in register r0 to 1 to check the base case. If the value is less or equal, it returns immediately. Otherwise, it saves the return address by pushing the LR register onto the stack. Then it decrements r0 by 1 and calls itself recursively. After the recursive call returns, it restores LR by popping from the stack, multiplies the returned value by the current n, and returns the result. The stack keeps track of return addresses for each recursive call. This process repeats until the base case is reached, then the calls return one by one, computing the factorial. The execution table tracks each step, register values, stack state, and return values to help visualize the recursion flow.