Bird
0
0

What will be the output in R0 after calling this recursive ARM function with R0=3?

medium📝 Analysis Q5 of 15
ARM Architecture - Subroutines and Stack
What will be the output in R0 after calling this recursive ARM function with R0=3?
func:
    CMP R0, #1
    BLE end
    PUSH {LR}
    SUB R0, R0, #1
    BL func
    POP {LR}
    ADD R0, R0, #2
end:
    BX LR
A5
B1
C3
D7
Step-by-Step Solution
Solution:
  1. Step 1: Trace recursion calls

    func(3) calls func(2), which calls func(1). func(1) returns immediately.
  2. Step 2: Calculate return values

    func(1) returns 1; func(2) adds 2 to 1 = 3; func(3) adds 2 to 3 = 5.
  3. Step 3: Re-examine base case

    Since CMP R0, #1 and BLE end, func(1) returns R0=1 without addition.
  4. Final Answer:

    5 -> Option A
  5. Quick Check:

    Recursive addition accumulates to 5 [OK]
Quick Trick: Add after recursive call accumulates result [OK]
Common Mistakes:
  • Stopping at base case value without additions
  • Forgetting to add after recursion
  • Miscounting recursive calls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More ARM Architecture Quizzes