Procedure Call Registers - Bowdoin Collegesbarker/teaching/courses/systems/17spri… · %rsp+8...
Transcript of Procedure Call Registers - Bowdoin Collegesbarker/teaching/courses/systems/17spri… · %rsp+8...
Sean Barker
Procedure Call Registers
1
%rax %eax
%rbx %ebx
%rdx %edx
%rcx %ecx
%rsi %esi
%rdi %edi
%rbp %ebp
%rsp %esp
%r8 %r8d
%r9 %r9d
%r11 %r11d
%r10 %r10d
%r12 %r12d
%r13 %r13d
%r15 %r15d
%r14 %r14d
Return
Arg 4
Arg 3
Arg 2
Arg 1
Stack ptr
Arg 5
Arg 6
Sean Barker
Call Stack
2
StackPointer:%rsp
StackGrows Down
Increasing Addresses
Stack“Top”
Stack“Bo;om”
Sean Barker
Stack Operations
3
-8
StackGrows Down
Increasing Addresses
Stack“Bo8om”
StackPointer:%rsp
Stack“Top”
StackPointer:%rsp
StackGrows Down
Increasing Addresses
Stack“Top”
Stack“Bo;om”
+8
popq Dest pushq Src
Sean Barker
Procedure Call Example (1)
4
0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq
0000000000400540 <foo>: • • 400544: callq 400550 <bar> 400549: mov %rax,(%rbx) • •
0x400544
0x120
•••
%rsp
0x120
0x128
0x130
%rip
Sean Barker
Procedure Call Example (2)
5
0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq
0000000000400540 <foo>: • • 400544: callq 400550 <bar> 400549: mov %rax,(%rbx) • •
0x400550
0x118
0x400549
•••
%rsp
0x120
0x128
0x130
0x118
%rip
Sean Barker
Procedure Call Example (3)
6
0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq
0000000000400540 <foo>: • • 400544: callq 400550 <bar> 400549: mov %rax,(%rbx) • •
0x400557
0x118
0x400549
•••
%rsp
0x120
0x128
0x130
0x118
%rip
Sean Barker
Procedure Call Example (4)
7
0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq
0000000000400540 <foo>: • • 400544: callq 400550 <bar> 400549: mov %rax,(%rbx) • •
0x400549
0x120
•••
%rsp
0x120
0x128
0x130
%rip
Sean Barker
Passing Data
8
Registers¢ First6arguments
¢ Returnvalue
Stack
%rdi
%rsi
%rdx
%rcx
%r8
%r9
%rax
Arg7
• • •
Arg8
Argn
• • •
Sean Barker
Stack Frames
9
FramePointer:%rbp
StackPointer:%rsp
Stack“Top”
PreviousFrame
Frameforproc
(Op:onal) x
Sean Barker
Call Chain Example
10
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
yoo
who
amI
amI
amI
Example CallChain
amI
ProcedureamI()isrecursive
Sean Barker
Stack Frame Allocation (1)
11
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo yoo(…) { • • who(); • • }
Sean Barker
Stack Frame Allocation (2)
12
yoo(…) { • • who(); • • }
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
who(…) { • • • amI(); • • • amI(); • • • }
Sean Barker
Stack Frame Allocation (3)
13
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
amI
amI(…) { • • amI(); • • }
Sean Barker
Stack Frame Allocation (4)
14
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
amI
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
Sean Barker
Stack Frame Allocation (5)
15
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
amI
amI
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
Sean Barker
Stack Frame Allocation (6)
16
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
amI
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
amI(…) { • • amI(); • • }
Sean Barker
Stack Frame Allocation (7)
17
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
Sean Barker
Stack Frame Allocation (8)
18
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
Sean Barker
Stack Frame Allocation (9)
19
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
amI
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
amI(…) { • • amI(); • • }
Sean Barker
Stack Frame Allocation (10)
20
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo
who
yoo(…) { • • who(); • • }
who(…) { • • • amI(); • • • amI(); • • • }
Sean Barker
Stack Frame Allocation (11)
21
yoo
who
amI
amI
amI
amI
yoo
%rbp
%rsp
Stack
yoo yoo(…) { • • who(); • • }
Sean Barker
Stack Frame Components
22
ReturnAddr
SavedRegisters
+Local
Variables
ArgumentBuild
(Op<onal)
Old%rbp
Arguments7+
Caller Frame
Framepointer%rbp
Stackpointer %rsp
(Op<onal)
Sean Barker
Stack Example: incr
23
long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x; }
incr: movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret
Register Use(s)
%rdi Argumentp
%rsi Argumentval,y
%rax x,Returnvalue
Sean Barker
Using the Stack (1)
24
call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret
long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; }
%rsp
Ini$alStackStructure
...
Rtnaddress
15213 Unused %rsp
Resul$ngStackStructure
...
Rtnaddress
%rsp+8
Sean Barker
Using the Stack (2)
25
call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret
long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; }
15213 Unused %rsp
StackStructure
...
Rtnaddress
%rsp+8
Register Use(s)
%rdi &v1
%rsi 3000
Sean Barker
Using the Stack (3)
26
call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret
long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; }
18213
Unused %rsp
StackStructure
...
Rtnaddress
%rsp+8
Register Use(s)
%rdi &v1
%rsi 3000
incr: movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret
Sean Barker
Using the Stack (4)
27
call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret
long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; } 18213
Unused %rsp
StackStructure
...
Rtnaddress
%rsp+8
Register Use(s)
%rax Returnvalue
%rsp
UpdatedStackStructure
...
Rtnaddress
Sean Barker
Using the Stack (5)
28
call_incr: subq $16, %rsp movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax addq $16, %rsp ret
long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; }
Register Use(s)
%rax Returnvalue
%rsp
UpdatedStackStructure
...
Rtnaddress
%rsp
FinalStackStructure
...
Sean Barker
Stack Frame Components
29
ReturnAddr
SavedRegisters
+Local
Variables
ArgumentBuild
(Op<onal)
Old%rbp
Arguments7+
Caller Frame
Framepointer%rbp
Stackpointer %rsp
(Op<onal)
Sean Barker
Register Conventions
30
%rax
%rdx %rcx
Returnvalue(caller-saved)
%r8 %r9 %r10 %r11
%rdi %rsi
Arguments(caller-saved)
Caller-savedtemporaries
%rbx
%rsp
Callee-saved Temporaries
Special%rbp
%r12 %r13 %r14 %r15
Sean Barker
Callee-Saved Example (1)
31
call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret
long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2; } %rsp
Ini$alStackStructure
...
Rtnaddress
15213 Unused %rsp
Resul$ngStackStructure
...
Rtnaddress
%rsp+8 Saved%rbx
Sean Barker
Callee-Saved Example (2)
32
call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi call incr addq %rbx, %rax addq $16, %rsp popq %rbx ret
long call_incr2(long x) { long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2; }
%rsp
Pre-returnStackStructure
...
Rtnaddress
15213 Unused %rsp
Resul3ngStackStructure
...
Rtnaddress
%rsp+8 Saved%rbx
Sean Barker
Stack Frame Components
33
ReturnAddr
SavedRegisters
+Local
Variables
ArgumentBuild
(Op<onal)
Old%rbp
Arguments7+
Caller Frame
Framepointer%rbp
Stackpointer %rsp
(Op<onal)
Sean Barker
Recursion Example
34
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Sean Barker
Recursion Base Case
35
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Register Use(s) Type
%rdi x Argument
%rax Returnvalue Returnvalue
Sean Barker
Recursion Register Save
36
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Register Use(s) Type
%rdi x Argument
%rsp
...
Rtnaddress
Saved%rbx
Sean Barker
Recursion Call Setup
37
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Register Use(s) Type
%rdi x >> 1 Rec.argument
%rbx x & 1 Callee-saved
Sean Barker
Recursive Call
38
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Register Use(s) Type
%rbx x & 1 Callee-saved
%rax Recursivecallreturnvalue
Sean Barker
Recursion Result
39
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Register Use(s) Type
%rbx x & 1 Callee-saved
%rax Returnvalue
Sean Barker
Recursion Result
40
/* Recursive popcount */ long pcount_r(unsigned long x) { if (x == 0) return 0; else return (x & 1) + pcount_r(x >> 1); }
pcount_r: movl $0, %eax testq %rdi, %rdi je .L6 pushq %rbx movq %rdi, %rbx andl $1, %ebx shrq %rdi call pcount_r addq %rbx, %rax popq %rbx .L6: rep; ret
Register Use(s) Type
%rax Returnvalue Returnvalue
%rsp ...