Bird
0
0

Given this ARM assembly snippet for a recursive sum function:

medium📝 Analysis Q4 of 15
ARM Architecture - Subroutines and Stack
Given this ARM assembly snippet for a recursive sum function:
sum:
    CMP R0, #0
    BEQ end
    PUSH {LR}
    SUB R0, R0, #1
    BL sum
    POP {LR}
    ADD R0, R0, R1
end:
    BX LR

What is the main issue with this code?
ALR is not saved before recursive call
BR1 is never initialized before use
CThe base case is incorrect
DStack pointer is not adjusted
Step-by-Step Solution
Solution:
  1. Step 1: Analyze register usage

    The code adds R0 and R1 after recursion, but R1 is never set.
  2. Step 2: Identify missing initialization

    Without initializing R1, the addition uses garbage value causing wrong result.
  3. Final Answer:

    R1 is never initialized before use -> Option B
  4. Quick Check:

    Uninitialized register use = R1 [OK]
Quick Trick: Always initialize registers before use in recursion [OK]
Common Mistakes:
  • Assuming base case is wrong
  • Ignoring LR saving which is done
  • Thinking stack pointer is not adjusted

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More ARM Architecture Quizzes