University
of
Washington
Compiling
Loops
C/Java
code:
while ( sum != 0 ) {
<loop body>
}
Machine
code:
loopTop:
cmpl $0, %eax
je
loopDone
<loop body code>
jmp
loopTop
loopDone:
¢
How
to
compile
other
loops
should
be
straigh
§ The
only
slightly
tricky
part
is
to
be
sure
where
the
condi6onal
branch
occurs:
top
or
bo8om
of
the
loop
¢
How
would
for(i=0;
i<100;
i++)
be
implemented?
x86
University
of
Washington
“Do-‐While”
Loop
Example
C
Code
int fact_do(int
Goto
Version
int fact_goto(int
x)
{
{
int result = 1;
do {
result *= x;
x = x-1;
} while (x > 1);
return result;
int result = 1;
loop:
result *= x;
x = x-1;
if (x > 1) goto loop;
return result;
}
}
¢
¢
x)
Use
backward
branch
to
conUnue
looping
Only
take
branch
when
“while”
condiUon
holds
x86
University
of
Washington
“Do-‐While”
Loop
CompilaUon
Goto
Version
int
fact_goto(int x)
{
int result = 1;
loop:
result *= x;
x = x-1;
if (x > 1)
goto loop;
return result;
}
Assembly
fact_goto:
pushl %ebp
movl %esp,%ebp
movl $1,%eax
movl 8(%ebp),%edx
.L11:
imull %edx,%eax
decl %edx
cmpl $1,%edx
jg .L11
movl %ebp,%esp
popl %ebp
ret
x86
Registers:
%edx
%eax
x
result
#
#
#
#
Setup
Setup
eax = 1
edx = x
#
#
#
#
result *= x
x-Compare x : 1
if > goto loop
# Finish
# Finish
# Finish
University
of
Washington
General
“Do-‐While”
TranslaUon
Goto
Version
C
Code
do
¢
Body:
{
¢
Body
while (Test);
}
loop:
Body
if (Test)
goto loop
Statement1;
Statement2;
…
Statementn;
Test
returns
integer
=
0
interpreted
as
false
≠
0
interpreted
as
true
x86
University
of
Washington
“While”
Loop
TranslaUon
C
Code
int fact_while(int
{
int result = 1;
while (x > 1) {
result *= x;
x = x-1;
};
return result;
}
Goto
Version
x)
int fact_while_goto(int x)
{
int result = 1;
goto middle;
loop:
result *= x;
x = x-1;
middle:
if (x > 1)
goto loop;
return result;
}
Used
by
GCC
for
both
IA32
&
x86-‐64
¢ First
iteraUon
jumps
over
body
computaUon
within
loop
straight
to
test
¢
x86
University
of
Washington
“While”
Loop
Example
int fact_while(int x)
{
int result = 1;
while (x > 1) {
result *= x;
x--;
};
return result;
}
# x in %edx, result in %eax
jmp
.L34
#
goto Middle
.L35:
# Loop:
imull %edx, %eax #
result *= x
decl %edx
#
x-.L34:
# Middle:
cmpl $1, %edx
#
x:1
jg
.L35
#
if >, goto
#
Loop
x86
University
of
Washington
“For”
Loop
Example:
Square-‐and-‐MulUply
/* Compute x raised to nonnegative power p */
int ipwr_for(int x, unsigned int p)
{
int result;
for (result = 1; p != 0; p = p>>1) {
if (p & 0x1)
result *= x;
x = x*x;
}
return result;
}
¢
Algorithm
§ Exploit
bit
representa6on:
p = p0 + 2p1 + 22p2 + … 2n–1pn–1
§ Gives:
xp = z0 · z1 2 · (z2 2) 2 · … · (…((zn –12) 2 )…) 2
zi = 1
when
pi = 0
zi = x when
pi = 1
§ Complexity
O(log p)
Example
n–1
Umes
310
=
32
*
38
=
32
*
((32)2)2
x86
University
of
Washington
ipwr
ComputaUon
/* Compute x raised to nonnegative power p */
int ipwr_for(int x, unsigned int p)
{
int result;
for (result = 1; p != 0; p = p>>1) {
if (p & 0x1)
result *= x;
x = x*x;
}
return result;
}
before
iteraUon
1
2
3
4
5
result
x=3
p=10
1
1
9
9
59049
3
9
81
6561
43046721
10=10102
5= 1012
2= 102
1=
12
02
x86
University
of
Washington
“For”
Loop
Example
int result;
for (result = 1; p != 0; p = p>>1)
{
if (p & 0x1)
result *= x;
x = x*x;
}
General
Form
for (Init; Test; Update)
Body
Init
result = 1
Test
p != 0
Update
p = p >> 1
Body
{
if (p & 0x1)
result *= x;
x = x*x;
}
x86
University
of
Washington
“For”→
“While”
For
Version
for (Init; Test; Update
)
Body
Goto
Version
Init;
goto middle;
loop:
Body
Update
;
middle:
if (Test)
goto loop;
done:
While
Version
Init;
while (Test
) {
Body
Update
;
}
x86
University
of
Washington
For-‐Loop:
CompilaUon
For
Version
for (Init; Test; Update
)
Body
for (result = 1; p != 0; p = p>>1)
{
if (p & 0x1)
result *= x;
x = x*x;
}
Goto
Version
Init;
goto middle;
loop:
Body
Update
;
middle:
if (Test)
goto loop;
done:
result = 1;
goto middle;
loop:
if (p & 0x1)
result *= x;
x = x*x;
p = p >> 1;
middle:
if (p != 0)
goto loop;
done:
x86