0% found this document useful (0 votes)
8 views42 pages

06-machine-control

Uploaded by

cherfaoui nafaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views42 pages

06-machine-control

Uploaded by

cherfaoui nafaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Carnegie

CarnegieMellon
Mellon

Machine-­‐Level  Programming  II:  Control  


 
15-­‐213:  Introduc�on  to  Computer  Systems  
6  Lecture,  Sep.  17,  2015  
th

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

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



Carnegie Mellon

Today  
¢ Control:  Condi�on  codes  
¢ Condi�onal  branches  

¢ Loops  

¢ Switch  Statements  

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



Carnegie Mellon

Processor  State  (x86-­‐64,  Par�al)  


¢ Informa�on  about  
currently  execu�ng   Registers  
program   %rax %r8
§ Temporary  data   %rbx %r9
(  %rax,  …  )   %rcx %r10
§ Loca�on  of  run�me  stack   %rdx %r11
(  %rsp  )   %rsi %r12
§ Loca�on  of  current  code   %rdi %r13
control  point   %rsp %r14
(  %rip,  …  )  
%rbp %r15
§ Status  of  recent  tests  
(  CF,  ZF,  SF,  OF  )  
%rip Instruc�on  pointer  
Current  stack  top  
CF ZF SF OF Condi�on  codes  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   3

Carnegie Mellon

Condi�on  Codes  (Implicit  Se�ng)  


¢ Single  bit  registers  
§CF    Carry  Flag  (for  unsigned)  SF    Sign  Flag  (for  signed)  
§ZF    Zero  Flag  OF    Overflow  Flag  (for  signed)  

¢ Implicitly  set  (think  of  it  as  side  effect)  by  arithme�c  opera�ons  
Example:  addq  Src,Dest  ↔  t = a+b  
CF  set  if  carry  out  from  most  significant  bit  (unsigned  overflow)  
ZF  set  if  t == 0  
SF  set  if  t < 0  (as  signed)  
OF  set  if  two’s-­‐complement  (signed)  overflow  
(a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)  

¢ Not  set  by  leaq  instruc�on  


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

Carnegie Mellon

Condi�on  Codes  (Explicit  Se�ng:  Compare)  


¢ Explicit  Se�ng  by  Compare  Instruc�on  
§cmpq  Src2,  Src1  
§cmpq b,a  like  compu�ng  a-b  without  se�ng  des�na�on  

