Bird
Raised Fist0
ARM Architectureknowledge~10 mins

Stack pointer (R13/SP) in ARM Architecture - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Stack pointer (R13/SP)
Start Program
↓
Initialize SP (R13)
↓
Push Data
→SP = SP - 4
↓
Pop Data
→Load Data from SP
↓
End Program
The stack pointer (R13/SP) tracks the top of the stack. It moves down when pushing data and up when popping data.
Execution Sample
ARM Architecture
MOV R13, #0x1000  ; Initialize SP
PUSH {R0}          ; Push R0 onto stack
POP {R1}           ; Pop top of stack into R1
This code sets the stack pointer, pushes a register onto the stack, then pops it back into another register.
Analysis Table
StepInstructionSP (R13) ValueActionStack Memory State
1MOV R13, #0x10000x1000Initialize SP to 0x1000Empty
2PUSH {R0}0x0FFCSP decremented by 4; R0 stored at 0x0FFC0x0FFC: R0 value
3POP {R1}0x1000R1 loaded from 0x0FFC; SP incremented by 4Empty
💡 Program ends after POP; SP returns to initial 0x1000 indicating empty stack
State Tracker
VariableStartAfter Step 1After Step 2After Step 3
SP (R13)Undefined0x10000x0FFC0x1000
Stack MemoryEmptyEmpty0x0FFC: R0 valueEmpty
R0ValueXValueXStored on stackValueX
R1UndefinedUndefinedUndefinedValueX (popped)
Key Insights - 3 Insights
Why does the stack pointer (SP) decrease when pushing data?
Because the ARM stack grows downward in memory, pushing data means moving SP to a lower address before storing data, as shown in step 2 of the execution_table.
How does the POP instruction restore the stack pointer?
POP reads data from the current SP address, then increases SP to point above the popped data, effectively removing it from the stack, as seen in step 3.
What does it mean when SP returns to its initial value after popping?
It means the stack is empty again, no data remains pushed, as shown by SP returning to 0x1000 in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What is the SP value after the PUSH instruction?
A0x1004
B0x1000
C0x0FFC
DUndefined
💡 Hint
Check the 'SP (R13) Value' column at step 2 in the execution_table.
At which step does the stack pointer return to its initial value?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at the SP values in the execution_table; see when SP equals 0x1000 again.
If the PUSH instruction stored 8 bytes instead of 4, how would SP change at step 2?
ASP would decrease by 8
BSP would increase by 8
CSP would stay the same
DSP would decrease by 4
💡 Hint
Recall that pushing data decreases SP by the size of data stored, see step 2.
Concept Snapshot
Stack pointer (R13/SP) holds the address of the top of the stack.
Pushing data decreases SP (stack grows down).
Popping data increases SP (removes data).
SP must be initialized before use.
SP points to the last pushed data location.
Full Transcript
The stack pointer, also called R13 or SP in ARM architecture, tracks the top of the stack in memory. When the program starts, SP is set to a starting address, for example 0x1000. When data is pushed onto the stack, SP moves down by the size of the data (usually 4 bytes) before storing the data at the new SP address. When data is popped, the CPU reads from the current SP address and then moves SP back up by the same amount, effectively removing the data from the stack. This behavior is shown in the execution table where SP starts at 0x1000, moves to 0x0FFC after a push, and returns to 0x1000 after a pop, indicating the stack is empty again. Understanding this movement of SP is key to managing function calls, local variables, and interrupts in ARM programming.

Practice

(1/5)
1. What is the primary role of the stack pointer (R13/SP) in ARM processors?
easy
A. It tracks the top of the stack during program execution.
B. It stores the current instruction being executed.
C. It holds the address of the program's entry point.
D. It manages the processor's clock speed.

Solution

  1. Step 1: Understand the stack pointer's function

    The stack pointer (R13/SP) is a special register that points to the top of the stack in memory.
  2. Step 2: Identify its role in program execution

    It helps manage data pushed onto or popped from the stack, such as function calls and local variables.
  3. Final Answer:

    It tracks the top of the stack during program execution. -> Option A
  4. Quick Check:

    Stack pointer = top of stack tracker [OK]
Hint: Stack pointer always points to the current top of the stack [OK]
Common Mistakes:
  • Confusing stack pointer with program counter
  • Thinking it stores instructions
  • Assuming it controls processor speed
2. Which of the following is the register number for the stack pointer register in ARM assembly?
easy
A. R14
B. R13
C. SP
D. PC

Solution

  1. Step 1: Recall ARM register naming

    In ARM architecture, R13 is the register used as the stack pointer.
  2. Step 2: Match register names to functions

    R14 is the link register, PC is the program counter, and SP is an alias for R13.
  3. Final Answer:

    R13 -> Option B
  4. Quick Check:

    Stack pointer register = R13 [OK]
