0% found this document useful (0 votes)
7 views11 pages

Lecture Slides 04 046-X86-Loops

The document provides a detailed explanation of compiling loops from C/Java code to machine code, including examples of 'do-while', 'while', and 'for' loops. It illustrates how to translate these loops into assembly language using backward branches and conditional jumps. Additionally, it discusses the algorithmic complexity of certain operations, specifically in the context of exponentiation using bit manipulation.

Uploaded by

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

Lecture Slides 04 046-X86-Loops

The document provides a detailed explanation of compiling loops from C/Java code to machine code, including examples of 'do-while', 'while', and 'for' loops. It illustrates how to translate these loops into assembly language using backward branches and conditional jumps. Additionally, it discusses the algorithmic complexity of certain operations, specifically in the context of exponentiation using bit manipulation.

Uploaded by

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

University

 of  Washington  

Compiling  Loops  
C/Java  code:   Machine  code:  
while ( sum != 0 ) { loopTop: cmpl $0, %eax
<loop body> je loopDone
} <loop body code>
jmp loopTop
loopDone:

¢ How  to  compile  other  loops  should  be  straigh<orward  


§ 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   Goto  Version  
 int fact_do(int x)  int fact_goto(int x)
{ {
int result = 1; int result = 1;
do { loop:
result *= x; result *= x;
x = x-1; x = x-1;
} while (x > 1); if (x > 1) goto loop;
return result; return result;
} }

¢ Use  backward  branch  to  conUnue  looping  


¢ Only  take  branch  when  “while”  condiUon  holds  

x86  
University  of  Washington  

“Do-­‐While”  Loop  CompilaUon   Registers:  


%edx x
Goto  Version   Assembly   %eax result
int  fact_goto:
fact_goto(int x) pushl %ebp # Setup
{ movl %esp,%ebp # Setup
int result = 1; movl $1,%eax # eax = 1
movl 8(%ebp),%edx # edx = x

loop: .L11:
result *= x; imull %edx,%eax # result *= x
x = x-1; decl %edx # x--
if (x > 1) cmpl $1,%edx # Compare x : 1
goto loop; jg .L11 # if > goto loop

return result; movl %ebp,%esp # Finish


} popl %ebp # Finish
ret # Finish

x86  
University  of  Washington  

General  “Do-­‐While”  TranslaUon  


C  Code   Goto  Version  
do loop:
Body   Body  
while (Test); if (Test)
goto loop

¢ Body:   {
Statement1;  
  Statement2;  
…  
 
Statementn;  
  }
 
¢ Test  returns  integer  
=  0  interpreted  as  false    
≠  0  interpreted  as  true  
x86  
University  of  Washington  

“While”  Loop  TranslaUon  


C  Code   Goto  Version  
 int fact_while(int x) int fact_while_goto(int x)
{ {
int result = 1; int result = 1;
while (x > 1) { goto middle;
result *= x; loop:
x = x-1; result *= x;
}; x = x-1;
return result; 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) # x in %edx, result in %eax
{ jmp .L34 # goto Middle
int result = 1; .L35: # Loop:
while (x > 1) { imull %edx, %eax # result *= x
result *= x; decl %edx # x--
x--; .L34: # Middle:
}; cmpl $1, %edx # x:1
return result; 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

Example  
zi = 1    when  pi = 0

n–1    Umes   310  =  32  *  38  
zi = x when  pi = 1  
§ Complexity  O(log p)
 =  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   result x=3 p=10


1   1 3 10=10102
2   1 9 5= 1012
3   9 81 2= 102
4   9 6561 1= 12
5   59049 43046721 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   Test   Update   Body  


result = 1 p != 0 p = p >> 1 {
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:
While  Version   Body
  Init; Update  ;
while (Test  ) { middle:
Body   if (Test)
Update  ;   goto loop;
} done:

x86  
University  of  Washington  

For-­‐Loop:  CompilaUon  
for (result = 1; p != 0; p = p>>1)
For  Version  
{
  for (Init; Test; Update  ) if (p & 0x1)
result *= x;
Body   x = x*x;
}

Goto  Version  
  Init; result = 1;
goto middle;
goto middle; loop:
loop: if (p & 0x1)
Body result *= x;
Update  ; x = x*x;
middle: p = p >> 1;
if (Test) middle:
if (p != 0)
goto loop;
goto loop;
done: done:
x86  

You might also like