Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified.

Similar presentations


Presentation on theme: "1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified."— Presentation transcript:

1 1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective, 3 rd ed.”, Randy Bryant & David O’Hallaron, 2015

2 2 Today Complete addressing mode, address computation (leal) Arithmetic operations x86-64 Control: Condition codes Conditional branches and moves Loops

3 3 C Code int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } int pcount_do(unsigned x) { int result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; } Goto Version int pcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } int pcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } “Do-While” Loop Example Count number of 1’s in argument x (“popcount”) Use conditional branch to either continue looping or to exit loop

4 4 Goto Version “Do-While” Loop Compilation movl $0, %eax# result = 0.L2:# loop: movq %rdi, %rdx andl $1, %edx# t = x & 0x1 addq %rdx, %rax# result += t shrq %rdi# x >>= 1 jne.L2# if (x) goto loop rep; ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } RegisterUse(s) %rdi Argument x %raxresult

5 5 C Code do Body while ( Test ); do Body while ( Test ); Goto Version loop: Body if ( Test ) goto loop loop: Body if ( Test ) goto loop General “Do-While” Translation Body: Test returns integer  = 0 interpreted as false  ≠ 0 interpreted as true { Statement 1 ; Statement 2 ; … Statement n ; }

6 6 While version while ( Test ) Body while ( Test ) Body General “While” Translation #1 “Jump-to-middle” translation Used with -Og Goto Version goto test; loop: Body test: if ( Test ) goto loop; done: goto test; loop: Body test: if ( Test ) goto loop; done:

7 7 C Code long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } Jump to Middle Version long pcount_goto_jtm (unsigned long x) { long result = 0; goto test; loop: result += x & 0x1; x >>= 1; test: if(x) goto loop; return result; } long pcount_goto_jtm (unsigned long x) { long result = 0; goto test; loop: result += x & 0x1; x >>= 1; test: if(x) goto loop; return result; } While Loop Example #1 Compare to do-while version of function Initial goto starts loop at test

8 8 While version while ( Test ) Body while ( Test ) Body Do-While Version if (! Test ) goto done; do Body while( Test ); done: if (! Test ) goto done; do Body while( Test ); done: General “While” Translation #2 “Do-while” conversion Used with –O1 Goto Version if (! Test ) goto done; loop: Body if ( Test ) goto loop; done: if (! Test ) goto done; loop: Body if ( Test ) goto loop; done:

9 9 C Code long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } Do-While Version long pcount_goto_dw (unsigned long x) { long result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if(x) goto loop; done: return result; } long pcount_goto_dw (unsigned long x) { long result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if(x) goto loop; done: return result; } While Loop Example #2 Compare to do-while version of function Initial conditional guards entrance to loop

10 10 C Code “For” Loop Example Is this code equivalent to other versions? #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; }

11 11 “For” Loop Form for ( Init ; Test ; Update ) Body General Form for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit;} { unsigned bit = (x >> i) & 0x1; result += bit;} Init Test Update Body

12 12 “For” Loop  While Loop for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version

13 13 For-While Conversion long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { unsigned bit = (x >> i) & 0x1; result += bit; i++; } return result; } long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { unsigned bit = (x >> i) & 0x1; result += bit; i++; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; } { unsigned bit = (x >> i) & 0x1; result += bit; } Init Test Update Body

14 14 “For” Loop  …  Goto for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version Init ; if (! Test ) goto done; do Body Update while( Test ); done: Init ; if (! Test ) goto done; do Body Update while( Test ); done: Init ; if (! Test ) goto done; loop: Body Update if ( Test ) goto loop; done: Init ; if (! Test ) goto done; loop: Body Update if ( Test ) goto loop; done:

15 15 C Code “For” Loop Do-While Conversion Initial test can be optimized away long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } Goto Version long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } Init ! Test Body Update Test

16 16 C Code “For” Loop Do-While Conversion Initial test can be optimized away long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } Goto Version long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; } Init ! Test Body Update Test What if WSIZE < 0? Then compiler will recognize that the loop will never run and will eliminate code

17 17 “For” Loop Conversion Example Assembly Version mov %eax,-0xc(%rbp) mov -0xc(%rbp),%eax add %rax,-0x8(%rbp) addl $0x1,-0x10(%rbp) mov -0x10(%rbp),%eax cmp $0x1f,%eax jbe 0x400545 mov -0x8(%rbp),%rax pop %rbp retq mov %eax,-0xc(%rbp) mov -0xc(%rbp),%eax add %rax,-0x8(%rbp) addl $0x1,-0x10(%rbp) mov -0x10(%rbp),%eax cmp $0x1f,%eax jbe 0x400545 mov -0x8(%rbp),%rax pop %rbp retq _pcount_for: push %rbp mov %rsp,%rbp mov %edi,-0x14(%rbp) movq $0x0,-0x8(%rbp) movl $0x0,-0x10(%rbp) jmp 0x400562 mov -0x10(%rbp),%eax mov -0x14(%rbp),%edx mov %eax,%ecx shr %cl,%edx mov %edx,%eax and $0x1,%eax _pcount_for: push %rbp mov %rsp,%rbp mov %edi,-0x14(%rbp) movq $0x0,-0x8(%rbp) movl $0x0,-0x10(%rbp) jmp 0x400562 mov -0x10(%rbp),%eax mov -0x14(%rbp),%edx mov %eax,%ecx shr %cl,%edx mov %edx,%eax and $0x1,%eax

18 18 Summary Today  Complete addressing mode, address computation ( leal )  Arithmetic operations  Control: Condition codes  Conditional branches & conditional moves  Loops Next Time  Switch statements  Stack  Call / return  Procedure call discipline


Download ppt "1 Machine-Level Programming V: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2016 Systems book chapter 3* * Modified."

Similar presentations


Ads by Google