1/18
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Functions
A stored subroutine that performs a specific task based on the parameters with which it is provided
Complex operations can be performed by calling a procedure
Make code easier to understand and manage
Program elements that can easily be reused
Same procedure can be called many times within a program
Is Branching to a Label a Function?
no, you should follow specific rules in order to implement a function
Application Programming Interface (API)
Defines the interfaces by which one software program communicates with another at the source code level
Defines the interface only
The user of it can ignore the implementation
Many implementations of the same one
C standard library hides many low-level details of the system
Functions as Detectives
Assigned a secret mission (function call)
Acquires necessary resources (acquire parameters and memory - stack)
Perform the mission (execute instructions)
Leaves no trace (clean up memory)
Returns safely to the point of origin (function return)
Breaking Down Function Execution
Caller stores arguments in registers or memory
Function call: Caller transfers flow control to the callee
Callee acquires/allocates memory for doing work
Callee executes the function body
Callee stores the result in “some” register
Callee deallocates memory
Function return: Callee returns control the caller
Instructions for Procedure Calls
Calling Convention
A function implementation should follow these to ensure interoperability
Many times, they make your code inefficient
Leave no trace
With one in place:
Functions written by different programmers can interoperate
Functions compiled by two different compilers can interoperate
A library function by a third party can be used without corrupting state
Convention 1 - Registers for Procedure Calls
r0-r3: “argument” registers in which to pass parameters
r0: return value registers
lr: return address register (link register)
Convention 2 - Preserving Registers
Assembly convention
Registers must be restored after procedure call
If usage of these registers is avoided no spilling of registers on the stack is required
Convention 3 - Preserving Registers
Sometimes a procedure needs to use more registers than just four arguments and two return values
Register content must be preserved during a procedure call
Moving the contents of registers to the main memory is called spilling registers
Registers are stored to memory using a conceptual data structure known as a stack
The stack pointer register SP points to the contents of the register most recently pushed onto the stack
Procedure Nesting
bl save pc to lr
We must always save the lr register, if a nested procedure is called
Lr can be saved in stack
If usage of these registers is avoided, no spilling of registers on the stack is required
Stack Memory
Stack is a memory region used to store local state for your functions
It is a dynamic memory region
The stack pointer (SP) starts at address 0x20020000
The current stack pointer bottom is pointed by register SP
A stack is like a Last In First Out (LIFO) Queue
You can use register sp to grow (decrease register sp) and shrink (increase register sp)
What is the Stack used to store?
Preserved registers
Local function variables
input arguments to the function
Memory Layout
The Stack
Growing the Stack
Instructions for Stack
Simple Procedure in C
Complex Calculations
must be broken down into smaller steps → compiler