Week 10
Functions in ARM Assembly – Function basics
COS10004 Computer Systems · Lecture 10.1 – Functions in ARM Assembly - Function basics
Chris McCarthy
FUNCTIONS
- Functions/methods/procedures/sub-routines:
- A callable block of organised, re-usable code
- Typically single action
- accepts arguments (ie parameters)
- Eg in C: int add(int x, inty) { int sum = x + y; return(sum); }
Function A
… Instruction pointer FuncB(int i)
… …
… return j
Y = FuncB(3)
… FuncB(int i) … Instruction pointer … … return j
Y = FuncB(3)
… FuncB(int i)
… …
… Instruction pointer return j
Y = FuncB(3)
… FuncB(int i)
… …
… return j
Y = FuncB(3) Instruction pointer
… Instruction pointer FuncB(int i)
… …
… return j
Y = FuncB(3)
… FuncB(int i) … Instruction pointer … … return j
Y = FuncB(3)
… FuncB(int i)
… …
… Instruction pointer return j
Y = FuncB(3)
… Instruction pointer FuncB(int i)
… …
… return j
Y = FuncB(3)
… FuncB(int i)
… …
… return j
Y = FuncB(3) Instruction pointer
FUNCTION BASICS
- When a function is called:
- Arguments need to be placed somewhere the function can access
- program control shifts to the function’s instructions
- When a function completes:
- Return value needs to placed somewhere for the calling function to retrieve
- after where it was called fromprogram control shifts back to the instruction immediately
- Managing this requires a some house keeping needed!
- High level programming languages hide most of this!
- Not ASM!
FUNCTIONS IN ASM
- Not 'native' to assembly
- We need to do a lot of the management ourselves
- Argument passing:
- How do we pass arguments from one function to another
- Storing and recalling register values
- each function we call will want to use the same registers (only 13 general purpose registers!)
- How do we manage this?
- Managing the program control
- Jumping from one function to another, and then returning back!
- Not 'native' to assembly
- We need to do a lot of the management ourselves
- Argument passing:
- How do we pass arguments from one function to another
- Storing and recalling register values
- each function we call will want to use the same registers (only 13 general purpose registers!)
- How do we manage this?
- Managing the program control
- Jumping from one function to another, and then returning back!
REGISTER MANAGEMENT
- Application Binary Interface (ABI) sets standard way of using ARM registers.
- r0-r3 used for function arguments and return values
- r4-r12 promised not to be altered by functions
- lr and sp used for stack management
- pc is the next instruction – we can use it to exit a function call
Register Brief Preserved ABI Rules
r0 Argument No r0 and r1 are used for passing the first two
and result arguments to functions, and returning the results Argument of functions. If a function does not use them for a
r1 and result No return value, they can take any value after a
function.
r2 Argument No r2 and r3 are used for passing the second two
arguments to functions. There values after a
r3 Argument No function is called can be anything. General r4 to r12 are used for working values, and their r4 - r12* purpose Yes value after a function is called must be the same
as before. Return lr is the address to branch back to when a
lr(r14) address No function is finished, but this does have to contain
the same address after the function has finished.
sp (r13) Stack Yes sp is the stack pointer. Its value must be the same
pointer after the function has finished. Program mov pc,lr causes a function to return.
pc (r15) Counter ? Alt: push {lr} at the start of the function,10/3/202210/3/2022 COS10004 Computer Systemspop {pc} COS10004 Computer Systemsat the end1616
CALLING FUNCTIONS
- By convention, the first two function arguments are loaded into r0 and r1.
- The next two are put into r2 and r3.
- The return value of the function is written into r0 and r1 (lowest word in r0).
- The function promises not to alter r4-r12.
- ... but suppose the function needs to use many registers to do calculations...
SUMMARY
- Functions are the building blocks of programs:
- Organised, re-usable blocks of code
- Higher level programming languages have built in support for functions:
- Not ASM!
- One thing we need to manage is register use
- Application Binary Interface (ABI) defines conventions for the use of registers
- Next lecture:
- How do we store and recall register values? With a stack of course!