Functions Overview
Notes
- Jorgensen chapter 12.8
- For functions to work, the programmers must agree on some rules.
- These are called calling conventions.
- This is not uniform across the computing world
- This is not uniform across an architecture.
- We will follow the System v AMD 64 ABI
- These are the rules for 64 bit x86 on linux
- Mostly
- Saved vs preserved registers
- Return values
- How to pass parameters
- Rules for the stack
- Return values are in the a register.
- We already have scratch vs preserved in theory
- But how do we save values?
- The stack.
- Activation Records
- Each function has an activation record
- This is also called a stack frame
- in x86 rbp marks the beginning of the activation record
- in X86 rsp marks the end of the activation record.
- The function can do mostly what it wants with an activation record.
- Including storing local data.
- Saving values on the stack
- For scratch registers registers
- Push the registers on the stack before a function call.
- Pop them off after the return.
- for preserved registers
- Push the register value on the stack before use
- Pop the value after register use but before return.
- For scratch registers registers
- We also have parameters
- rdi, rsi, rdx, rsx, r8, r9, push on the stack in reverse order
-
more than 8th arg 8th arg 7th arg end of calling activation record calling rip old rbp ← rbp (start of called activation record) Other local information ← rsp (end of called activation record)
- We know about the rsp %16 = 0 rule for stack alignment.
- One last bit, the red zone.
- This is 128 bytes reserved below the current AR for temp usage
-
more than 8th arg 8th arg 7th arg end of calling activation record calling rip old rbp ← rbp (start of called activation record) Other local information ← rsp (end of called activation record) RED ZONE ← rsp - 128
- We will discuss the activation record further in the future.