Professional Documents
Culture Documents
CS 312 Lecture - 7c - Assembly Level ProgrammingIII-procedures
CS 312 Lecture - 7c - Assembly Level ProgrammingIII-procedures
Today
Procedures
Stack Structure
Calling Conventions
Passing control
Passing data
Managing local data
Illustration of Recursion
x86-64 Stack
Stack “Bottom”
Region of memory managed
with stack discipline
Grows toward lower addresses Increasing
Addresses
Stack “Top”
Stack
Grows
Down
Stack Pointer: %rsp
-8
Stack “Top”
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
Carnegie Mellon
Stack
Grows
+8 Down
Stack Pointer: %rsp
Stack “Top”
Today
Procedures
Stack Structure
Calling Conventions
Passing control
Passing data
Managing local data
Illustration of Recursion
long
long tt == mult2(x,
mult2(x, y);
y);
*dest
*dest == t;
t;
}}
0000000000400540
0000000000400540 <multstore>:
<multstore>:
400540:
400540: push
push %rbx
%rbx ## Save
Save %rbx
%rbx
400541:
400541: mov
mov %rdx,%rbx
%rdx,%rbx ## Save
Save dest
dest
400544:
400544: callq
callq 400550
400550 <mult2>
<mult2> ## mult2(x,y)
mult2(x,y)
400549:
400549: mov
mov %rax,(%rbx)
%rax,(%rbx) ## Save
Save at
at dest
dest
40054c:
40054c: pop
pop %rbx
%rbx ## Restore
Restore %rbx
%rbx
40054d:
40054d: retq
retq ## Return
Return
long
long mult2
mult2 0000000000400550
0000000000400550 <mult2>:
<mult2>:
(long
(long a,
a, long
long b)
b) 400550:
400550: mov
mov %rdi,%rax
%rdi,%rax ## aa
{{ 400553:
400553: imul
imul %rsi,%rax
%rsi,%rax ## aa ** bb
long
long ss == aa ** b;
b; 400557:
400557: retq
retq ## Return
Return
return
return s;s;
}}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9
Carnegie Mellon
%rip 0x400544
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq
%rip 0x400550
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq
%rip 0x400557
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq
%rip 0x400549
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq
Today
Procedures
Stack Structure
Calling Conventions
Passing control
Passing data
Managing local data
Illustrations of Recursion & Pointers
Registers Stack
First 6 arguments
•••
%rdi
%rsi Arg n
%rdx
•••
%rcx
%r8 Arg 8
%r9 Arg 7
Return value
%rax Only allocate stack space
when needed
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16
void
void multstore
multstore
Data Flow (long
(long x,
x, long
long y,
y, long
long *dest)
*dest)
{{
Examples long
long tt == mult2(x,
mult2(x, y);
y);
*dest
*dest == t;
t;
}}
0000000000400540
0000000000400540 <multstore>:
<multstore>:
## xx in
in %rdi,
%rdi, yy in
in %rsi,
%rsi, dest
dest in
in %rdx
%rdx
••••••
400541:
400541: mov
mov %rdx,%rbx
%rdx,%rbx ## Save
Save dest
dest
400544:
400544: callq
callq 400550
400550 <mult2>
<mult2> ## mult2(x,y)
mult2(x,y)
## tt in
in %rax
%rax
400549:
400549: mov
mov %rax,(%rbx)
%rax,(%rbx) ## Save
Save at
at dest
dest
••••••
long
long mult2
mult2 0000000000400550
0000000000400550 <mult2>:
<mult2>:
(long
(long a,
a, long
long b)
b) ## aa in
in %rdi,
%rdi, bb in
in %rsi
%rsi
{{ 400550:
400550: mov
mov %rdi,%rax
%rdi,%rax ## aa
long
long ss == aa ** b;
b; 400553:
400553: imul
imul %rsi,%rax
%rsi,%rax ## aa ** bb
return
return s;s; ## ss in
in %rax
%rax
}} 400557:
400557: retq
retq ## Return
Return
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17
Carnegie Mellon
Today
Procedures
Stack Structure
Calling Conventions
Passing control
Passing data
Example: incr
long
long incr(long
incr(long *p, *p, long
long val)
val) {{
long
long xx == *p;
*p;
long
long yy == xx ++ val;
val;
*p
*p == y;
y;
return
return x;x;
}}
incr:
incr: Register Use(s)
movq
movq (%rdi),
(%rdi), %rax
%rax %rdi Argument p
addq
addq %rax,
%rax, %rsi
%rsi
movq %rsi, %rsi Argument val, y
movq %rsi, (%rdi)
(%rdi)
ret
ret %rax x, Return value
call_incr:
call_incr:
subq
subq $16,
$16, %rsp
%rsp Resulting Stack Structure
movq
movq $15213,
$15213, 8(%rsp)
8(%rsp)
movl
movl $3000,
$3000, %esi
%esi
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi ...
call
call incr
incr
addq
addq 8(%rsp),
8(%rsp), %rax
%rax Rtn address
addq
addq $16,
$16, %rsp
%rsp
ret
ret 15213 %rsp+8
Unused %rsp
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 21
Carnegie Mellon
long
long call_incr()
call_incr() {{
long ...
long v1
v1 == 15213;
15213;
long
long v2
v2 == incr(&v1,
incr(&v1, 3000);
3000);
return
return v1+v2;
v1+v2; Rtn address
}}
18213 %rsp+8
Unused %rsp
call_incr:
call_incr: Register Use(s)
subq
subq $16,
$16, %rsp
%rsp
movq
movq $15213,
$15213, 8(%rsp)
8(%rsp) %rax Return value
movl
movl $3000,
$3000, %esi
%esi
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi Updated Stack Structure
call
call incr
incr
addq
addq 8(%rsp),
8(%rsp), %rax
%rax
addq
addq $16,
$16, %rsp
%rsp ...
ret
ret
Rtn address %rsp
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 24
Carnegie Mellon
call_incr:
call_incr: Register Use(s)
subq
subq $16,
$16, %rsp
%rsp
movq $15213, %rax Return value
movq $15213, 8(%rsp)
8(%rsp)
movl
movl $3000,
$3000, %esi
%esi
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi Final Stack Structure
call
call incr
incr
addq
addq 8(%rsp),
8(%rsp), %rax
%rax
addq
addq $16,
$16, %rsp
%rsp ...
ret
ret %rsp
“Caller Saved”
Caller saves temporary values in its frame before the call
“Callee Saved”
Callee saves temporary values in its frame before using
Callee restores them before returning to caller
Callee-Saved Example #1
Initial Stack Structure
long
long call_incr2(long
call_incr2(long x)
x) {{
long
long v1
v1 == 15213;
15213;
long
long v2
v2 == incr(&v1,
incr(&v1, 3000);
3000); ...
return
return x+v2;
x+v2;
}} Rtn address %rsp
call_incr2:
call_incr2:
pushq
pushq %rbx
%rbx Resulting Stack Structure
subq
subq $16,
$16, %rsp
%rsp
movq
movq %rdi,
%rdi, %rbx
%rbx
movq
movq $15213,
$15213, 8(%rsp)
8(%rsp) ...
movl
movl $3000,
$3000, %esi
%esi
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi
call incr Rtn address
call incr
addq
addq %rbx,
%rbx, %rax
%rax Saved %rbx
addq
addq $16,
$16, %rsp
%rsp 15213 %rsp+8
popq
popq %rbx
%rbx
ret
ret Unused %rsp
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 30
Carnegie Mellon
Today
Procedures
Stack Structure
Calling Conventions
Passing control
Passing data
Managing local data
Illustration of Recursion (Go over exercise for students)
Rtn address
Saved %rbx %rsp
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35
Carnegie Mellon