Bird
0
0

What is wrong with this recursive ARM function that calculates factorial?

medium📝 Analysis Q7 of 15
ARM Architecture - Subroutines and Stack
What is wrong with this recursive ARM function that calculates factorial?
fact:
    CMP R0, #1
    BLE end
    PUSH {LR}
    SUB R0, R0, #1
    BL fact
    MUL R0, R0, R0
    POP {LR}
end:
    BX LR
AStack pointer is not adjusted
BBase case should be zero, not one
CLR is not saved before call
DMultiplying R0 by itself instead of original value
Step-by-Step Solution
Solution:
  1. Step 1: Analyze multiplication step

    After recursion, it multiplies R0 by R0, which squares the result incorrectly.
  2. Step 2: Identify correct multiplication

    It should multiply the returned factorial by the original R0 before decrement.
  3. Final Answer:

    Multiplying R0 by itself instead of original value -> Option D
  4. Quick Check:

    Factorial multiply needs original input, not squared result [OK]
Quick Trick: Save original input before recursion for correct multiply [OK]
Common Mistakes:
  • Assuming base case is wrong
  • Ignoring LR saving which is done
  • Confusing stack pointer adjustments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More ARM Architecture Quizzes