You are on page 1of 54

Carnegie Mellon

Machine-­‐Level  Programming  III:  


Procedures  
 
15-­‐213:  Introduc;on  to  Computer  Systems  
7th  Lecture,  Sep.  22,  2015  

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

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on  


Mechanisms  in  Procedures  
P(…) {
¢  Passing  control   •

§  To  beginning  of  procedure  code   y = Q(x);
§  Back  to  return  point   print(y)

¢  Passing  data  
}
§  Procedure  arguments  
§  Return  value  
¢  Memory  management   int Q(int i)
{
§  Allocate  during  procedure  execu;on   int t = 3*i;
§  Deallocate  upon  return   int v[10];

¢  Mechanisms  all  implemented  with   •
machine  instruc;ons   return v[t];
}
¢  x86-­‐64  implementa;on  of  a  procedure  
uses  only  those  mechanisms  required  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   2

Carnegie Mellon

Today  
¢  Procedures  
§  Stack  Structure  
§  Calling  Conven;ons  
Passing  control  
§ 
§  Passing  data  
§  Managing  local  data  
§  Illustra;on  of  Recursion  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   3



Carnegie Mellon

x86-­‐64  Stack  
Stack  “BoUom”  
¢  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  Perspec;ve,  Third  Edi;on   4



Carnegie Mellon

x86-­‐64  Stack:  Push  


Stack  “BoUom”  
¢  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  Perspec;ve,  Third  Edi;on   5

Carnegie Mellon

x86-­‐64  Stack:  Pop  


Stack  “BoUom”  
¢  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  Perspec;ve,  Third  Edi;on   6



Carnegie Mellon

Today  
¢  Procedures  
§  Stack  Structure  
§  Calling  Conven;ons  
Passing  control  
§ 
§  Passing  data  
§  Managing  local  data  
§  Illustra;on  of  Recursion  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   7



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

long t = mult2(x, y);


*dest = t;
}

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

long mult2 0000000000400550 <mult2>:


(long a, long b) 400550: mov %rdi,%rax # a
{ 400553: imul %rsi,%rax # a * b
long s = a * b; 400557: retq # Return
return s;
}
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   8

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  instruc;on  right  a]er  call  
§  Example  from  disassembly  
¢  Procedure  return:  ret
§  Pop  address  from  stack  
§  Jump  to  address  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   9



Control  Flow  Example  #1   •

0000000000400540 <multstore>:
0x130 •

• 0x128 •

• 0x120
400544: callq 400550 <mult2>
400549: mov %rax,(%rbx)

• %rsp 0x120

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


400557: retq

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   10



Control  Flow  Example  #2   •

0000000000400540 <multstore>:
0x130 •

• 0x128 •

• 0x120
400544: callq 400550 <mult2>
400549: mov %rax,(%rbx) 0x118 0x400549

• %rsp 0x118

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


400557: retq

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   11



Control  Flow  Example  #3   •

0000000000400540 <multstore>:
0x130 •

• 0x128 •

• 0x120
400544: callq 400550 <mult2>
400549: mov %rax,(%rbx) 0x118 0x400549

• %rsp 0x118

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


400557: retq

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   12



Control  Flow  Example  #4   •

0000000000400540 <multstore>:
0x130 •

• 0x128 •

• 0x120
400544: callq 400550 <mult2>
400549: mov %rax,(%rbx)

• %rsp 0x120

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


400557: retq

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   13



Carnegie Mellon

Today  
¢  Procedures  
§  Stack  Structure  
§  Calling  Conven;ons  
Passing  control  
§ 
§  Passing  data  
§  Managing  local  data  
§  Illustra;ons  of  Recursion  &  Pointers  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   14



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  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
•••

long mult2 0000000000400550 <mult2>:


(long a, long b) # a in %rdi, b in %rsi
{ 400550: mov %rdi,%rax # a
long s = a * b; 400553: imul %rsi,%rax # a * b
return s; # s in %rax
} 400557: retq # Return
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   16

Carnegie Mellon

Today  
¢  Procedures  
§  Stack  Structure  
§  Calling  Conven;ons  
Passing  control  
§ 
§  Passing  data  
§  Managing  local  data  
§  Illustra;on  of  Recursion  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   17



Carnegie Mellon

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  discipline  


§  State  for  given  procedure  needed  for  limited  ;me  
From  when  called  to  when  return  
§ 
§  Callee  returns  before  caller  does  
¢  Stack  allocated  in  Frames  
§  state  for  single  procedure  instan;a;on  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   18

Carnegie Mellon

Call  Chain  Example  


Example
yoo(…) Call  Chain  
{
• yoo
• who(…)
who(); { who
• • • •
• amI();
} amI(…) amI amI
• • • {
amI(); •
• • • amI

} amI();
• amI

}

Procedure  amI()  is  recursive  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   19



Carnegie Mellon

Stack  Frames   Previous  


Frame  
¢  Contents  
§  Return  informa;on  
Frame  Pointer:  %rbp
§  Local  storage  (if  needed)  
(Op;onal)  x
§  Temporary  space  (if  needed)   Frame  for  
  proc