Hint: Remember R13 is SP, R14 is LR, PC is R15 [OK]
Common Mistakes:
  • Confusing R13 with R14 (link register)
  • Thinking SP is the register number
  • Confusing it with PC (R15)
3. Consider this ARM assembly snippet:
SUB SP, SP, #4
STR R0, [SP]
ADD SP, SP, #4

What does this code do to the stack pointer?
medium
A. Moves SP down by 4, stores R0, then moves SP back up by 4
B. Moves SP up by 4, stores R0, then moves SP down by 4
C. Stores R0 without changing SP
D. Causes a runtime error due to invalid SP manipulation

Solution

  1. Step 1: Analyze the SUB instruction

    SUB SP, SP, #4 decreases SP by 4, moving the stack pointer down to allocate space.
  2. Step 2: Understand STR and ADD instructions

    STR R0, [SP] stores the value of R0 at the new top of the stack. ADD SP, SP, #4 restores SP back up by 4.
  3. Final Answer:

    Moves SP down by 4, stores R0, then moves SP back up by 4 -> Option A
  4. Quick Check:

    SUB decreases SP, ADD restores SP [OK]
Hint: SUB decreases SP (push), ADD increases SP (pop) [OK]
Common Mistakes:
  • Mixing up SUB and ADD effects on SP
  • Assuming SP does not change
  • Thinking this causes an error
4. In ARM assembly, a programmer writes:
ADD SP, SP, #8
LDR R1, [SP, #-4]

What is the problem with this code?
medium
A. There is no problem; code works as intended.
B. The immediate value #8 is invalid for ADD instruction.
C. LDR cannot use negative offsets in ARM assembly.
D. SP is moved up before loading, causing incorrect address access.

Solution

  1. Step 1: Examine ADD SP, SP, #8

    This moves SP up by 8, effectively popping 8 bytes off the stack.
  2. Step 2: Analyze LDR with negative offset

    LDR R1, [SP, #-4] tries to load from SP minus 4 bytes, but since SP moved up, this address may not point to the intended data.
  3. Step 3: Identify the problem

    Moving SP up before loading causes the load to access an incorrect or invalid memory location.
  4. Final Answer:

    SP is moved up before loading, causing incorrect address access. -> Option D
  5. Quick Check:

    SP must point correctly before LDR [OK]
Hint: Adjust SP after loading, not before, to avoid wrong addresses [OK]
Common Mistakes:
  • Assuming negative offsets are invalid
  • Ignoring SP movement effect on address
  • Believing immediate #8 is illegal
5. You want to save registers R0 to R3 on the stack before a function call and restore them after. Which ARM assembly sequence correctly uses the stack pointer (R13/SP) to do this?
hard
A. SUB SP, SP, #4 STR R0, [SP] STR R1, [SP, #4] STR R2, [SP, #8] STR R3, [SP, #12] ...function call... LDMIA SP!, {R0-R3}
B. ADD SP, SP, #16 STR R0, [SP] STR R1, [SP, #4] STR R2, [SP, #8] STR R3, [SP, #12] ...function call... LDMFD SP!, {R0-R3}
C. SUB SP, SP, #16 STR R0, [SP] STR R1, [SP, #4] STR R2, [SP, #8] STR R3, [SP, #12] ...function call... LDMIA SP!, {R0-R3}
D. SUB SP, SP, #16 STR R0, [SP, #12] STR R1, [SP, #8] STR R2, [SP, #4] STR R3, [SP] ...function call... LDMIA SP!, {R0-R3}

Solution

  1. Step 1: Allocate stack space for 4 registers

    SUB SP, SP, #16 reserves 16 bytes (4 registers x 4 bytes each) by moving SP down.
  2. Step 2: Store registers in ascending order

    STR instructions store R0 at [SP], R1 at [SP+4], R2 at [SP+8], and R3 at [SP+12], matching stack layout.
  3. Step 3: Restore registers after function call

    LDMIA SP!, {R0-R3} loads multiple registers and increments SP, restoring original SP value.
  4. Final Answer:

    The sequence using SUB SP, SP, #16, ordered STRs, and LDMIA SP!, {R0-R3} -> Option C
  5. Quick Check:

    SUB allocates, STR stores, LDMIA restores [OK]
Hint: Subtract before storing, add after loading with LDMIA SP! [OK]
Common Mistakes:
  • Using ADD instead of SUB to allocate stack space
  • Incorrect offsets for storing registers
  • Reversing order of stored registers
  • Using wrong load multiple instruction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard