Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

Carnegie Mellon

Machine-Level Programming III:


Procedures

Authors: Randal E. Bryant and David R. O’Hallaron


Slides may have modified keeping the class objectives and schedule in mind

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition


Data movement instruction (byte,word
etc)
 Movl $0x4050, %eax# immreg, 4 bytes
 Movw %bp, %sp # reg reg, 2 bytes
 Movb (%rdi,%rcx), %al # mem  reg, 1 byte
 Movb $-17, (%esp) # imm  mem, 1 byte
 Movq %rax,-12(%rbp) # regmem, 8bytes

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2


Mechanisms in Procedures
P(…)
P(…) {{
 Passing control ••
••
 To beginning of procedure code
yy == Q(x);
Q(x);
 Back to return point print(y)
print(y)
 Passing data ••
}}
 Procedure arguments
 Return value
 Memory management int
int Q(int
Q(int i)i)
{{
 Allocate during procedure execution int
int tt == 3*i;
3*i;
 Deallocate upon return int
int v[10];
v[10];
••
 Mechanisms all implemented with ••
machine instructions return
return v[t];
v[t];
 x86-64 implementation of a }}

procedure uses only those


mechanisms required 3
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Carnegie Mellon

Today
 Procedures
 Stack Structure
 Calling Conventions

Passing control
 Passing data
 Managing local data
 Illustration of Recursion

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4


Carnegie Mellon

x86-64 Stack
Stack “Bottom”
 Region of memory managed
with stack discipline
 Grows toward lower addresses Increasing
Addresses

 Register %rsp contains


lowest stack address
 address of “top” element
Stack
Grows
Down
Stack Pointer: %rsp

Stack “Top”

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5


Carnegie Mellon

x86-64 Stack: Push


Stack “Bottom”
 pushq Src
 Fetch operand at Src
 Decrement %rsp by 8 Increasing
Addresses
 Write operand at address given by %rsp

Stack
Grows
Down
Stack Pointer: %rsp
-8

Stack “Top”
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6
Carnegie Mellon

x86-64 Stack: Pop


Stack “Bottom”
 popq Dest
 Read value at address given by %rsp
 Increment %rsp by 8 Increasing
Addresses
 Store value at Dest (must be register)

Stack
Grows
+8 Down
Stack Pointer: %rsp

Stack “Top”

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7


Carnegie Mellon

Today
 Procedures
 Stack Structure
 Calling Conventions

Passing control
 Passing data
 Managing local data
 Illustration of Recursion

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8


