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

06 Machine Control

Uploaded by

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

06 Machine Control

Uploaded by

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

Carnegie

CarnegieMellon
Mello

Machine-Level Programming
II: Control
15-213: Introduction to Computer Systems
6th Lecture, Sep. 17, 2015

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

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


Carnegie Mello

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

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


Carnegie Mello

Processor State (x86-64,


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

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 Mello

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 Mello

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 Mello

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)
Greater or Equal
setge ~(SF^OF)
(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 Mello

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
 Register Use(s)
32-bit
int
int gtinstructions
gt (long
(long x,x,also set upper
long
long y)
y) 32 bits to 0
{ %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 Mello

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

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


Carnegie Mello

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 Mello

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 Mello

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 Mello

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  Execute appropriate one
goto Done;
Done;
Else:
Else:
val
val == Else_Expr;
Else_Expr;
Done:
Done:
.
. .. .
.

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


Carnegie Mello

Using Conditional Moves


 Conditional Move
Instructions
 Instruction supports:
if (Test) Dest  Src C Code
 Supported in post-1995 x86 processors val = Test
 GCC tries to use them ? Then_Expr
 But, only when known to be safe : Else_Expr;
 Why?
Goto Version
 Branches are very disruptive to
instruction flow through pipelines result
result = Then_Expr;
= Then_Expr
 Conditional moves do not require control eval = Else_Expr;
eval = Else_Expr;
nt
nt =
= !Test;
!Test;
transfer 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 Mello

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 Mello

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 Mello

Today
 Control: Condition codes
 Conditional branches

 Loops

 Switch Statements

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


Carnegie Mello

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

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

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 Mello

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 Mello

While Loop Example #1


C Code Jump to
long
long pcount_while
pcount_while Middle
long
long pcount_goto_jtm
pcount_goto_jtm
(unsigned
(unsigned long
long x)
x) { (unsigned
(unsigned long
long x)
x) {
{ Version {
long
long result
result = = 0;
0; long
long result
result =
= 0;
0;
while
while (x)
(x) { { goto
goto test;
test;
result
result +=
+= xx &
& 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 Mello

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 Mello

While Loop Example #2


C Code Do-While
long
long pcount_while
pcount_while Version
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 +=
+= xx &
& 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 Mello

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

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

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 bit
bit ==
(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 Mello

“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 done:
done:
optimized away return
return result;
result;
}
}
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29
Carnegie Mello

Today

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


Carnegie Mello

long
long switch_eg
switch_eg
Switch
{{
(long
(long x,x, long
long y,
y, long
long z)
z) Statement
long
long ww == 1;
switch(x)
switch(x) {{
1; Example
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 Mello

Jump Table Structure


Switch Form Jump Table Jump Targets
switch(x)
switch(x) {{ jtab: Targ0
Targ0: Code Block
case
case val_0:
val_0: 0
Block Targ1
Block 00
case
case val_1:
val_1: Targ2 Targ1: Code Block
Block
Block 11 •
•• •• •• 1

case
case val_n-1:
val_n-1: •
Block
Block n–1
n–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 Mello

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 Note that w not
values takes
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
initialized here 33
Carnegie Mello

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
.align 88
return
return w; w; .L4:
.L4:
}} .quad
.quad .L8
.L8 ## xx == 00
.quad
.quad .L3
.L3 ## xx == 11
.quad .L5 ## xx == 22
Setup: .quad
.quad
.L5
.L9 ## xx == 33
.quad .L9
.quad
.quad .L8
.L8 ## xx == 44
switch_eg: .quad .L7 ## xx == 55
.quad .L7
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 Mello

Assembly Setup Explanation


 Table Structure Jump table
 Each target requires 8 bytes
.section .rodata
 Base address at .L4 .section
.align
.align 88
.rodata

.L4:
.L4:
.quad
.quad .L8
.L8 ## xx == 00
.quad
.quad .L3
.L3 ## xx == 11
 Jumping .quad
.quad .L5
.L5 ## xx == 22
.quad .L9 ## xx == 33
 Direct: jmp .L8 .quad
.quad
.quad
.L9
.L8
.L8 ## xx == 44
 Jump target is denoted by label .L8 .quad
.quad
.quad
.L7
.L7
.L7
## xx == 55
## xx == 66
.quad .L7

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

Jump Table
Jump table
switch(x)
switch(x) {{
.section
.section .rodata
.rodata
case
case 1:
1: //
// .L3
.L3
.align
.align 88 ww == y*z;
y*z;
.L4:
.L4: break;
.quad .L8 ## xx == 00
break;
.quad .L8 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
case
case 3:
3: //
// .L9
.L9
.quad
.quad .L7
.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 Mello

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 Mello

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 Mello

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 2:
case 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 Mello

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 Mello

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