CS 312 Lecture - 7b - Machine Level ProgrammingII-control
CS 312 Lecture - 7b - Machine Level ProgrammingII-control
CarnegieMellon
Mellon
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
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)
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
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)
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;
}
}
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:
.
. .. .
.
Today
Control: Condition codes
Conditional branches
Loops
Switch Statements
loop
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
#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
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;
}
}
Today
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
•
Translation (Extended C) •
goto
goto*JTab[x];
*JTab[x]; •
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
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;
}}
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
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;
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
Register Use(s)
%rdi Argument x
%rsi Argument y
%rdx Argument z
%rax Return value
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