Professional Documents
Culture Documents
07 Machine Procedures
07 Machine Procedures
Instructors:
Randal
E.
Bryant
and
David
R.
O’Hallaron
Today
¢ Procedures
§ Stack
Structure
§ Calling
Conven;ons
Passing
control
§
§ Passing
data
§ Managing
local
data
§ Illustra;on
of
Recursion
x86-‐64
Stack
Stack
“BoUom”
¢ 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
Perspec;ve,
Third
Edi;on
5
Carnegie Mellon
Stack
Grows
+8
Down
Stack
Pointer:
%rsp
Stack “Top”
Today
¢ Procedures
§ Stack
Structure
§ Calling
Conven;ons
Passing
control
§
§ Passing
data
§ Managing
local
data
§ Illustra;on
of
Recursion
0000000000400540 <multstore>:
400540: push %rbx # Save %rbx
400541: mov %rdx,%rbx # Save dest
400544: callq 400550 <mult2> # mult2(x,y)
400549: mov %rax,(%rbx) # Save at dest
40054c: pop %rbx # Restore %rbx
40054d: retq # Return
%rip 0x400544
0000000000400550 <mult2>:
400550: mov %rdi,%rax
•
•
400557: retq
%rip 0x400550
0000000000400550 <mult2>:
400550: mov %rdi,%rax
•
•
400557: retq
%rip 0x400557
0000000000400550 <mult2>:
400550: mov %rdi,%rax
•
•
400557: retq
%rip 0x400549
0000000000400550 <mult2>:
400550: mov %rdi,%rax
•
•
400557: retq
Today
¢ Procedures
§ Stack
Structure
§ Calling
Conven;ons
Passing
control
§
§ Passing
data
§ Managing
local
data
§ Illustra;ons
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
Perspec;ve,
Third
Edi;on
15
void multstore
Data
Flow
(long x, long y, long *dest)
{
Examples
long t = mult2(x, y);
*dest = t;
}
0000000000400540 <multstore>:
# x in %rdi, y in %rsi, dest in %rdx
•••
400541: mov %rdx,%rbx # Save dest
400544: callq 400550 <mult2> # mult2(x,y)
# t in %rax
400549: mov %rax,(%rbx) # Save at dest
•••
Today
¢ Procedures
§ Stack
Structure
§ Calling
Conven;ons
Passing
control
§
§ Passing
data
§ Managing
local
data
§ Illustra;on
of
Recursion
Stack-‐Based
Languages
¢ Languages
that
support
recursion
§ e.g.,
C,
Pascal,
Java
§ Code
must
be
“Reentrant”
Mul;ple
simultaneous
instan;a;ons
of
single
procedure
§
§ Need
some
place
to
store
state
of
each
instan;a;on
§ Arguments
§ Local
variables
§ Return
pointer
Stack
Example
yoo(…) yoo
%rbp
{
yoo
yoo
• who %rsp
•
who();
• amI amI
•
} amI
amI
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
who
• • • •
amI(); %rbp
who();
• • • • amI amI who
• amI(); %rsp
} • • •
} amI
amI
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• who
• • • • amI amI
•
• amI();
} • •amI();
• %rbp
} • amI amI
• %rsp
}
amI
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• •• amI(…)
• who
• { amI amI
•
• amI();•
} • •amI();
••
} • amI amI
• amI();
} • %rbp
• amI
}
amI
%rsp
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• •• amI(…)
• who
• { amI amI
•
• amI();• amI(…)
} • •amI();
• •{
} • • amI amI
• amI(); •
} •
• amI(); amI
} • amI
•
} %rbp
amI
%rsp
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• •• amI(…)
• who
• { amI amI
•
• amI();•
} • •amI();
••
} • amI amI
• amI();
} • %rbp
• amI
}
amI
%rsp
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• who
• • • • amI amI
•
• amI();
} • •amI();
• %rbp
} • amI amI
• %rsp
}
amI
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
who
• • • •
amI(); %rbp
who();
• • • • amI amI who
• amI(); %rsp
} • • •
} amI
amI
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• who
• • • • amI amI
•
• amI();
} • •amI();
• %rbp
} • amI amI
• %rsp
}
amI
Stack
Example
yoo(…) yoo
{ who(…)
•{ yoo
yoo
who
• • • •
amI(); %rbp
who();
• • • • amI amI who
• amI(); %rsp
} • • •
} amI
amI
Stack
Example
yoo
yoo(…) %rbp
{ yoo
yoo
• who %rsp
•
who(); amI amI
•
•
} amI
amI
Example:
incr
long incr(long *p, long val) {
long x = *p;
long y = x + val;
*p = y;
return x;
}
call_incr:
subq $16, %rsp Resul;ng
Stack
Structure
movq $15213, 8(%rsp)
movl $3000, %esi
leaq 8(%rsp), %rdi .
.
.
call incr
addq 8(%rsp), %rax
Rtn
address
addq $16, %rsp
ret 15213 %rsp+8
Unused
%rsp
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
34
Carnegie Mellon
long call_incr() {
long v1 = 15213; .
.
.
long v2 = incr(&v1, 3000);
return v1+v2; Rtn
address
}
18213 %rsp+8
Unused
%rsp
call_incr:
subq $16, %rsp Register
Use(s)
movq $15213, 8(%rsp) %rax Return
value
movl $3000, %esi
leaq 8(%rsp), %rdi Updated
Stack
Structure
call incr
addq 8(%rsp), %rax
addq $16, %rsp .
.
.
ret
Rtn
address
%rsp
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
37
Carnegie Mellon
call_incr:
subq $16, %rsp Register
Use(s)
movq $15213, 8(%rsp) %rax Return
value
movl $3000, %esi
leaq 8(%rsp), %rdi Final
Stack
Structure
call incr
addq 8(%rsp), %rax
addq $16, %rsp .
.
.
ret %rsp
call_incr2:
pushq %rbx
Resul;ng
Stack
Structure
subq $16, %rsp
movq %rdi, %rbx
movq $15213, 8(%rsp) .
.
.
movl $3000, %esi
leaq 8(%rsp), %rdi
call incr Rtn
address
addq %rbx, %rax Saved
%rbx
addq $16, %rsp
15213 %rsp+8
popq %rbx
ret Unused
%rsp
Bryant
and
O’Hallaron,
Computer
Systems:
A
Programmer’s
Perspec;ve,
Third
Edi;on
43
Carnegie Mellon
Today
¢ Procedures
§ Stack
Structure
§ Calling
Conven;ons
Passing
control
§
§ Passing
data
§ Managing
local
data
§ Illustra;on
of
Recursion
Rtn address