1/215
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
ARM Procedure Call Standard
A set of guidelines for using registers when calling subroutines in ARM architecture.
Preserved Registers
Registers that must retain their values after a subroutine call.
Non-Preserved Registers
Registers that can be freely modified by a subroutine without needing to restore their original values.
r0
Argument 1 and return value; not preserved.
r1
Argument 2; not preserved.
r2
Argument 3; not preserved.
r3
Argument 4; not preserved.
r4
General-purpose V1; preserved.
r5
General-purpose V2; preserved.
r6
General-purpose V3; preserved.
r7
General-purpose V4; preserved.
r8
General-purpose V5; preserved.
r9
Platform specific/V6; usage is platform-dependent.
r10
General-purpose V7; preserved.
r11
General-purpose V8; preserved.
r12 (IP)
Intra-procedure-call register; not preserved.
r13 (SP)
Stack pointer; preserved, must remain the same after a subroutine.
r14 (LR)
Link register; not preserved.
r15 (PC)
Program counter; do not directly change.
Callee Saved Registers
Registers that the callee must restore if modified.
Caller Saved Registers
Registers that the caller must save if they need to retain their values.
SP
Stack pointer; must be the same after a subroutine has completed.
LR
Link register; does not need to contain the same value after a subroutine.
64 bits
If return has 64 bits, then r0:r1 hold it.
128 bits
If the return has 128 bits, r0-r3 hold it.
Leaf Function
A function that does not call any other function.
Non-Leaf Function
A function that calls another function.
Function Call Tree
All functions in a program forms a function call tree. Assume no recursive function call.
Assembly Code for Add2
Add2 ADD r0, r0, r1 ; r0 = a + b
BX LR ; return
C Function for Add2
int Add2(int a, int b)
{
return a + b;
}
Assembly Code Implementation
Add2 ADD r2, r0, r1 ; r2 = a+b
MOV r0, r2 ; just realize …
BX LR ; return
Assembly Code with r4
Add2 ADD r4, r0, r1 ; r4 = a+b
MOV r0, r4 ; r0 = r4
BX LR ; return
Preserved Register
If one insists to change the value of a preserved register, this is what needs to be done to make the function correct.
Calc Function
int Calc(int a, int b)
{
return Add2(a, b) + 10;
}
Return Address and Stack
Non-leaf function must use stack to save return address!
Function Return
Push the return address (in LR) on the entry, pop the return address to PC for function return.
Stack Usage
Calc PUSH {LR} ; save LR in stack
BL Add2 ; r0 = Add2(a, b)
ADD r0, #10 ; r0 = r0 + 10
POP {PC} ; restore LR
Leaf Function Return
Leaf function uses 'BX LR' for return.
Non-Leaf Function Return
Non-leaf function uses 'PUSH {LR}' and 'POP {PC}'.
Add3 Function
int Add3(int a, int b, int c)
{
return Add2(Add2(a, b), c);
}
Alternative Add3 Function
int Add3(int a, int b, int c)
{
int t1 = Add2(a, b);
int t2 = Add2(t1, c);
return t2;
}
Assembly Code for Add3
Add3 PUSH {LR} ; push return address
BL Add2 ; r0 = Add2(a, b)
MOV r1, r2 ; r2 = c
BL Add2 ; r0 = Add2(r0, c)
POP {PC} ; return
Non-Leaf Function: Preserved and Non-Preserved
Refers to functions that may or may not preserve the state of certain registers across function calls.
Assembly code
A low-level programming language that is closely related to machine code.
Add3
A function that takes three integer parameters and returns their sum using the Add2 function.
PUSH {LR}
An assembly instruction that saves the link register (LR) to the stack.
BL Add2
An assembly instruction that branches to the Add2 function and links the return address.
MOV r1, r2
An assembly instruction that moves the value in register r2 to register r1.
POP {PC}
An assembly instruction that pops the top value from the stack into the program counter (PC), effectively returning from a function.
Stack
A data structure that follows the Last In First Out (LIFO) principle, used for storing temporary data.
Non-Preserved Register
A register that may be modified during function calls and does not need to retain its value.
Add4
A function that takes four integer parameters and returns the sum of two Add2 function calls.
PUSH {r2}
An assembly instruction that saves the value of register r2 onto the stack.
POP {r1}
An assembly instruction that retrieves the top value from the stack into register r1.
ADD r0, r2, r0
An assembly instruction that adds the values in registers r2 and r0 and stores the result in r0.
What is different: t1 must be preserved
Indicates that the variable t1 needs to retain its value across a function call in the context of Add4.
What happens if those are not enough?!
A rhetorical question regarding the limitations of register availability for passing arguments.
sum6
A function that takes six integer arguments and returns their sum.
int32_t
A data type representing a 32-bit signed integer.
PUSH
An assembly instruction that adds values to the stack.
POP
An assembly instruction that removes values from the stack.
PC
Program Counter, a register that contains the address of the next instruction to execute.
MOVS
An assembly instruction that moves a value into a register and sets the condition flags.
ADDS
An assembly instruction that adds two values and sets the condition flags.
LDRD
An assembly instruction that loads two registers from memory.
BX
An assembly instruction that branches to the address in a register.
CALLING CONVENTION
A set of rules defining how functions receive parameters and return values.
extern
A keyword used in C to declare a function defined in another file.
global
A keyword used in assembly to declare a symbol that can be accessed from other files.
sum3
A function that takes three integer arguments and returns their sum.
data memory
Memory used to store data variables during program execution.
register
A small amount of storage available directly in the CPU for quick data access.
ADDS sp, sp, #8
An assembly instruction that adjusts the stack pointer to remove two values from the stack.
sum6(1, 2, 3, 4, 5, 6)
An example function call that passes six integer values to the sum6 function.