Bird
0
0

You want to implement a recursive Fibonacci function in ARM assembly. Which approach correctly handles parameter passing and return values?

hard📝 Application Q9 of 15
ARM Architecture - Subroutines and Stack
You want to implement a recursive Fibonacci function in ARM assembly. Which approach correctly handles parameter passing and return values?
APass n in R7, no recursion needed, use loop instead
BPass n in R0, save LR, call recursively with n-1 and n-2, add results in R0
CUse global variables for n, no need to save LR, return sum in R1
DCall recursive function without saving LR, return value in SP
Step-by-Step Solution
Solution:
  1. Step 1: Understand parameter passing

    ARM passes first argument in R0, so n should be in R0 for recursion.
  2. Step 2: Manage recursion and return

    Save LR before recursive calls to preserve return address, call fib(n-1) and fib(n-2), then add results.
  3. Step 3: Return result in R0

    Return value conventionally in R0, so sum of fib(n-1) and fib(n-2) goes in R0.
  4. Final Answer:

    Pass n in R0, save LR, call recursively with n-1 and n-2, add results in R0 -> Option B
  5. Quick Check:

    Correct recursion = R0 param + LR save + R0 return [OK]
Quick Trick: Pass in R0, save LR, return result in R0 for recursion [OK]
Common Mistakes:
  • Using global variables instead of registers
  • Not saving LR causing return errors
  • Returning values in stack pointer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More ARM Architecture Quizzes