1 / 15

Today

Machine-Level Programming II : Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2014 Systems book chapter 3*. * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron , 2011. Today.

zuzela
Download Presentation

Today

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Machine-Level Programming II: Control: loopsComp 21000: Introduction to Computer Organization & SystemsMarch 2014Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron, 2011

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

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

  4. “Do-While” Loop Compilation Goto Version • Registers: %edxx %ecxresult intpcount_do(unsigned x){ int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } movl $0, %ecx # result = 0 .L2: # loop: movl %edx, %eax andl $1, %eax # t = x & 1 addl %eax, %ecx # result += t shrl %edx # x >>= 1 jne .L2 # If !0, goto loop

  5. General “Do-While” Translation Goto Version C Code • Body: • Test returns integer • = 0 interpreted as false • ≠ 0 interpreted as true loop: Body if (Test) gotoloop do Body while (Test); { Statement1; Statement2; … Statementn; }

  6. “While” Loop Example C Code GotoVersion • Is this code equivalent to the do-while version? • Must jump out of loop if test fails intpcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } intpcount_do(unsigned x){ int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }

  7. “While” Loop Example GotoVersion Assembly intpcount_do(unsigned x){ int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; } _pcount_do: 00001d30 pushl %ebp 00001d31 movl %esp, %ebp 00001d33 subl $16, %esp 00001d36 movl 8(%ebp), %eax 00001d39 movl %eax, -4(%ebp) 00001d3c movl $0, -16(%ebp) 00001d43 jmp 0x1d5b 00001d45 movl -4(%ebp), %eax 00001d48 andl $1, %eax 00001d4b movl -16(%ebp), %ecx 00001d4e addl %ecx, %eax 00001d50 movl %eax, -16(%ebp) 00001d53 movl -4(%ebp), %eax 00001d56 shrl %eax 00001d5ecmpl $0, %eax 00001d61 jne 0x1d45 00001d63 movl -16(%ebp), %eax 00001d72addl $16, %esp 00001d75 popl %ebp 00001d76 ret Note that some redundant instructions have been removed so the memory addresses don’t quite work.

  8. General “While” Translation While version while (Test) Body Goto Version Do-While Version if (!Test) gotodone; loop: Body if (Test) gotoloop; done: if (!Test) gotodone; do Body while(Test); done:

  9. Is this code equivalent to other versions? “For” Loop Example C Code #define WSIZE 8*sizeof(int) intpcount_for(unsigned x) { inti; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; }

  10. “For” Loop Form Init i = 0 General Form Test for (Init; Test; Update ) Body i < WSIZE Update i++ for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } Body { unsigned mask = 1 << i; result += (x & mask) != 0; }

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

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

  13. Initial test can be optimized away “For” Loop Conversion Example Goto Version C Code intpcount_for_gt(unsigned x) { inti; int result = 0; i = 0; if (!(i < WSIZE)) goto done; loop: { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; if (i < WSIZE) goto loop; done: return result; } #define WSIZE 8*sizeof(int) intpcount_for(unsigned x) { inti; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } Init !Test Body Update Test

  14. Redundant instructions have been removed, so addresses are not correct. “For” Loop Conversion Example Assembly Version _pcount_for: 00001d10 pushl %ebp 00001d11 movl %esp, %ebp 00001d13 subl $24, %esp 00001d16 movl 8(%ebp), %eax 00001d19 movl %eax, -4(%ebp) 00001d1c movl $0, -20(%ebp) 00001d23 movl $0, -16(%ebp) 00001d2a jmp 0x1d5f 00001d2c movl -16(%ebp), %eax 00001d2f movl %eax, %ecx 00001d31 movl $1, %eax 00001d36 shll %cl, %eax 00001d38 movl %eax, -24(%ebp) 00001d3b movl -4(%ebp), %eax 00001d3e movl -24(%ebp), %edx 00001d41 andl %edx, %eax 00001d43cmpl $0, %eax 00001d46 setne %al 00001d49 andb $1, %al 00001d4b movzbl %al, %eax 00001d4e movl -20(%ebp), %edx 00001d51 addl %edx, %eax 00001d53 movl %eax, -20(%ebp) 00001d56 movl -16(%ebp), %eax 00001d59 addl $1, %eax 00001d62cmpl $31, %eax 00001d65 jbe 0x1d2c 00001d67 movl -20(%ebp), %eax 00001d76addl $24, %esp 00001d79 popl %ebp 00001d7a ret

  15. 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

More Related