Slides 12 x86 Procedures
Slides 12 x86 Procedures
PROCEDURES
(FUNCTIONS)
CONTROL FLOW
Function
▸ A unit of code we can call
▸ Similar to a jump, except it can return
▸ Must support passing data as function arguments and return values
2
x86-64 STACK
3
PUSHING TO A STACK
Pushing
▸ pushq Src
▹ Fetch operand at Src
▹ Decrement %rsp by 8
▹ Write operand at address given by %rsp
4
POPPING FROM A STACK
Popping
▸ popq Dest
▹ Read operand at address given by %rsp
▹ Write to Dest
▹ Increment %rsp by 8
5
STACK OPERATION EXAMPLES
6
CONTROL FLOW TERMINOLOGY
7
CONTROL FLOW INSTRUCTIONS
Function call
▸ call label
▹ Push return address on stack
(that is, the address of next instruction after the call)
▹ Jump to label
Function return
▸ ret
▹ Pop return address from stack
▹ Jump to address
8
CONTROL FLOW EXAMPLE
9
CONTROL FLOW EXAMPLE
10
CONTROL FLOW EXAMPLE
11
CONTROL FLOW EXAMPLE
12
PRACTICE PROBLEM
call next
next: The “call” will cause the address of the
popq %rax “popq” instruction to be pushed onto the
stack.
What is the value of %rax? Then, the popq instruction will pop that
address (its own address) off, and store
What would this be useful for?
it into rax.
15
CALL CHAIN EXAMPLE
16
CALL CHAIN EXAMPLE
17
CALL CHAIN EXAMPLE
18
CALL CHAIN EXAMPLE
19
CALL CHAIN EXAMPLE
20
CALL CHAIN EXAMPLE
21
CALL CHAIN EXAMPLE
22
CALL CHAIN EXAMPLE
23
CALL CHAIN EXAMPLE
24
CALL CHAIN EXAMPLE
25
CALL CHAIN EXAMPLE
26
CALL CHAIN EXAMPLE
27
X86-64 / LINUX STACK FRAME
28
X86-64 / LINUX STACK FRAME
29
FUNCTION ARGUMENTS
%rdi
%rsi
%rdx Argument n
%rcx . . .
%r8 Argument 8
%r9 Argument 7
Given the C function above, identify function arguments being passed to foo:
0000000000000000 <call_foo>:
0: sub $0xA8,%rsp
7: mov 0x68(%rsp),%rax
a[9]
c: mov %rax,0x18(%rsp)
11: mov 0x60(%rsp),%rax
16: mov %rax,0x10(%rsp) a[8]
1b: mov 0x58(%rsp),%rax
20: mov %rax,0x8(%rsp) a[7]
25: mov 0x50(%rsp),%rax
2a: mov %rax,(%rsp) a[6]
2e: mov 0x48(%rsp),%r9 a[5]
33: mov 0x40(%rsp),%r8 a[4]
38: mov 0x38(%rsp),%rcx a[3]
3d: mov 0x30(%rsp),%rdx a[2]
42: mov 0x28(%rsp),%rsi a[1]
47: mov 0x20(%rsp),%rdi a[0]
4c: callq <foo>
51: add $0xA8,%rsp
58: retq 32
LOCAL VARIABLES
How are they preserved if the current function calls another function?
▸ Compiler updates %rsp beyond local variables before issuing “call”
33
LOCAL VARIABLES
call_foo() {
long a[16];
foo(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
}
0000000000000000 <call_foo>:
0: sub $0xA8,%rsp Makes room for both a[16] and partial
7: mov 0x68(%rsp),%rax argument build for foo.
c: mov %rax,0x18(%rsp)
11: mov 0x60(%rsp),%rax
16: mov %rax,0x10(%rsp)
1b: mov 0x58(%rsp),%rax
20: mov %rax,0x8(%rsp)
25: mov 0x50(%rsp),%rax
2a: mov %rax,(%rsp)
2e: mov 0x48(%rsp),%r9
33: mov 0x40(%rsp),%r8
38: mov 0x38(%rsp),%rcx
3d: mov 0x30(%rsp),%rdx
42: mov 0x28(%rsp),%rsi
47: mov 0x20(%rsp),%rdi
4c: callq <foo>
Puts the stack pointer back to where
51: add $0xA8,%rsp
it was at address 0x0.
58: retq 34
REGISTER SAVING CONVENTIONS
Conventions
▸ “Caller Save”
▹ Caller saves temporary in its frame before calling
▸ “Callee Save”
▹ Callee saves temporary in its frame before using
▹ Callee restores values before returning
42
X86-64 CALLER SAVED REGISTERS
▸ %rax
▹ Return value
▸ %r10, %r11
▹ Temporaries
43
X86-64 CALLEE SAVED REGISTERS
▸ %rbp
▹ May be used as frame pointer
▸ %rsp
▹ Special form of callee save
▹ Restored to original value upon
return from function
45
CALLEE SAVED EXAMPLE
46
CALLEE SAVED EXAMPLE
47
X86-64 FLOATING POINT ARGUMENTS
Floating point
▸ Special vectored registers to pass (AVX-512)
▸ %zmm0 - %zmm31
▹ Capacity for a vector of 8 doubles
▹ Also used for vectored integer operations (more later)
▹ Unique to x64
▹ Special instruction sets: MMX -> SSE -> AVX -> AVX-512
48
32-BIT CALLING CONVENTIONS
Win32 stdcall
▸ Caller pushes arguments on stack before call
▸ Callee clears arguments off stack before returning from call
▸ Saves some instructions since callee is already restoring the stack at the end of the
function
Fastcall
▸ Save memory operations by passing arguments in registers
▸ Microsoft implementation
▹ First two arguments passed in registers %ecx and %edx
▹ Code written on Windows must deal with stdcall and fastcall conventions
▸ Linux
▹ Must declare in function prototype which calling convention is being used
▹ http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
49
32-BIT CALLING CONVENTIONS
thiscall
▸ Used for C++
▸ Linux
▹ Same as cdecl, but first argument assumed to be “this” pointer
▸ Windows/Visual C++
▹ “this” pointer passed in %ecx
▹ Callee cleans the stack when arguments are not variable length
▹ Caller cleans the stack when arguments are variable length
50