COMP 520: Compilers!
Lecture 13: x86 Details!
1
Announcements + Logistics
2
x86 Basics Continued!
3
From Yesterday: Register File
rax, rcx, rdx, rbx, rsi, rdi
4
From Yesterday: Register File
rax, rcx, rdx, rbx, rsi, rdi
5
REX Prefix
6
REX Prefix
RAX, RCX, RDX, RBX, RSP, RBP, RSI, RDI
R8, R9, R10, R11, R12, R13, R14, R15
7
REX Prefix: [regB+regX*8+520],regW
8
Thus, prefix bytes control registers
9
Jump/Branch
jmp rax
jmp 4000 0096
Unconditional Jump (not very interesting)
Jump to some other location in executable code
10
Jump/Branch
11
Jump/Branch
12
CFLAGs
CF, PF, AF, ZF, SF, OF
Idea: When comparing two parameters, generate everything! Are they equal? Is a<b? Etc.
13
CFLAGs
14
Comparison of code
Code that you see
if( rax == 3 )
rcx = 4;
else
rcx = 5;
print( rcx )
Code that CPU sees
cmp rax,3
je IsEqual
mov rcx,5
jmp End
IsEqual: mov rcx,4
End: push rcx
call print
15
Stack pointer(s)
rsp, rbp
rsp= stack pointer
rbp= stack base pointer
16
Stack pointer(s)
rsp, rbp
rsp= stack pointer
rbp= stack base pointer
Used for: (1) parameters in a function call,�(2) temporary variables, (3) stack framing for more temp variables, and more!
17
Stack Pointer
18
Base Stack Pointer
19
Stack Growth
Push/Pop data on/off the stack.
Each “entry” is 8 bytes�in this example.
�For 32-bit,�it would be 4 bytes.
20
0 |
0 |
0 |
4 |
65535 |
RSP
RBP
THE STACK
Stack Growth
Shown in the stack on the right.
Unintuitively, higher positions�are at lower memory addresses.��E.g. the number “4”�is at rbp-8, not rbp+8,�or rsp+8, not rsp-8
21
0 |
0 |
0 |
4 |
65535 |
RSP
RBP
LOWER�ADDRESSES
Stack Growth
Shown in the stack on the right.
Unintuitively, higher positions�are at lower memory addresses.��E.g. the number “4”�is at rbp-8, not rbp+8,�or rsp+8, not rsp-8
22
0 |
0 |
0 |
4 |
65535 |
RSP
RBP
LOWER�ADDRESSES
Local Variables
23
Local Variables
24
Local Variables
Consider:
push 4
25
|
|
|
4 |
Some other var |
RIP
RSP
RSP
RBP
Local Variables
Consider:
mov [rbp-8],5
26
|
|
|
4 -> 5 |
Some other var |
RIP
RSP
RBP
Local Variables
Consider:
mov [rbp-8],5
27
|
|
|
5 |
Some other var |
RIP
RSP
RBP
Next use of the stack
a = 3; b = 5;
someMethod( a, b );
28
Stack pointer(s)
rsp, rbp
someMethod( int a, int b );
push dword[b]
push dword[a]
call someMethod
29
Stack pointer(s) – How to call a method
rsp, rbp
someMethod( a, b );
push dword[b]
push dword[a]
call someMethod
30
|
|
|
[b] = 5 |
Some other var |
RIP
RBP
RSP
RSP
Stack pointer(s) – How to call a method
rsp, rbp
someMethod( a, b );
push dword[b]
push dword[a]
call someMethod
31
|
|
[a] = 3 |
[b] = 5 |
Some other var |
RIP
RBP
RSP
RSP
Stack pointer(s) – How to call a method
rsp, rbp
someMethod( a, b );
push dword[b]
push dword[a]
call someMethod
…
32
|
Return Address |
[a] = 3 |
[b] = 5 |
Some other var |
RIP
RBP
RSP
RSP
Return Address
Stack pointer(s) – How to call a method
rsp, rbp
At the end of someMethod:
ret
“Take address at top of stack”�“Set RIP to be that address”�“Pop the top of the stack”
33
|
Return Address |
[a] = 3 |
[b] = 5 |
Some other var |
RBP
RSP
RIP
Stack pointer(s) – How to call a method
rsp, rbp
someMethod( a, b );
push dword[b]
push dword[a]
call someMethod
add rsp,16
34
|
Return Address |
[a] = 3 |
[b] = 5 |
Some other var |
RSP,RBP
RSP
RIP
What does a CALLED method look like?
35
Lastly, pop
pop rcx
36
Worksheet Question Q3
How to set-up a stackframe…
We know that we need:
37
Worksheet Question Q3
Teardown is just undoing our work…
38
Stack Space in x86
39
Back to Memory Organization
40
.text Segment
41
Static vs Dynamic memory
int[] p = new int[ someVariableSize ];
Doesn’t fit our notions of .bss nor .data! (Why?)�If we use the stack, then we consume� a ton of stack space.
42
Dynamic memory is in the heap
int[] p = new int[ someVariableSize ];
The heap is just a memory location.
Simplest heap possible:
43
Super-simple heap
44
Heap Ptr
Heap Base: 0x8000 0000
Heap End: 0x8FFF FFFF
Super-simple heap
45
Heap Ptr
I want 3072 bytes of data!� malloc(3072)
new char[3072]
Heap Base: 0x8000 0000
Heap End: 0x8FFF FFFF
Super-simple heap
46
Heap Ptr
I want 3072 bytes of data!� malloc(3072)
new char[3072]
Returned start of allocated memory
Heap Base: 0x8000 0000
Heap End: 0x8FFF FFFF
What does this look like?
Consider:
47
How is something returned?
48
How is something returned?
49
FASTCALL,�but assume push 8
Assumptions
50
What does this look like?
Consider:
51
push 8
call malloc
mov [a], rax
What does this look like?
Consider:
52
push 8
call malloc
mov [a], rax
mov [rax+0],3
What does this look like?
Consider:
53
push 8
call malloc
mov [a], rax
mov [rax+0],3
mov [rax+4],5
Field variables are offsets
54
x: From some base address, add +0
y: From some base address, add +4
So how did we figure out the alloc size of “A”?
55
So how did we figure out the alloc size of “A”?
56
Coming up next!
57
Review this content!
58