0% found this document useful (0 votes)
18 views

CS 312 Lecture - 7b - Machine Level ProgrammingII-control

This document discusses control flow in machine-level programming, including condition codes, conditional branching, loops, and switch statements. It explains how arithmetic and comparison instructions implicitly or explicitly set condition codes like the zero flag and carry flag. Conditional jump instructions allow branching code execution based on these condition codes. Reading and setting individual bits in registers allows condition codes to control program flow.

Uploaded by

Hypazia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

CS 312 Lecture - 7b - Machine Level ProgrammingII-control

This document discusses control flow in machine-level programming, including condition codes, conditional branching, loops, and switch statements. It explains how arithmetic and comparison instructions implicitly or explicitly set condition codes like the zero flag and carry flag. Conditional jump instructions allow branching code execution based on these condition codes. Reading and setting individual bits in registers allows condition codes to control program flow.

Uploaded by

Hypazia
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Carnegie

CarnegieMellon
Mellon

Machine-Level Programming II:


Control

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

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


Carnegie Mellon

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

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


Carnegie Mellon

Processor State (x86-64, Partial)


 Information about
currently executing Registers
program %rax %r8
 Temporary data %rbx %r9
( %rax, … ) %rcx %r10
 Location of runtime stack %rdx %r11
( %rsp ) %rsi %r12
 Location of current code %rdi %r13
control point %rsp %r14
( %rip, … )
%rbp %r15
 Status of recent tests
( CF, ZF, SF, OF )
%rip Instruction pointer
Current stack top
CF ZF SF OF Condition codes
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3
Carnegie Mellon

Condition Codes (Implicit Setting)


 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 arithmetic


operations
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 instruction


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

Condition Codes (Explicit Setting:


Compare)
 Explicit Setting by Compare Instruction
cmpq Src2, Src1
cmpq b,a like computing a-b without setting destination

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 Perspective, Third Edition 5


Carnegie Mellon

Condition Codes (Explicit Setting: Test)


 Explicit Setting by Test instruction
testq Src2, Src1
testq b,a like computing a&b without setting destination

Sets condition 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 Perspective, Third Edition 6


Carnegie Mellon

Reading Condition Codes


 SetX Instructions
 Set low-order byte of destination to 0 or 1 based on combinations of
condition codes
 Does not alter remaining 7 bytes
SetX Condition Description
sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
setns ~SF Nonnegative
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 Perspective, Third Edition 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 Perspective, Third Edition 8
Carnegie Mellon

Reading Condition Codes (Cont.)


 SetX Instructions:
 Set single byte based on combination of condition
codes
 One of addressable byte registers
 Does not alter remaining bytes
 Typically use movzbl to finish job
 32-bit instructions also set upper 32 bits to 0
