Stack operations (PUSH, POP) in ARM Architecture - Time & Space Complexity
We want to understand how the time to add or remove items from a stack changes as the stack grows.
How does the time needed for PUSH and POP operations change with the number of items?
Analyze the time complexity of the following ARM assembly code for PUSH and POP.
PUSH {r0} // Save register r0 on the stack
POP {r0} // Restore register r0 from the stack
This code saves a register value onto the stack and then restores it back.
In this example, each PUSH or POP is a single instruction without loops or recursion.
- Primary operation: Single memory store (PUSH) or load (POP)
- How many times: Once per operation
Each PUSH or POP takes the same amount of time no matter how many items are already on the stack.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: Time stays constant regardless of stack size.
Time Complexity: O(1)
This means each PUSH or POP operation takes the same fixed time no matter how big the stack is.
[X] Wrong: "PUSH and POP take longer as the stack grows because more items are involved."
[OK] Correct: Each operation only touches the top item, so the stack size does not affect the time.
Knowing that stack operations run in constant time helps you explain efficient memory management and function calls in real programs.
"What if the stack was implemented using a linked list instead of memory addresses? How would the time complexity change?"
Practice
PUSH instruction do in ARM architecture stack operations?Solution
Step 1: Understand the role of PUSH
PUSH adds data to the stack, which means it stores registers by moving the stack pointer downwards.Step 2: Compare with POP
POP removes data by moving the stack pointer upwards, opposite to PUSH.Final Answer:
It adds registers to the stack by decreasing the stack pointer. -> Option AQuick Check:
PUSH = add to stack [OK]
- Confusing PUSH with POP
- Thinking PUSH increases stack pointer
- Assuming PUSH clears the stack
Solution
Step 1: Recall ARM PUSH syntax
ARM uses curly braces to list registers for PUSH, likePUSH {R0, R1}.Step 2: Identify incorrect syntax
Parentheses, spaces without braces, or square brackets are not valid for PUSH in ARM.Final Answer:
PUSH {R0, R1} -> Option CQuick Check:
Curly braces list registers in PUSH [OK]
- Using parentheses instead of braces
- Listing registers without any brackets
- Using square brackets which are invalid
SUB SP, SP, #8
PUSH {R4, R5}
POP {R6, R7}What happens to the stack pointer (SP) after these operations?
Solution
Step 1: Analyze SUB SP, SP, #8
This subtracts 8 from SP, moving it down by 8 bytes.Step 2: Understand PUSH {R4, R5}
PUSH stores two registers, so SP decreases by another 8 bytes (4 bytes each register).Step 3: Understand POP {R6, R7}
POP removes two registers, so SP increases by 8 bytes.Final Answer:
SP decreases by 8, then decreases by 8, then increases by 8 -> Option AQuick Check:
SP moves down for SUB and PUSH, up for POP [OK]
- Forgetting SUB changes SP before PUSH
- Assuming POP decreases SP
- Miscounting bytes per register
PUSH R0, R1
POP {R2, R3}Solution
Step 1: Check PUSH syntax
PUSH requires registers to be enclosed in curly braces, soPUSH R0, R1is invalid.Step 2: Check POP syntax
POP syntax with curly braces is correct, soPOP {R2, R3}is valid.Final Answer:
PUSH syntax is incorrect; registers must be in braces. -> Option BQuick Check:
PUSH needs braces { } [OK]
- Using commas without braces in PUSH
- Confusing parentheses with braces
- Thinking POP cannot pop multiple registers
Solution
Step 1: Understand stack order with PUSH
PUSH stores registers in order: R0, then R1, then R2, with R2 at the lowest address.Step 2: POP must restore in reverse order
POP restores registers in reverse order of PUSH to correctly restore the original values.Step 3: Check options
PUSH {R0, R1, R2}
...
POP {R2, R1, R0} pops in reverse order as pushed, preserving correct values.Final Answer:
PUSH {R0, R1, R2} ... POP {R2, R1, R0} -> Option DQuick Check:
POP order is reverse of PUSH order to restore correctly [OK]
- Reversing POP order incorrectly
- Changing register order between PUSH and POP
- Assuming POP order does not matter
Ready to prove it?
Pass the test and this pattern moves unknown
