Parameter passing in registers in ARM Architecture - Time & Space Complexity
When a program calls a function, it often sends information called parameters. In ARM architecture, these parameters are passed using registers.
We want to understand how the time to pass parameters grows as the number of parameters increases.
Analyze the time complexity of the following ARM code snippet that passes parameters in registers.
MOV R0, param1
MOV R1, param2
MOV R2, param3
BL function_call
This code moves up to four parameters into registers R0 to R3 before calling a function.
Look for repeated steps that take time.
- Primary operation: Moving each parameter into a register (MOV instructions).
- How many times: Once per parameter, up to four times.
As the number of parameters increases, the number of MOV instructions grows linearly, but only up to four parameters.
| Input Size (parameters) | Approx. Operations (MOV instructions) |
|---|---|
| 1 | 1 |
| 3 | 3 |
| 4 | 4 |
| 5 | 4 (only first 4 in registers) |
Pattern observation: The number of operations grows linearly with parameters but stops at four because only four registers are used.
Time Complexity: O(1)
This means the time to pass parameters in registers does not grow beyond a fixed small number, even if more parameters exist.
[X] Wrong: "Passing more parameters always takes more time because each parameter adds a new instruction."
[OK] Correct: Only the first four parameters use registers, so after four, extra parameters are passed differently and do not add register moves.
Understanding how parameters are passed in registers helps you explain efficient function calls and low-level performance, a useful skill in systems programming and embedded development.
What if the architecture allowed passing eight parameters in registers instead of four? How would the time complexity change?
Practice
Solution
Step 1: Understand ARM calling convention basics
ARM uses registers r0 to r3 to pass the first four parameters to functions for speed.Step 2: Identify correct register range
Registers r0, r1, r2, and r3 are used, not the higher registers.Final Answer:
r0 to r3 -> Option CQuick Check:
First four parameters = r0 to r3 [OK]
- Confusing r4-r7 as parameter registers
- Thinking all parameters fit in registers
- Assuming stack is always used for parameters
Solution
Step 1: Recall register parameter limits
Only the first four parameters fit in registers r0 to r3.Step 2: Understand handling of extra parameters
Any parameter beyond the fourth is passed on the stack.Final Answer:
First four in r0-r3, fifth on stack -> Option AQuick Check:
5th parameter goes on stack [OK]
- Assuming registers can hold more than 4 parameters
- Passing all parameters on stack unnecessarily
- Misplacing parameters in wrong registers
Solution
Step 1: Identify register usage for parameters
ARM passes first four parameters in r0 to r3 registers.Step 2: Determine location for extra parameters
Parameters beyond the fourth (5th and 6th) are passed on the stack.Final Answer:
r0, r1, r2, r3 hold first four; stack holds 5th and 6th -> Option BQuick Check:
Extra params after 4th go to stack [OK]
- Assuming registers beyond r3 hold parameters
- Ignoring stack usage for extra parameters
- Mixing register order
Solution
Step 1: Recall ARM parameter passing rules
Only the first four parameters are passed in registers r0 to r3.Step 2: Identify cause of incorrect values
If last three parameters are not placed on the stack properly, their values will be incorrect inside the function.Final Answer:
Only first four parameters are passed in registers; last three must be on stack but were not -> Option DQuick Check:
Extra params must be on stack [OK]
- Assuming all params fit in registers
- Using wrong registers for extra params
- Ignoring stack for extra parameters
Solution
Step 1: Understand ARM calling convention for parameters
ARM passes up to four parameters in registers r0 to r3 for speed.Step 2: Arrange remaining parameters
Parameters beyond the fourth must be passed on the stack as per convention.Step 3: Apply to eight parameters
First four go in r0-r3; last four go on the stack to maximize speed and follow rules.Final Answer:
Pass first four parameters in r0-r3 registers; place remaining four on the stack -> Option AQuick Check:
4 in registers + rest on stack = fastest [OK]
- Trying to pass more than 4 params in registers
- Passing all params on stack unnecessarily
- Ignoring ARM calling convention rules
Ready to prove it?
Pass the test and this pattern moves unknown
