1. Trang chủ
  2. » Giáo Dục - Đào Tạo

06 loops 09 11 tủ tài liệu bách khoa

11 46 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 11
Dung lượng 143,86 KB

Nội dung

University  of  Washington   Compiling  Loops   C/Java  code:   while ( sum != ) { } Machine  code:   loopTop: cmpl $0, %eax je loopDone jmp loopTop loopDone: ¢  How  to  compile  other  loops  should  be  straigh 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 = edx = x # # # # result *= x x-Compare x : if > goto loop # Finish # Finish # Finish University  of  Washington   General  “Do-­‐While”  TranslaUon   Goto  Version   C  Code   ¢  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 · (z2 2) · … · (…((zn –12) )…) zi =    when  pi = zi = x when  pi =   §  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             result x=3 p=10 1 9 59049 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 = Test   p != Update   p = p >> 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  

Ngày đăng: 09/11/2019, 06:40