Functions

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/18

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

19 Terms

1
New cards

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

2
New cards

Is Branching to a Label a Function?

no, you should follow specific rules in order to implement a function

3
New cards

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

<ul><li><p>Defines the interfaces by which one software program communicates with another at the source code level&nbsp;</p></li></ul><p>Defines the interface only&nbsp;</p><ul><li><p>The user of it can ignore the implementation&nbsp;</p></li><li><p>Many implementations of the same one</p></li><li><p>C standard library hides many low-level details of the system</p></li></ul><p></p>
4
New cards

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)

5
New cards

Breaking Down Function Execution

  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

6
New cards

Instructions for Procedure Calls

knowt flashcard image
7
New cards

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

8
New cards

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)

9
New cards

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

<p>Assembly convention&nbsp;</p><ul><li><p>Registers must be restored after procedure call</p></li><li><p>If usage of these registers is avoided no spilling of registers on the stack is required</p></li></ul><p></p>
10
New cards

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

11
New cards

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

<ul><li><p><span>bl save pc to lr</span></p></li><li><p><span>We must always save the lr register, if a nested procedure is called</span></p></li><li><p><span>Lr can be saved in stack</span></p></li><li><p><span>If usage of these registers is avoided, no spilling of registers on the stack is required</span></p></li></ul><p></p>
12
New cards

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)

<p>Stack is a memory region used to store local state for your functions</p><p>It is a dynamic memory region</p><ul><li><p>The stack pointer (SP) starts at address 0x20020000</p></li><li><p>The current stack pointer bottom is pointed by register SP</p></li><li><p>A stack is like a Last In First Out (LIFO) Queue</p></li><li><p>You can use register sp to grow (decrease register sp) and shrink (increase register sp)</p></li></ul><p></p>
13
New cards

What is the Stack used to store?

  • Preserved registers

  • Local function variables

  • input arguments to the function

14
New cards

Memory Layout

<p></p>
15
New cards

The Stack

knowt flashcard image
16
New cards

Growing the Stack

knowt flashcard image
17
New cards

Instructions for Stack

knowt flashcard image
18
New cards

Simple Procedure in C

knowt flashcard image
19
New cards

Complex Calculations

must be broken down into smaller steps → compiler

<p>must be broken down into smaller steps → compiler</p>