Bird
0
0

Identify the error in this recursive ARM assembly function snippet:

medium📝 Analysis Q14 of 15
ARM Architecture - Subroutines and Stack
Identify the error in this recursive ARM assembly function snippet:
factorial:
    CMP R0, #1
    BLE end_factorial
    SUB R0, R0, #1
    BL factorial
    MUL R0, R0, R1
end_factorial:
    MOV R1, R0
    BX LR

What is the main problem?
AMissing PUSH and POP of LR causing return address loss
BIncorrect base case comparison
CWrong multiplication order
DUsing MOV instead of MOVS
Step-by-Step Solution
Solution:
  1. Step 1: Check stack usage for recursion

    Recursive calls must save LR on stack before calling to preserve return address.
  2. Step 2: Identify missing PUSH/POP

    This code lacks PUSH {LR} before BL and POP {LR} after, risking corrupted return address.
  3. Final Answer:

    Missing PUSH and POP of LR causing return address loss -> Option A
  4. Quick Check:

    Save LR on stack in recursion [OK]
Quick Trick: Always save LR on stack before recursive call [OK]
Common Mistakes:
  • Ignoring stack save for LR
  • Assuming base case is wrong
  • Confusing MOV with MOVS

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More ARM Architecture Quizzes