Stack  Pointer:  %rsp


¢  Management  
§  Space  allocated  when  enter  procedure   Stack  “Top”  
“Set-­‐up”  code  
§ 
§  Includes  push  by  call  instruc;on  
§  Deallocated  when  return  
§  “Finish”  code  
§  Includes  pop  by  ret  instruc;on  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   20



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
%rbp
{
yoo
yoo
• who %rsp

who();
• amI amI

} amI

amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   21



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
who
• • • •
amI(); %rbp
who();
• • • • amI amI who
• amI(); %rsp
} • • •
} amI

amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   22



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• who
• • • • amI amI

• amI();
} • •amI();
• %rbp
} • amI amI
• %rsp
}
amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   23



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• •• amI(…)
• who
• { amI amI

• amI();•
} • •amI();
••
} • amI amI
• amI();
} • %rbp
• amI
}
amI
%rsp

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   24



Carnegie Mellon

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

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   25



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• •• amI(…)
• who
• { amI amI

• amI();•
} • •amI();
••
} • amI amI
• amI();
} • %rbp
• amI
}
amI
%rsp

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   26



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• who
• • • • amI amI

• amI();
} • •amI();
• %rbp
} • amI amI
• %rsp
}
amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   27



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
who
• • • •
amI(); %rbp
who();
• • • • amI amI who
• amI(); %rsp
} • • •
} amI

amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   28



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
amI(…) who
• • • •
{
amI();
who();
• who
• • • • amI amI

• amI();
} • •amI();
• %rbp
} • amI amI
• %rsp
}
amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   29



Carnegie Mellon

Stack  
Example  
yoo(…) yoo
{ who(…)
•{ yoo
yoo
who
• • • •
amI(); %rbp
who();
• • • • amI amI who
• amI(); %rsp
} • • •
} amI

amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   30



Carnegie Mellon

Stack  
Example  
yoo
yoo(…) %rbp
{ yoo
yoo
• who %rsp

who(); amI amI


} amI

amI

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   31



Carnegie Mellon

x86-­‐64/Linux  Stack  Frame  


¢  Current  Stack  Frame  (“Top”  to  BoWom)  
§  “Argument  build:”  
Parameters  for  func;on  about  to  call   Caller
Frame  
§  Local  variables  
Arguments  
If  can’t  keep  in  registers   7+  
§  Saved  register  context   Frame  pointer   Return  Addr  
§  Old  frame  pointer  (op;onal)   %rbp Old  %rbp
(Op;onal)
Saved  
¢  Caller  Stack  Frame   Registers  
+  
§  Return  address   Local  
Pushed  by  call  instruc;on  
§  Variables  
§  Arguments  for  this  call   Argument  
Stack  pointer Build  
(Op;onal)  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on  
%rsp
32

Carnegie Mellon

Example:  incr
long incr(long *p, long val) {
long x = *p;
long y = x + val;
*p = y;
return x;
}

incr: Register   Use(s)  


movq (%rdi), %rax
addq %rax, %rsi %rdi Argument  p
movq %rsi, (%rdi) %rsi Argument  val,  y
ret
%rax x,  Return  value

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   33



Carnegie Mellon

Example:  Calling  incr  #1


Ini;al  Stack  Structure  
long call_incr() {
long v1 = 15213;
long v2 = incr(&v1, 3000); .  .  .  
return v1+v2;
} Rtn  address   %rsp

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

Example:  Calling  incr  #2


Stack  Structure  
long call_incr() {
long v1 = 15213;
long v2 = incr(&v1, 3000); .  .  .  
return v1+v2;
}
Rtn  address  
15213 %rsp+8
Unused   %rsp
call_incr:
subq $16, %rsp
movq $15213, 8(%rsp) Register   Use(s)  
movl $3000, %esi %rdi &v1
leaq 8(%rsp), %rdi
call incr %rsi 3000
addq 8(%rsp), %rax
addq $16, %rsp
ret

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   35



Carnegie Mellon

Example:  Calling  incr  #3


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

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   36



Carnegie Mellon

Example:  Calling  incr  #4 Stack  Structure  

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

Example:  Calling  incr  #5


long call_incr() {
Updated  Stack  Structure  
long v1 = 15213;
long v2 = incr(&v1, 3000);
.  .  .  
return v1+v2;
}
Rtn  address   %rsp

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

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   38



Carnegie Mellon

Register  Saving  Conven;ons  


¢  When  procedure  yoo  calls  who:  
§  yoo  is  the  caller  
§  who  is  the  callee  
¢  Can  register  be  used  for  temporary  storage?  
yoo: who:
• • • • • •
movq $15213, %rdx subq $18213, %rdx
call who • • •
addq %rdx, %rax ret
• • •
ret

§  Contents  of  register  %rdx  overwriUen  by  who


§  This  could  be  trouble  ➙  something  should  be  done!  
§  Need  some  coordina;on  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   39

Carnegie Mellon