void
void multstore
multstore
Code Examples {{
(long
(long x,
x, long
long y,
y, long
long *dest)
*dest)

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

Procedure Control Flow


 Use stack to support procedure call and return
 Procedure call: call label

 Push return address on stack


 Jump to label
 Return address:
 Address of the next instruction right after call
 Procedure return: ret
 Pop address from stack
 Jump to address

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10


Control Flow Example #1 •
0000000000400540 <multstore>:
0x130 •
0000000000400540 <multstore>:
•• 0x128 •
•• 0x120
400544:
400544: callq
callq 400550
400550 <mult2>
<mult2>
400549:
400549: mov
mov %rax,(%rbx)
%rax,(%rbx)
••
•• %rsp 0x120

%rip 0x400544
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11


Control Flow Example #2 •
0000000000400540 <multstore>:
0x130 •
0000000000400540 <multstore>:
•• 0x128 •
•• 0x120
400544:
400544: callq
callq 400550
400550 <mult2>
<mult2>
400549: 0x118 0x400549
400549: mov
mov %rax,(%rbx)
%rax,(%rbx)
••
•• %rsp 0x118

%rip 0x400550
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12


Control Flow Example #3 •
0000000000400540 <multstore>:
0x130 •
0000000000400540 <multstore>:
•• 0x128 •
•• 0x120
400544:
400544: callq
callq 400550
400550 <mult2>
<mult2>
400549: 0x118 0x400549
400549: mov
mov %rax,(%rbx)
%rax,(%rbx)
••
•• %rsp 0x118

%rip 0x400557
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13


Control Flow Example #4 •
0000000000400540 <multstore>:
0x130 •
0000000000400540 <multstore>:
•• 0x128 •
•• 0x120
400544:
400544: callq
callq 400550
400550 <mult2>
<mult2>
400549:
400549: mov
mov %rax,(%rbx)
%rax,(%rbx)
••
•• %rsp 0x120

%rip 0x400549
0000000000400550
0000000000400550 <mult2>:
<mult2>:
400550:
400550: mov
mov %rdi,%rax
%rdi,%rax
••
••
400557:
400557: retq
retq

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14


Carnegie Mellon

Today
 Procedures
 Stack Structure
 Calling Conventions

Passing control
 Passing data
 Managing local data
 Illustrations of Recursion & Pointers

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15


Carnegie Mellon

Procedure Data Flow

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18


Carnegie Mellon

x86-64/Linux Stack Frame


 Current Stack Frame (“Top” to
Bottom)
 “Argument build:” Caller
Frame
Parameters for function about to call
Arguments
 Local variables 7+
If can’t keep in registers Frame pointer Return Addr
 Saved register context %rbp Old %rbp
(Optional)
 Old frame pointer (optional)
Saved
Registers
+
 Caller Stack Frame Local
 Return address Variables
Pushed by call instruction

Argument
 Arguments for this call Stack pointer Build
%rsp (Optional)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19
Carnegie Mellon

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20


Carnegie Mellon

Example: Calling incr #1


Initial Stack Structure
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 %rsp

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

Example: Calling incr #2


Stack Structure
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
15213 %rsp+8
Unused %rsp
call_incr:
call_incr:
subq
subq $16,
$16, %rsp
%rsp
movq $15213, Register Use(s)
movq $15213, 8(%rsp)
8(%rsp)
movl
movl $3000,
$3000, %esi
%esi %rdi &v1
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi %rsi 3000
call
call incr
incr
addq
addq 8(%rsp),
8(%rsp), %rax
%rax
addq
addq $16,
$16, %rsp
%rsp
ret
ret

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 22


Carnegie Mellon

Example: Calling incr #3


Stack Structure
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:
subq
subq $16,
$16, %rsp
%rsp
movq $15213, Register Use(s)
movq $15213, 8(%rsp)
8(%rsp)
movl
movl $3000,
$3000, %esi
%esi %rdi &v1
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi %rsi 3000
call
call incr
incr
addq
addq 8(%rsp),
8(%rsp), %rax
%rax
addq
addq $16,
$16, %rsp
%rsp
ret
ret

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23


Carnegie Mellon

Example: Calling incr #4 Stack Structure

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

Example: Calling incr #5


Updated Stack Structure
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 %rsp

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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25


Carnegie Mellon

Register Saving Conventions


 When procedure yoo calls who:
 yoo is the caller
 who is the callee
 Can register be used for temporary storage?
yoo:
yoo: who:
who:
•• •• •• •• •• ••
movq
movq $15213,
$15213, %rdx
%rdx subq
subq $18213,
$18213, %rdx
%rdx
call
call who
who •• •• ••
addq
addq %rdx,
%rdx, %rax
%rax ret
ret
•• •• ••
ret
ret
 Contents of register %rdx overwritten by who
 This could be trouble ➙ something should be done!
 Need some coordination
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 26
Carnegie Mellon

Register Saving Conventions


 When procedure yoo calls who:
 yoo is the caller
 who is the callee
 Can register be used for temporary storage?
 Conventions

 “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

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 27


Carnegie Mellon

x86-64 Linux Register Usage #1


 %rax Return value %rax
 Return value
 Also caller-saved %rdi
 Can be modified by procedure %rsi
 %rdi, ..., %r9 %rdx
Arguments
 Arguments %rcx
 Also caller-saved
%r8
 Can be modified by procedure
%r9
 %r10, %r11
 Caller-saved %r10
Caller-saved
 Can be modified by procedure temporaries %r11

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 28


Carnegie Mellon

x86-64 Linux Register Usage


#2
 %rbx, %r12, %r13, %r14 %rbx
 Callee-saved Callee-saved %r12
 Callee must save & restore Temporaries %r13
 %rbp %r14
 Callee-saved
 Callee must save & restore
%rbp
Special
 May be used as frame pointer %rsp
 Can mix & match
 %rsp
 Special form of callee save
 Restored to original value upon
exit from procedure

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29


Carnegie Mellon

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

Callee-Saved Example #2 Resulting 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
}}
Saved %rbx
15213 %rsp+8
call_incr2:
call_incr2:
pushq
pushq %rbx
%rbx Unused %rsp
subq
subq $16,
$16, %rsp
%rsp
movq
movq %rdi,
%rdi, %rbx
%rbx
movq
movq $15213,
$15213, 8(%rsp)
8(%rsp) Pre-return Stack Structure
movl
movl $3000,
$3000, %esi
%esi
leaq
leaq 8(%rsp),
8(%rsp), %rdi
%rdi
call
call incr
incr ...
addq
addq %rbx,
%rbx, %rax
%rax
addq
addq $16,
$16, %rsp
%rsp Rtn address %rsp
popq
popq %rbx
%rbx
ret
ret
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31
Carnegie Mellon

Today
 Procedures
 Stack Structure
 Calling Conventions

Passing control
 Passing data
 Managing local data
 Illustration of Recursion (Go over exercise for students)

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32


Carnegie Mellon

Recursive Function pcount_r:


movl $0, %eax
/*
/* Recursive
Recursive popcount
popcount */
*/ testq %rdi, %rdi
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ je .L6
if
if (x
(x ==
== 0)
0) pushq %rbx
return
return 0;
0; movq %rdi, %rbx
else
else andl $1, %ebx
return
return (x
(x && 1)
1) shrq %rdi
++ pcount_r(x
pcount_r(x >>
>> 1);
1); call pcount_r
}} addq %rbx, %rax
popq %rbx
.L6:
rep; ret

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33


Carnegie Mellon

Recursive Function Terminal Case


/*
/* Recursive
Recursive popcount
popcount */
*/ pcount_r:
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ movl $0, %eax
if
if (x
(x ==
== 0)
0) testq %rdi, %rdi
return
return 0;
0; je .L6
else
else pushq %rbx
return
return (x
(x && 1)
1) movq %rdi, %rbx
++ pcount_r(x
pcount_r(x >>
>> 1);
1); andl $1, %ebx
}} shrq %rdi
call pcount_r
addq %rbx, %rax
popq %rbx
.L6:
Register Use(s) Type rep; ret
%rdi x Argument
%rax Return value Return value

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34


Carnegie Mellon

Recursive Function Register Save


pcount_r:
/*
/* Recursive
Recursive popcount
popcount */
*/ movl $0, %eax
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ testq %rdi, %rdi
if
if (x
(x ==
== 0)
0) je .L6
return
return 0;
0; pushq %rbx
else
else movq %rdi, %rbx
return
return (x
(x && 1)
1) andl $1, %ebx
++ pcount_r(x
pcount_r(x >>
>> 1);
1); shrq %rdi
}} call pcount_r
addq %rbx, %rax
popq %rbx
.L6:
rep; ret
Register Use(s) Type
%rdi x Argument
...

Rtn address
Saved %rbx %rsp
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35
Carnegie Mellon

Recursive Function Call Setup


/*
/* Recursive
Recursive popcount
popcount */
*/ pcount_r:
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ movl $0, %eax
if
if (x
(x ==
== 0)
0) testq %rdi, %rdi
return
return 0;
0; je .L6
else
else pushq %rbx
return
return (x
(x && 1)
1) movq %rdi, %rbx
++ pcount_r(x
pcount_r(x >>
>> 1);
1); andl $1, %ebx
}} shrq %rdi
call pcount_r
addq %rbx, %rax
popq %rbx
.L6:
Register Use(s) Type rep; ret
%rdi x >> 1 Rec. argument
%rbx x & 1 Callee-saved

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 36


Carnegie Mellon

Recursive Function Call


/*
/* Recursive
Recursive popcount
popcount */
*/ pcount_r:
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ movl $0, %eax
if
if (x
(x ==
== 0)
0) testq %rdi, %rdi
return
return 0;
0; je .L6
else
else pushq %rbx
return
return (x
(x && 1)
1) movq %rdi, %rbx
++ pcount_r(x
pcount_r(x >>>> 1);
1); andl $1, %ebx
}} shrq %rdi
call pcount_r
addq %rbx, %rax
popq %rbx
.L6:
Register Use(s) Type rep; ret
%rbx x & 1 Callee-saved
%rax Recursive call
return value

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 37


Carnegie Mellon

Recursive Function Result


/*
/* Recursive
Recursive popcount
popcount */
*/ pcount_r:
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ movl $0, %eax
if
if (x
(x ==
== 0)
0) testq %rdi, %rdi
return
return 0;
0; je .L6
else
else pushq %rbx
return
return (x
(x && 1)
1) movq %rdi, %rbx
++ pcount_r(x
pcount_r(x >>
>> 1);
1); andl $1, %ebx
}} shrq %rdi
call pcount_r
addq %rbx, %rax
popq %rbx
.L6:
Register Use(s) Type rep; ret
%rbx x & 1 Callee-saved
%rax Return value

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 38


Carnegie Mellon

Recursive Function Completion


pcount_r:
/*
/* Recursive
Recursive popcount
popcount */
*/ movl $0, %eax
long
long pcount_r(unsigned
pcount_r(unsigned long
long x)
x) {{ testq %rdi, %rdi
if
if (x
(x ==
== 0)
0) je .L6
return
return 0;
0; pushq %rbx
else
else movq %rdi, %rbx
return
return (x
(x && 1)
1) andl $1, %ebx
++ pcount_r(x
pcount_r(x >>
>> 1);
1); shrq %rdi
}} call pcount_r
addq %rbx, %rax
popq %rbx
.L6:
rep; ret
Register Use(s) Type
%rax Return value Return value
...
%rsp

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 39


Carnegie Mellon

Observations About Recursion


 Handled Without Special Consideration
 Stack frames mean that each function call has private storage

Saved registers & local variables
 Saved return pointer
 Register saving conventions prevent one function call from corrupting
another’s data
 Unless the C code explicitly does so (e.g., buffer overflow)
 Stack discipline follows call / return pattern
 If P calls Q, then Q returns before P
 Last-In, First-Out
 Also works for mutual recursion
 P calls Q; Q calls P

Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 40


Carnegie Mellon

x86-64 Procedure Summary


 Important Points
 Stack is the right data structure for procedure call /
return Caller
 If P calls Q, then Q returns before P Frame
Arguments
 Recursion (& mutual recursion) handled 7+
by normal calling conventions Return Addr
%rbp Old %rbp
 Can safely store values in local stack frame and in (Optional)
callee-saved registers Saved
 Put function arguments at top of stack Registers
+
 Result return in %rax
Local
 Pointers are addresses of values Variables
 On stack or global
Argument
Build
%rsp
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 41

You might also like