Load and store instructions (LDR, STR) in ARM Architecture - Time & Space Complexity
When working with ARM assembly, load (LDR) and store (STR) instructions move data between memory and registers.
We want to understand how the time to execute these instructions changes as we perform more of them.
Analyze the time complexity of the following ARM code snippet.
MOV R0, #0 ; Initialize counter
LOOP:
LDR R1, [R2, R0] ; Load from memory at address R2 + R0
STR R1, [R3, R0] ; Store to memory at address R3 + R0
ADD R0, R0, #4 ; Move to next word (4 bytes)
CMP R0, R4 ; Compare counter with size
BLT LOOP ; Repeat if less
This code copies a block of memory word by word from one location to another.
Look for repeated instructions inside loops or recursion.
- Primary operation: The loop repeats the load (LDR) and store (STR) instructions.
- How many times: The loop runs once for each word in the block, controlled by the counter compared to the size.
As the number of words to copy increases, the loop runs more times.
| Input Size (n words) | Approx. Operations (load + store per word) |
|---|---|
| 10 | 20 |
| 100 | 200 |
| 1000 | 2000 |
Pattern observation: The total operations grow directly in proportion to the number of words copied.
Time Complexity: O(n)
This means the time to complete the copy grows linearly with the number of words to load and store.
[X] Wrong: "Load and store instructions take constant time regardless of how many times they run."
[OK] Correct: Each load and store instruction takes time, so running them many times adds up, making total time grow with input size.
Understanding how repeated memory operations scale helps you reason about performance in low-level programming and embedded systems.
"What if the loop copied two words per iteration instead of one? How would the time complexity change?"
Practice
LDR instruction do in ARM architecture?Solution
Step 1: Understand the purpose of LDR
The LDR instruction is used to read or load data from a memory address into a CPU register.Step 2: Differentiate from STR
STR is the opposite; it stores data from a register back into memory.Final Answer:
Loads data from memory into a register -> Option BQuick Check:
LDR = Load from memory [OK]
- Confusing LDR with STR
- Thinking LDR performs arithmetic
- Assuming LDR changes program flow
Solution
Step 1: Identify the store instruction
STR is used to store data from a register into memory.Step 2: Match registers to syntax
STR R1, [R0] means store the value in R1 into the memory address held in R0.Final Answer:
STR R1, [R0] -> Option CQuick Check:
STR stores register to memory [OK]
- Swapping register and address in syntax
- Using LDR instead of STR for storing
- Confusing source and destination registers
STR R2, [R3] LDR R4, [R3]
What will be the value in register R4 after execution if R2 = 10 and the memory at address in R3 initially contains 5?
Solution
Step 1: Execute STR instruction
STR R2, [R3] stores the value 10 (from R2) into the memory address pointed by R3, replacing the initial 5.Step 2: Execute LDR instruction
LDR R4, [R3] loads the updated memory value (now 10) into R4.Final Answer:
10 -> Option AQuick Check:
Store then load updates R4 to 10 [OK]
- Assuming memory value stays 5 after STR
- Confusing source and destination registers
- Ignoring instruction order
LDR R1, R2 STR R3, [R4]
Solution
Step 1: Check LDR syntax
LDR loads from memory address, so the source must be in brackets like [R2]. Here, R2 is missing brackets, so syntax is wrong.Step 2: Check STR syntax
STR R3, [R4] is correct because it stores R3's value into memory at address in R4.Final Answer:
LDR syntax is incorrect; missing brackets around R2 -> Option AQuick Check:
LDR needs brackets for memory address [OK]
- Omitting brackets for memory operands
- Confusing register direct with memory access
- Thinking STR needs brackets on source register
Solution
Step 1: Understand block copy requirement
Copying 4 words means loading each word from source and storing it to destination, advancing addresses each time.Step 2: Analyze Use a loop: LDR R0, [R5], #4; STR R0, [R6], #4 repeated 4 times
Use a loop: LDR R0, [R5], #4; STR R0, [R6], #4 repeated 4 times uses LDR and STR with post-increment addressing ([R5], #4 and [R6], #4) inside a loop repeated 4 times, correctly copying each word and moving pointers.Step 3: Analyze other options
Use LDR R0, [R5]; STR R0, [R6] once only copies only one word. Use STR R0, [R5]; LDR R0, [R6] repeated 4 times reverses load/store roles. Use LDR R0, [R5, #4]; STR R0, [R6, #4] once copies only one word with offset but no loop.Final Answer:
Use a loop: LDR R0, [R5], #4; STR R0, [R6], #4 repeated 4 times -> Option DQuick Check:
Loop with LDR/STR and address increment copies block [OK]
- Copying only one word instead of all 4
- Mixing load and store instructions
- Not incrementing addresses after each word
Ready to prove it?
Pass the test and this pattern moves unknown