§CF  set  if  carry  out  from  most  significant  bit  (used  for  unsigned  comparisons)  
§ZF  set  if  a == b  
§SF  set  if  (a-b) < 0  (as  signed)  
§OF  set  if  two’s-­‐complement  (signed)  overflow  
(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

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



Carnegie Mellon

Condi�on  Codes  (Explicit  Se�ng:  Test)  


¢ Explicit  Se�ng  by  Test  instruc�on  
§testq  Src2,  Src1  
§testq b,a  like  compu�ng  a&b  without  se�ng  des�na�on    

§Sets  condi�on  codes  based  on  value  of  Src1  &  Src2  
§Useful  to  have  one  of  the  operands  be  a  mask  

§ZF  set  when  a&b == 0  


§SF  set  when  a&b < 0

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



Carnegie Mellon

Reading  Condi�on  Codes  


¢ SetX  Instruc�ons  
§ Set  low-­‐order  byte  of  des�na�on  to  0  or  1  based  on  combina�ons  of  
condi�on  codes  
§ Does  not  alter  remaining  7  bytes  
SetX   Condi�on   Descrip�on  
sete ZF Equal  /  Zero  
setne ~ZF Not  Equal  /  Not  Zero  
sets SF Nega�ve  
setns ~SF Nonnega�ve  
setg ~(SF^OF)&~ZF Greater  (Signed)  
setge ~(SF^OF) Greater  or  Equal  (Signed)  
setl (SF^OF) Less  (Signed)  
setle (SF^OF)|ZF Less  or  Equal  (Signed)  
seta ~CF&~ZF Above  (unsigned)  
setb CF Below  (unsigned)  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   7

x86-­‐64  Integer  Registers  
%rax %al %r8 %r8b

%rbx %bl %r9 %r9b

%rcx %cl %r10 %r10b

%rdx %dl %r11 %r11b

%rsi %sil %r12 %r12b

%rdi %dil %r13 %r13b

%rsp %spl %r14 %r14b

%rbp %bpl %r15 %r15b

§ Can  reference  low-­‐order  byte  


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

Carnegie Mellon

Reading  Condi�on  Codes  (Cont.)  


¢ SetX  Instruc�ons:    
§ Set  single  byte  based  on  combina�on  of  condi�on  
codes  
¢ One  of  addressable  byte  registers  
§ Does  not  alter  remaining  bytes  
§ Typically  use  movzbl  to  finish  job  
§ 32-­‐bit  instruc�ons  also  set  upper  32  bits  to  0  
Register   Use(s)  
int gt (long x, long y)
{ %rdi Argument  x
return x > y; %rsi Argument  y
}
%rax Return  value

cmpq %rsi, %rdi # Compare x:y


setg %al # Set when >
movzbl %al, %eax # Zero rest of %rax
ret
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   9

Carnegie Mellon

Today  
¢ Control:  Condi�on  codes  
¢ Condi�onal  branches  

¢ Loops  

¢ Switch  Statements  

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



Carnegie Mellon

Jumping  
¢ jX  Instruc�ons  
§ Jump  to  different  part  of  code  depending  on  condi�on  codes  

jX   Condi�on   Descrip�on  
jmp 1 Uncondi�onal  
je ZF Equal  /  Zero  
jne ~ZF Not  Equal  /  Not  Zero  
js SF Nega�ve  
jns ~SF Nonnega�ve  
jg ~(SF^OF)&~ZF Greater  (Signed)  
jge ~(SF^OF) Greater  or  Equal  (Signed)  
jl (SF^OF) Less  (Signed)  
jle (SF^OF)|ZF Less  or  Equal  (Signed)  
ja ~CF&~ZF Above  (unsigned)  
jb CF Below  (unsigned)  

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



Carnegie Mellon

Condi�onal  Branch  Example  (Old  Style)  


¢ Genera�on  
shark> gcc –Og -S –fno-if-conversion control.c
absdiff:
long absdiff cmpq %rsi, %rdi # x:y
(long x, long y) jle .L4
{ movq %rdi, %rax
long result; subq %rsi, %rax
if (x > y) ret
result = x-y; .L4: # x <= y
else movq %rsi, %rax
result = y-x; subq %rdi, %rax
return result; ret
}
Register   Use(s)  
%rdi Argument  x
%rsi Argument  y
%rax Return  value
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   12

Carnegie Mellon

Expressing  with  Goto  Code  


¢ C  allows  goto  statement  
¢ Jump  to  posi�on  designated  by  label  

long absdiff long absdiff_j


(long x, long y) (long x, long y)
{ {
long result; long result;
if (x > y) int ntest = x <= y;
result = x-y; if (ntest) goto Else;
else result = x-y;
result = y-x; goto Done;
return result; Else:
} result = y-x;
Done:
return result;
}

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



Carnegie Mellon

General  Condi�onal  Expression  Transla�on  


(Using  Branches)  
C  Code  
val = Test ? Then_Expr : Else_Expr;

val = x>y ? x-y : y-x;

Goto  Version  
ntest = !Test; § Create  separate  code  regions  for  
if (ntest) goto Else;
then  &  else  expressions  
val = Then_Expr;
goto Done; § Execute  appropriate  one  
Else:
val = Else_Expr;
Done:
. . .

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



Carnegie Mellon

Using  Condi�onal  Moves  


¢ Condi�onal  Move  Instruc�ons  
§ Instruc�on  supports:  
if  (Test)  Dest  ß  Src  
§ Supported  in  post-­‐1995  x86  processors   C  Code  
§ GCC  tries  to  use  them   val = Test
§ But,  only  when  known  to  be  safe   ? Then_Expr
¢ Why?   : Else_Expr;
§ Branches  are  very  disrup�ve  to  
instruc�on  flow  through  pipelines  
Goto  Version  
§ Condi�onal  moves  do  not  require  control   result = Then_Expr;
transfer   eval = Else_Expr;
nt = !Test;
if (nt) result = eval;
return result;

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



Carnegie Mellon

Condi�onal  Move  Example  


long absdiff
(long x, long y)
{ Register   Use(s)  
long result;
if (x > y) %rdi Argument  x
result = x-y; %rsi Argument  y
else
%rax Return  value
result = y-x;
return result;
}

absdiff:
movq %rdi, %rax # x
subq %rsi, %rax # result = x-y
movq %rsi, %rdx
subq %rdi, %rdx # eval = y-x
cmpq %rsi, %rdi # x:y
cmovle %rdx, %rax # if <=, result = eval
ret
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   16

Carnegie Mellon

Bad  Cases  for  Condi�onal  Move  


Expensive  Computa�ons  
val = Test(x) ? Hard1(x) : Hard2(x);

¢ Both  values  get  computed  


¢ Only  makes  sense  when  computa�ons  
are  very  simple  
Risky  Computa�ons  
val = p ? *p : 0;

¢ Both  values  get  computed  


¢ May  have  undesirable  effects  
Computa�ons  with  side  effects  
val = x > 0 ? x*=7 : x+=3;

¢ Both  values  get  computed  


¢ Must  be  side-­‐effect  free  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   17

Carnegie Mellon

Today  
¢ Control:  Condi�on  codes  
¢ Condi�onal  branches  

¢ Loops  

¢ Switch  Statements  

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



Carnegie Mellon

“Do-­‐While”  Loop  Example  


C  Code   Goto  Version  
long pcount_do long pcount_goto
(unsigned long x) { (unsigned long x) {
long result = 0; long result = 0;
do { loop:
result += x & 0x1; result += x & 0x1;
x >>= 1; x >>= 1;
} while (x); if(x) goto loop;
return result; return result;
} }

¢ Count  number  of  1’s  in  argument  x  (“popcount”)  


¢ Use  condi�onal  branch  to  either  con�nue  looping  or  to  exit  

loop  

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



Carnegie Mellon

“Do-­‐While”  Loop  Compila�on  


Goto  Version  
long pcount_goto
(unsigned long x) {
Register   Use(s)  
long result = 0;
loop: %rdi Argument  x
result += x & 0x1; %rax result
x >>= 1;
if(x) goto loop;
return result;
}

movl $0, %eax # result = 0


.L2: # loop:
movq %rdi, %rdx
andl $1, %edx # t = x & 0x1
addq %rdx, %rax # result += t
shrq %rdi # x >>= 1
jne .L2 # if (x) goto loop
rep; ret
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   20

Carnegie Mellon

General  “Do-­‐While”  Transla�on  


C  Code   Goto  Version  
do loop:
Body   Body  
while (Test); if (Test)
goto loop
¢ Body:   {
Statement1;
Statement2;

Statementn;
}

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



Carnegie Mellon

General  “While”  Transla�on  #1  

¢ “Jump-­‐to-­‐middle”  transla�on  
¢ Used  with  -Og Goto  Version  
goto test;
loop:
While  version   Body  
while (Test) test:
Body   if (Test)
goto loop;
done:

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



Carnegie Mellon

While  Loop  Example  #1  


C  Code   Jump  to  Middle  
long pcount_while long pcount_goto_jtm
Version  
(unsigned long x) { (unsigned long x) {
long result = 0; long result = 0;
while (x) { goto test;
result += x & 0x1; loop:
x >>= 1; result += x & 0x1;
} x >>= 1;
return result; test:
} if(x) goto loop;
return result;
}

¢ Compare  to  do-­‐while  version  of  func�on  


¢ Ini�al  goto  starts  loop  at  test  

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



Carnegie Mellon

General  “While”  Transla�on  #2  


While  version  
¢ “Do-­‐while”  conversion  
while (Test)
¢ Used  with  –O1
Body  

Goto  Version  
Do-­‐While  Version   if (!Test)
if (!Test) goto done;
goto done; loop:
do Body  
Body   if (Test)
while(Test); goto loop;
done: done:
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   24

Carnegie Mellon

While  Loop  Example  #2  


C  Code   Do-­‐While  Version  
long pcount_while long pcount_goto_dw
(unsigned long x) { (unsigned long x) {
long result = 0; long result = 0;
while (x) { if (!x) goto done;
result += x & 0x1; loop:
x >>= 1; result += x & 0x1;
} x >>= 1;
return result; if(x) goto loop;
} done:
return result;
}

¢ Compare  to  do-­‐while  version  of  func�on  


¢ Ini�al  condi�onal  guards  entrance  to  loop  

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



Carnegie Mellon

“For”  Loop  Form   Init  


General  Form   i   = 0
 
for (Init; Test; Update ) Test  
Body
i   < WSIZE

#define WSIZE 8*sizeof(int) Update  


long pcount_for i++
(unsigned long x)
 
{
size_t i; Body  
long result = 0; {  
for (i = 0; i < WSIZE; i++) unsigned bit =
{ (x >> i) & 0x1;
unsigned bit = result += bit;
(x >> i) & 0x1; }
result += bit;
}
return result;
}
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   26

Carnegie Mellon

“For”  Loop  à  


à While  Loop  
For  Version  
 
for (Init; Test; Update )
Body  

While  Version  
 
Init;
while (Test  ) {
Body

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

Carnegie Mellon

For-­‐While  Conversion  
long pcount_for_while
Init   (unsigned long x)
{
i   = 0 size_t i;
long result = 0;
Test   i = 0;
i   < WSIZE while (i < WSIZE)
{
Update   unsigned bit =
(x >> i) & 0x1;
i++ result += bit;
i++;
Body   }
{   return result;
unsigned bit = }
(x >> i) & 0x1;
result += bit;
}

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



Carnegie Mellon

“For”  Loop  Do-­‐While  Conversion  


Goto  Version  
C  Code   long pcount_for_goto_dw
(unsigned long x) {
long pcount_for size_t i;
(unsigned long x) long result = 0;
{ i = 0; Init  
size_t i; if (!(i < WSIZE))
long result = 0; goto done; !Test  
for (i = 0; i < WSIZE; i++) loop:
{ {
unsigned bit = unsigned bit =
(x >> i) & 0x1; (x >> i) & 0x1; Body  
result += bit; result += bit;
} }
return result; i++; Update  
} if (i < WSIZE)
Test  
goto loop;
¢ Ini�al  test  can  be  op�mized   done:
away   return result;

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


}
29

Carnegie Mellon

Today  

¢ Control:  Condi�on  codes  


¢ Condi�onal  branches  
¢ Loops  
¢ Switch  Statements  

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



Carnegie Mellon

long switch_eg
(long x, long y, long z) Switch  Statement  
{
long w = 1; Example  
switch(x) {
case 1:
w = y*z; ¢ Mul�ple  case  labels  
break; § Here:  5  &  6  
case 2:
w = y/z; ¢ Fall  through  cases  
/* Fall Through */ § Here:  2  
case 3:
w += z; ¢ Missing  cases  
break; § Here:  4  
case 5:
case 6:
w -= z;
break;
default:
w = 2;
}
return w;
}
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   31

Carnegie Mellon

Jump  Table  Structure  


Switch  Form   Jump  Table   Jump  Targets  
switch(x) { jtab: Targ0
Targ0: Code  Block
case val_0: 0  
Block  0 Targ1
case val_1: Targ2 Targ1: Code  Block
Block  1 @
@@@ 1  
@
case val_n-1: @
Block  n–1 Targ2: Code  Block
} Targn-1
2  

@
Transla�on  (Extended  C)   @
goto *JTab[x]; @

Targn-1: Code  Block


n–1  

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



Carnegie Mellon

Switch  Statement  Example  


long switch_eg(long x, long y, long z)
{
long w = 1;
switch(x) {
. . .
}
return w;
}

Setup:  
Register   Use(s)  
switch_eg:
movq %rdx, %rcx %rdi Argument  x
cmpq $6, %rdi # x:6 %rsi Argument  y
ja .L8
jmp *.L4(,%rdi,8) %rdx Argument  z
%rax Return  value
What  range  of  values   Note  that  w  not  
takes  default?  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on  
ini�alized  here   33

Carnegie Mellon

Switch  Statement  Example  


long switch_eg(long x, long y, long z)
{
long w = 1;
switch(x) {
Jump  table  
. . .
.section .rodata
} .align 8
return w; .L4:
} .quad .L8 # x = 0
.quad .L3 # x = 1
.quad .L5 # x = 2
Setup:   .quad .L9 # x = 3
.quad .L8 # x = 4
switch_eg: .quad .L7 # x = 5
movq %rdx, %rcx .quad .L7 # x = 6
cmpq $6, %rdi # x:6
ja .L8 # Use default
Indirect     jmp *.L4(,%rdi,8) # goto *JTab[x]
jump  

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



Carnegie Mellon

Assembly  Setup  Explana�on  


¢ Table  Structure   Jump  table  
§ Each  target  requires  8  bytes  
.section .rodata
§ Base  address  at  .L4   .align 8
.L4:
.quad .L8 # x = 0
.quad .L3 # x = 1
¢ Jumping   .quad .L5 # x = 2
.quad .L9 # x = 3
§ Direct:  jmp .L8 .quad .L8 # x = 4
.quad .L7 # x = 5
§ Jump  target  is  denoted  by  label  .L8   .quad .L7 # x = 6

§ Indirect:  jmp *.L4(,%rdi,8)


§ Start  of  jump  table:  .L4  
§ Must  scale  by  factor  of  8  (addresses  are  8  bytes)  
§ Fetch  target  from  effec�ve  Address  .L4 + x*8  
§ Only  for    0  ≤  x  ≤  6  

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



Carnegie Mellon

Jump  Table  
Jump  table  
switch(x) {
.section .rodata case 1: // .L3
.align 8 w = y*z;
.L4: break;
.quad .L8 # x = 0
.quad .L3 # x = 1 case 2: // .L5
.quad .L5 # x = 2 w = y/z;
.quad .L9 # x = 3 /* Fall Through */
.quad .L8 # x = 4
.quad .L7 # x = 5
case 3: // .L9
.quad .L7 # x = 6 w += z;
break;
case 5:
case 6: // .L7
w -= z;
break;
default: // .L8
w = 2;
}

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



Carnegie Mellon

Code  Blocks  (x  ==  1)  


switch(x) { .L3:
case 1: // .L3 movq %rsi, %rax # y
w = y*z; imulq %rdx, %rax # y*z
break; ret
. . .
}

Register   Use(s)  
%rdi Argument  x
%rsi Argument  y
%rdx Argument  z
%rax Return  value

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



Carnegie Mellon

Handling  Fall-­‐Through  
long w = 1;
. . .
switch(x) { case 2:
. . . w = y/z;
case 2: goto merge;
w = y/z;
/* Fall Through */
case 3:
w += z;
break;
. . .
case 3:
}
w = 1;

merge:
w += z;

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



Carnegie Mellon

Code  Blocks  (x  ==  2,  x  ==  3)  


.L5: # Case 2
long w = 1; movq %rsi, %rax
. . . cqto
switch(x) { idivq %rcx # y/z
. . . jmp .L6 # goto merge
case 2: .L9: # Case 3
w = y/z; movl $1, %eax # w = 1
/* Fall Through */ .L6: # merge:
case 3: addq %rcx, %rax # w += z
w += z; ret
break;
. . .
} Register   Use(s)  
%rdi Argument  x
%rsi Argument  y
%rdx Argument  z
%rax Return  value
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   39

Carnegie Mellon

Code  Blocks  (x  ==  5,  x  ==  6,  default)  


switch(x) { .L7: # Case 5,6
. . . movl $1, %eax # w = 1
case 5: // .L7 subq %rdx, %rax # w -= z
case 6: // .L7 ret
w -= z; .L8: # Default:
break; movl $2, %eax # 2
default: // .L8 ret
w = 2;
}

Register   Use(s)  
%rdi Argument  x
%rsi Argument  y
%rdx Argument  z
%rax Return  value
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   40

Carnegie Mellon

Summarizing  
¢ C  Control  
§ if-­‐then-­‐else  
§ do-­‐while  
§ while,  for  
§ switch  
¢ Assembler  Control  
§ Condi�onal  jump  
§ Condi�onal  move  
§ Indirect  jump  (via  jump  tables)  
§ Compiler  generates  code  sequence  to  implement  more  complex  control  
¢ Standard  Techniques  
§ Loops  converted  to  do-­‐while  or  jump-­‐to-­‐middle  form  
§ Large  switch  statements  use  jump  tables  
§ Sparse  switch  statements  may  use  decision  trees  (if-­‐elseif-­‐elseif-­‐else)  
Bryant  and  O’Hallaron,  Computer  Systems:  A  Programmer’s  Perspec�ve,  Third  Edi�on   41

Carnegie Mellon

Summary  
¢ Today  
§ Control:  Condi�on  codes  
§ Condi�onal  branches  &  condi�onal  moves  
§ Loops  
§ Switch  statements  
¢ Next  Time  
§ Stack  
§ Call  /  return  
§ Procedure  call  discipline  

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

You might also like