131 Week 15 - ARM Functions Flashcards

1
Q

Function

A

A stored subroutine that performs a specific task based on the parameters with which it is provided.
Branching to a label is not a function, specific rules need to be followed to implement a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Uses of functions

A
  • Complex operations can be performed by calling a procedure.
  • Make code easier to understand and manage.
  • Program elements that can easily be re-used.
  • Same procedure can be called many times within a program.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Function execution

A
  1. Caller stores arguments in registers or memory
  2. Function call: Caller transfers flow control to the callee
  3. Callee acquires/allocates memory for doing work
  4. Callee executes the function body
  5. Callee stores the result in “some” register
  6. Callee deallocates memory
  7. Function return: Callee returns control the caller
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

branch and link

A

syntax: bl ProcedureAddress
bl stores the address of the next instruction in register lr (link register) then jumps to procedure address.
To return after function execution has finished, store the address in lr to pc.
This can be done by ending a function with bx lr.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Calling conventions

A

Implementation of a function should follow a set of calling conventions to ensure interoperability.
By using conventions functions written by different programmers or compiled using different compilers can still interoperate.
Conventions also allow you to use library functions without corrupting state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Convention 1 - registers for procedure calls

A

r0-r3 are used as argument registers where parameters can be passed to the function.
r0 is used to return a value from the function.
lr is used as a return address register.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Convention 2 - preserving registers.

A

If the function modifies the values of registers r4-r11, the stack pointer (SP/r13) or return address (LR/r14) it must preserve them after it has finished.
Temporary register (r12), argument registers (r0-r3) and CPSR are not preserved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Convention 3 - Using the stack to preserver registers

A

Registers are conventionally preserved by pushing the registers to the stack [PUSH {R4, LR}] then popping them from the stack at the end of the function [POP {R4, PC}].
Using PUSH {LR} and POP {PC} allows LR to be preserved during function execution and then moved to PC after execution has finished. It removes the need for the bx lr line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Register spilling

A

Moving the contents of registers to main memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Procedure nesting

A

If procedures are nested we must always preserve lr to the stack to save the return address for each function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Stack

A

Stack grows downwards.
Stack pointer (SP) always points to the top of the stack. Because the stack grows downwards, when we push an item to the stack, the stack pointer address will decrease.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly