06-machine-control
06-machine-control
CarnegieMellon
Mellon
Instructors:
Randal
E.
Bryant
and
David
R.
O’Hallaron
Today
¢ Control:
Condi�on
codes
¢ Condi�onal
branches
¢ Loops
¢ Switch Statements
¢ 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)
§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)
§Sets
condi�on
codes
based
on
value
of
Src1
&
Src2
§Useful
to
have
one
of
the
operands
be
a
mask
Today
¢ Control:
Condi�on
codes
¢ Condi�onal
branches
¢ Loops
¢ Switch Statements
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)
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:
. . .
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
Today
¢ Control:
Condi�on
codes
¢ Condi�onal
branches
¢ Loops
¢ Switch Statements
loop
¢ “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:
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
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;
}
Today
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
@
Transla�on
(Extended
C)
@
goto *JTab[x]; @
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
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;
}
Register
Use(s)
%rdi Argument
x
%rsi Argument
y
%rdx Argument
z
%rax Return
value
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;
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