Register Use(s)
int
int gt
gt (long
(long x,
x, long
long y)
y)
{ %rdi Argument x
{
return
return x
x >
> y;
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 Perspective, Third Edition 9
Carnegie Mellon

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

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


Carnegie Mellon

Jumping
 jX Instructions
 Jump to different part of code depending on condition codes

jX Condition Description
jmp 1 Unconditional
je ZF Equal / Zero
jne ~ZF Not Equal / Not Zero
js SF Negative
jns ~SF Nonnegative
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 Perspective, Third Edition 11


Carnegie Mellon

Conditional Branch Example (Old Style)


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

Expressing with Goto Code


 C allows goto statement
 Jump to position designated by label

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

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


Carnegie Mellon

General Conditional Expression


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

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

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

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


Carnegie Mellon

Using Conditional Moves


 Conditional Move Instructions
 Instruction 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;
Else_Expr;
 Branches are very disruptive to
instruction flow through pipelines
Goto Version
 Conditional moves do not require control result
result = Then_Expr;
= Then_Expr
transfer eval
eval =
= Else_Expr;
Else_Expr;
nt
nt =
= !Test;
!Test;
if
if (nt)
(nt) result
result = = eval;
eval;
return
return result;
result;

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


Carnegie Mellon

Conditional Move Example


long
long absdiff
absdiff
(long
(long x,x, long
long y)
y)
{
{ Register Use(s)
long
long result;
result; %rdi Argument x
if
if (x
(x >> y)
y)
result
result == x-y;
x-y; %rsi Argument y
else
else %rax Return value
result
result == y-x;
y-x;
return
return result;
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 Perspective, Third Edition 16
Carnegie Mellon

Bad Cases for Conditional Move


Expensive Computations
val
val =
= Test(x)
Test(x) ?
? Hard1(x)
Hard1(x) :
: Hard2(x);
Hard2(x);
 Both values get computed
 Only makes sense when computations
are very simple
Risky Computations
val
val =
= p
p ?
? *p
*p :
: 0;
0;
 Both values get computed
 May have undesirable effects
Computations with side effects
val
val =
= x
x >
> 0
0 ?
? x*=7
x*=7 :
: x+=3;
x+=3;
 Both values get computed
 Must be side-effect free
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17
Carnegie Mellon

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

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


Carnegie Mellon

“Do-While” Loop Example


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

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


 Use conditional branch to either continue looping or to exit

loop

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


Carnegie Mellon

“Do-While” Loop Compilation


Goto Version
long
long pcount_goto
pcount_goto
(unsigned
(unsigned long
long x)
x) {{ Register Use(s)
long
long result
result =
= 0;
0;
loop: %rdi Argument x
loop:
result
result +=
+= x
x &
& 0x1;
0x1; %rax result
x
x >>=
>>= 1;
1;
if(x)
if(x) goto
goto loop;
loop;
return
return result;
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 Perspective, Third Edition 20
Carnegie Mellon

General “Do-While” Translation


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 Perspective, Third Edition 21


Carnegie Mellon

General “While” Translation #1


 “Jump-to-middle” translation
 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 Perspective, Third Edition 22


Carnegie Mellon

While Loop Example #1


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

 Compare to do-while version of function


 Initial goto starts loop at test

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


Carnegie Mellon

General “While” Translation #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 Perspective, Third Edition 24
Carnegie Mellon

While Loop Example #2


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

 Compare to do-while version of function


 Initial conditional guards entrance to loop

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


Carnegie Mellon

“For” Loop Form Init


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

#define
#define WSIZE WSIZE 8*sizeof(int)
8*sizeof(int) Update
long
long pcount_for
pcount_for i++
i++
(unsigned
(unsigned long long x) x)
{
{
size_t
size_t i; i; Body
long
long result result = = 0; 0; {
{
for
for (i (i = = 0; 0; i i < < WSIZE;
WSIZE; i++) i++) unsigned
unsigned bit
bit ==
{
{ (x
(x >>
>> i)
i) &
& 0x1;
0x1;
unsigned
unsigned bit bit = = result
result +=
+= bit;
bit;
(x
(x >> >> i) i) & & 0x1;
0x1; }
}
result
result += += bit; bit;
}
}
return
return result; result;
}
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 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 Perspective, Third Edition 27
Carnegie Mellon

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

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


Carnegie Mellon

“For” Loop Do-While Conversion


Goto Version
C Code long
long pcount_for_goto_dw
pcount_for_goto_dw
(unsigned
(unsigned long
long x)
x) {
{
long
long pcount_for
pcount_for size_t
size_t i;i;
(unsigned
(unsigned long
long x)
x) long
long result
result = = 0;
0;
{
{ i
i == 0;
0; Init
size_t
size_t i;
i; if
if (!(i
(!(i << WSIZE))
WSIZE))
long
long result
result == 0;
0; goto
goto done;
done; !Test
for
for (i
(i =
= 0;
0; i
i << WSIZE;
WSIZE; i++)
i++) loop:
loop:
{
{ {
{
unsigned
unsigned bit
bit == unsigned
unsigned bit
bit ==
(x
(x >>
>> i)
i) && 0x1; (x Body
0x1; (x >>
>> i)
i) &
& 0x1;
0x1;
result
result +=
+= bit;
bit; result
result +=+= bit;
bit;
}
} }
}
return
return result;
result; i++; Update
i++;
}
} if
if (i
(i << WSIZE)
WSIZE) Test
goto
goto loop;
loop;
 Initial test can be optimized done:
done:
away return
return result;
result;
}
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29
Carnegie Mellon

Today

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


Carnegie Mellon

long
long switch_eg
switch_eg
(long
(long x,x, long
long y,
y, long
long z)
z)
Switch Statement
{{
long
long ww == 1;
1;
Example
switch(x)
switch(x) {{
case
case 1:
1:
ww == y*z;
y*z;
 Multiple case labels
break;
break;  Here: 5 & 6
case
case 2:
2:
ww == y/z;
y/z;
 Fall through cases
/*
/* Fall
Fall Through
Through */
*/  Here: 2
case
case 3:
3:
ww +=
+= z;
z;
 Missing cases
break;
break;  Here: 4
case
case 5:
5:
case
case 6:
6:
ww -=
-= z;
z;
break;
break;
default:
default:
ww == 2;
2;
}}
return
return w;w;
}}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 31
Carnegie Mellon

Jump Table Structure


Switch Form Jump Table Jump Targets
switch(x)
switch(x){{ jtab: Targ0
Targ0: Code Block
case
caseval_0:
val_0: 0
Block Targ1
Block00
case
caseval_1:
val_1: Targ2 Targ1: Code Block
Block
Block11 •
• • • 1
• • • •
case
caseval_n-1:
val_n-1: •
Block
Blockn–1n–1 Targ2: Code Block
}} Targn-1
2


Translation (Extended C) •
goto
goto*JTab[x];
*JTab[x]; •

Targn-1: Code Block


n–1
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32
Carnegie Mellon

Switch Statement Example


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

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

Switch Statement Example


long
long switch_eg(long
switch_eg(long x,
x, long
long y,
y, long
long z)
z)
{{
long
long ww == 1; 1;
switch(x)
switch(x) {{ Jump table
.. .. ..
.section
.section .rodata
.rodata
}} .align 8
.align 8
return
return w; w; .L4:
.L4:
}} .quad
.quad .L8
.L8 ## xx == 00
.quad
.quad .L3
.L3 ## xx == 11
.quad .L5
.L5 ## xx == 22
Setup: .quad
.quad .L9
.quad .L9 ## xx == 33
.quad
.quad .L8
.L8 ## xx == 44
switch_eg: .quad .L7
.quad .L7 ## xx == 55
movq %rdx, %rcx .quad
.quad .L7
.L7 ## xx == 66
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 Perspective, Third Edition 34


Carnegie Mellon

Assembly Setup Explanation


 Table Structure Jump table
 Each target requires 8 bytes
.section
.section .rodata
.rodata
 Base address at .L4 .align 8
.align 8
.L4:
.L4:
.quad
.quad .L8
.L8 ## xx == 00
.quad
.quad .L3
.L3 ## xx == 11
 Jumping .quad
.quad .L5
.L5 ## xx == 22
.quad
.quad .L9
.L9 ## xx == 33
 Direct: jmp .L8 .quad
.quad .L8
.L8 ## xx == 44
.quad .L7
.L7 ## xx == 55
 Jump target is denoted by label .L8 .quad
.quad .L7
.quad .L7 ## xx == 66

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


 Start of jump table: .L4
 Must scale by factor of 8 (addresses are 8 bytes)
 Fetch target from effective Address .L4 + x*8
 Only for 0 ≤ x ≤ 6
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35
Carnegie Mellon

Jump Table
Jump table
switch(x)
switch(x) {{
.section
.section .rodata
.rodata
case
case 1:
1: //
// .L3
.L3
.align 8
.align 8 ww == y*z;
y*z;
.L4:
.L4: break;
.quad .L8
break;
.quad .L8 ## xx == 00 case
.quad
.quad .L3
.L3 ## xx == 11 case 2:
2: //
// .L5
.L5
.quad
.quad .L5
.L5 ## xx == 22 ww == y/z;
y/z;
.quad
.quad .L9
.L9 ## xx == 33 /*
/* Fall
Fall Through
Through */
*/
.quad
.quad .L8
.L8 ## xx == 44
.quad .L7
case
case 3:
3: //
// .L9
.L9
.quad .L7 ## xx == 55
.quad
.quad .L7
.L7 ## xx == 66
ww +=
+= z;
z;
break;
break;
case
case 5:
5:
case
case 6:
6: //
// .L7
.L7
ww -=
-= z;
z;
break;
break;
default:
default: //
// .L8
.L8
ww == 2;
2;
}}

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


Carnegie Mellon

Code Blocks (x == 1)
switch(x)
switch(x) {{ .L3:
.L3:
case
case 1: 1: //
// .L3
.L3 movq
movq %rsi,
%rsi, %rax
%rax ## yy
ww == y*z;
y*z; imulq
imulq %rdx,
%rdx, %rax
%rax ## y*z
y*z
break;
break; ret
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 Perspective, Third Edition 37


Carnegie Mellon

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

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


Carnegie Mellon

Code Blocks (x == 2, x == 3)
.L5:
.L5: ## Case
Case 22
long
long ww == 1;
1; movq
movq %rsi,
%rsi, %rax
%rax
.. .. .. cqto
cqto
switch(x)
switch(x) {{ idivq
idivq %rcx
%rcx ## y/z
y/z
.. .. .. jmp
jmp .L6
.L6 ## goto
goto merge
merge
case
case 2:2: .L9:
.L9: ## Case
Case 33
ww == y/z;
y/z; movl
movl $1,
$1, %eax
%eax ## ww == 11
/* Fall Through */
/* Fall Through */ .L6:
.L6: ## merge:
merge:
case
case 3:3: addq
addq %rcx,
%rcx, %rax
%rax ## ww +=
+= zz
ww +=
+= z;
z; ret
ret
break;
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 Perspective, Third Edition 39


Carnegie Mellon

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


switch(x)
switch(x) {{ .L7:
.L7: ## Case
Case 5,65,6
.. .. .. movl
movl $1,
$1, %eax
%eax ## ww == 11
case
case 5: 5: //
// .L7
.L7 subq
subq %rdx,
%rdx, %rax
%rax ## ww -=
-= zz
case
case 6: 6: //
// .L7
.L7 ret
ret
ww -=
-= z;
z; .L8:
.L8: ## Default:
Default:
break;
break; movl
movl $2,
$2, %eax
%eax ## 22
default:
default: // // .L8
.L8 ret
ret
ww == 2;
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 Perspective, Third Edition 40


Carnegie Mellon

Summarizing
 C Control
 if-then-else
 do-while
 while, for
 switch
 Assembler Control
 Conditional jump
 Conditional 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 Perspective, Third Edition 41
Carnegie Mellon

Summary
 Today
 Control: Condition codes
 Conditional branches & conditional moves
 Loops
 Switch statements
 Next Time
 Stack
 Call / return
 Procedure call discipline

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

You might also like