0
0
ARM Architectureknowledge~10 mins

Parameter passing in registers in ARM Architecture - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parameter passing in registers
Function Call
Place Arguments in Registers
Jump to Function
Function Uses Registers to Access Parameters
Function Executes
Return Value in Register
Caller Continues
When a function is called, its input values (parameters) are placed into specific CPU registers. The function then reads these registers to get its inputs and returns the result in a register.
Execution Sample
ARM Architecture
int add(int a, int b) {
  return a + b;
}

// Caller:
int result = add(3, 5);
This code calls a function 'add' with two numbers, which are passed via registers, and returns their sum in a register.
Analysis Table
StepActionRegister State (r0, r1, r2...)Description
1Caller prepares argumentsr0=3, r1=5First argument 3 in r0, second argument 5 in r1
2Call function 'add'r0=3, r1=5Jump to function with arguments in registers
3Function reads parametersr0=3, r1=5Function accesses a=3 from r0, b=5 from r1
4Function computes sumr0=3, r1=5Calculate 3 + 5 = 8
5Function places result in r0r0=8, r1=5Return value 8 stored in r0
6Return to callerr0=8, r1=5Caller receives result in r0
7Caller stores resultr0=8Result 8 saved in variable 'result'
8Endr0=8Function call complete
💡 Function returns after placing result in r0, caller continues execution
State Tracker
Variable/RegisterStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
r0 (arg1/result)undefined3333888
r1 (arg2)undefined5555555
result (caller variable)undefinedundefinedundefinedundefinedundefinedundefinedundefined8
Key Insights - 3 Insights
Why are the first two arguments placed in registers r0 and r1?
In ARM calling conventions, the first four arguments are passed in registers r0 to r3 to speed up function calls by avoiding memory access. This is shown in execution_table rows 1 and 2.
How does the function return the result to the caller?
The function places the return value in register r0 before returning, as seen in execution_table row 5. The caller then reads r0 to get the result.
What happens if there are more than four parameters?
Only the first four parameters go into registers r0 to r3; additional parameters are passed on the stack (not shown here). This example shows just two parameters in registers.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What values do registers r0 and r1 hold?
Ar0=0, r1=0
Br0=8, r1=5
Cr0=3, r1=5
Dr0=5, r1=3
💡 Hint
Check the 'Register State' column at Step 3 in the execution_table.
At which step does the function place the result into register r0?
AStep 2
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the step where 'Function places result in r0' in the execution_table.
If the caller passed 10 and 20 instead of 3 and 5, what would r0 be after Step 5?
A30
B15
C10
D20
💡 Hint
Recall that the function adds the two arguments and returns the sum in r0.
Concept Snapshot
Parameter passing in registers:
- First 4 function arguments go into registers r0 to r3.
- Function reads parameters directly from these registers.
- Return value is placed in r0.
- This speeds up calls by avoiding memory.
- Extra arguments use the stack.
Full Transcript
When a function is called in ARM architecture, the first four parameters are placed into registers r0 through r3. The function accesses these registers to read its input values. After computing, the function places the return value in register r0. The caller then reads r0 to get the result. This method avoids slower memory access and makes function calls faster. If there are more than four parameters, the extra ones are passed on the stack, but this example shows only two parameters passed in registers.