Register  Saving  Conven;ons  


¢  When  procedure  yoo  calls  who:  
§  yoo  is  the  caller  
§  who  is  the  callee  
¢  Can  register  be  used  for  temporary  storage?  
¢  Conven;ons  

§  “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  Perspec;ve,  Third  Edi;on   40



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  Perspec;ve,  Third  Edi;on   41



Carnegie Mellon

x86-­‐64  Linux  Register  Usage  #2  


¢  %rbx,  %r12,  %r13,  %r14 %rbx
§  Callee-­‐saved   %r12
Callee-­‐saved
§  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  Perspec;ve,  Third  Edi;on   42



Carnegie Mellon

Callee-­‐Saved  Example  #1


Ini;al  Stack  Structure  
long call_incr2(long x) {
long v1 = 15213;
long v2 = incr(&v1, 3000); .  .  .  
return x+v2;
} Rtn  address   %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

Callee-­‐Saved  Example  #2 Resul;ng  Stack  Structure  


long call_incr2(long x) {
long v1 = 15213; .  .  .  
long v2 = incr(&v1, 3000);
return x+v2;
Rtn  address  
}
Saved  %rbx
15213 %rsp+8
call_incr2:
pushq %rbx Unused   %rsp
subq $16, %rsp
movq %rdi, %rbx
movq $15213, 8(%rsp) Pre-­‐return  Stack  Structure  
movl $3000, %esi
leaq 8(%rsp), %rdi
call incr .  .  .  
addq %rbx, %rax
addq $16, %rsp Rtn  address   %rsp
popq %rbx
ret
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   44

Carnegie Mellon

Today  
¢  Procedures  
§  Stack  Structure  
§  Calling  Conven;ons  
Passing  control  
§ 
§  Passing  data  
§  Managing  local  data  
§  Illustra;on  of  Recursion  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   45



Carnegie Mellon

Recursive  Func;on   pcount_r:


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

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   46



Carnegie Mellon

Recursive  Func;on  Terminal  Case  


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

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   47



Carnegie Mellon

Recursive  Func;on  Register  Save  


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

Rtn  address  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on  


Saved  %rbx %rsp
48

Carnegie Mellon

Recursive  Func;on  Call  Setup  


/* Recursive popcount */ pcount_r:
long pcount_r(unsigned long x) { movl $0, %eax
if (x == 0) testq %rdi, %rdi
return 0; je .L6
else pushq %rbx
return (x & 1) movq %rdi, %rbx
+ pcount_r(x >> 1); 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  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   49



Carnegie Mellon

Recursive  Func;on  Call  


/* Recursive popcount */ pcount_r:
long pcount_r(unsigned long x) { movl $0, %eax
if (x == 0) testq %rdi, %rdi
return 0; je .L6
else pushq %rbx
return (x & 1) movq %rdi, %rbx
+ pcount_r(x >> 1); 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 Recursive  call  
return  value  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   50



Carnegie Mellon

Recursive  Func;on  Result  


/* Recursive popcount */ pcount_r:
long pcount_r(unsigned long x) { movl $0, %eax
if (x == 0) testq %rdi, %rdi
return 0; je .L6
else pushq %rbx
return (x & 1) movq %rdi, %rbx
+ pcount_r(x >> 1); 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 Return  value  

Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec;ve,  Third  Edi;on   51



Carnegie Mellon

Recursive  Func;on  Comple;on  


pcount_r:
/* Recursive popcount */ movl $0, %eax
long pcount_r(unsigned long x) { testq %rdi, %rdi
if (x == 0) je .L6
return 0; pushq %rbx
else movq %rdi, %rbx
return (x & 1) andl $1, %ebx
+ pcount_r(x >> 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  Perspec;ve,  Third  Edi;on   52



Carnegie Mellon

Observa;ons  About  Recursion  


¢  Handled  Without  Special  Considera;on  
§  Stack  frames  mean  that  each  func;on  call  has  private  storage  
Saved  registers  &  local  variables  
§ 
§  Saved  return  pointer  
§  Register  saving  conven;ons  prevent  one  func;on  call  from  corrup;ng  
another’s  data  
§  Unless  the  C  code  explicitly  does  so  (e.g.,  buffer  overflow  in  Lecture  9)  
§  Stack  discipline  follows  call  /  return  paUern  
§  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  Perspec;ve,  Third  Edi;on   53



Carnegie Mellon

x86-­‐64  Procedure  Summary  


¢  Important  Points  
§  Stack  is  the  right  data  structure  for  procedure  call  /  
return   Caller
Frame  
§  If  P  calls  Q,  then  Q  returns  before  P  
Arguments  
¢  Recursion  (&  mutual  recursion)  handled  by   7+  
normal  calling  conven;ons   Return  Addr  
%rbp Old  %rbp  
§  Can  safely  store  values  in  local  stack  frame  and  in   (Op;onal)  
callee-­‐saved  registers   Saved
§  Put  func;on  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  Perspec;ve,  Third  Edi;on   54

You might also like