Download presentation
Presentation is loading. Please wait.
Published byMoris Washington Modified over 9 years ago
1
1 Machine-Level Programming IV: Control: loops Comp 21000: Introduction to Computer Organization & Systems March 2015 Systems book chapter 3* * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron, 2011
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 Registers: %edxx %ecxresult 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 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; }
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 C CodeGoto Version “While” Loop Example Is this code equivalent to the do-while version? Must jump out of loop if test fails int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; } int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; } int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }
7
7 AssemblyGoto Version “While” Loop Example Note that the jmp instruction goes to the test, it doesn’t implement the initial if statement itself. _pcount_do: 00001d30pushl%ebp 00001d31movl%esp, %ebp 00001d33subl$16, %esp 00001d36movl8(%ebp), %eax 00001d39movl%eax, -4(%ebp) 00001d3cmovl$0, -16(%ebp) 00001d43jmp0x1d5e 00001d45movl-4(%ebp), %eax 00001d48andl$1, %eax 00001d4bmovl-16(%ebp), %ecx 00001d4eaddl%ecx, %eax 00001d50movl%eax, -16(%ebp) 00001d53movl-4(%ebp), %eax 00001d56shrl%eax 00001d5ecmpl$0, %eax 00001d61jne0x1d45 00001d63movl-16(%ebp), %eax 00001d72addl$16, %esp 00001d75popl%ebp 00001d76ret _pcount_do: 00001d30pushl%ebp 00001d31movl%esp, %ebp 00001d33subl$16, %esp 00001d36movl8(%ebp), %eax 00001d39movl%eax, -4(%ebp) 00001d3cmovl$0, -16(%ebp) 00001d43jmp0x1d5e 00001d45movl-4(%ebp), %eax 00001d48andl$1, %eax 00001d4bmovl-16(%ebp), %ecx 00001d4eaddl%ecx, %eax 00001d50movl%eax, -16(%ebp) 00001d53movl-4(%ebp), %eax 00001d56shrl%eax 00001d5ecmpl$0, %eax 00001d61jne0x1d45 00001d63movl-16(%ebp), %eax 00001d72addl$16, %esp 00001d75popl%ebp 00001d76ret int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; } int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if (x) goto loop; done: return result; }
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 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 “For” Loop Example Is this code equivalent to other versions? #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; }
10
10 “For” Loop Form for ( Init ; Test ; Update ) Body General Form for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } i = 0 i < WSIZE i++ { unsigned mask = 1 << i; result += (x & mask) != 0; } { unsigned mask = 1 << i; result += (x & mask) != 0; } Init Test Update Body
11
11 “For” Loop While Loop for ( Init ; Test ; Update ) Body For Version Init ; while ( Test ) { Body Update ; } While Version
12
12 “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:
13
13 C Code “For” Loop Conversion Example Initial test can be optimized away #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } #define WSIZE 8*sizeof(int) int pcount_for(unsigned x) { int i; int result = 0; for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } return result; } Goto Version int pcount_for_gt(unsigned x) { int i; 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; } int pcount_for_gt(unsigned x) { int i; 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; } Init ! Test Body Update Test What if WSIZE < 0? Then compiler will recognize that the loop will never run and will eliminate code
14
14 “For” Loop Conversion Example Redundant instructions have been removed, so addresses are not correct. Assembly Version 00001d43cmpl$0, %eax 00001d46setne%al 00001d49andb$1, %al 00001d4bmovzbl%al, %eax 00001d4emovl-20(%ebp), %edx 00001d51addl%edx, %eax 00001d53movl%eax, -20(%ebp) 00001d56movl-16(%ebp), %eax 00001d59addl$1, %eax 00001d62cmpl$31, %eax 00001d65jbe0x1d2c 00001d67movl-20(%ebp), %eax 00001d76addl$24, %esp 00001d79popl%ebp 00001d7aret 00001d43cmpl$0, %eax 00001d46setne%al 00001d49andb$1, %al 00001d4bmovzbl%al, %eax 00001d4emovl-20(%ebp), %edx 00001d51addl%edx, %eax 00001d53movl%eax, -20(%ebp) 00001d56movl-16(%ebp), %eax 00001d59addl$1, %eax 00001d62cmpl$31, %eax 00001d65jbe0x1d2c 00001d67movl-20(%ebp), %eax 00001d76addl$24, %esp 00001d79popl%ebp 00001d7aret _pcount_for: 00001d10pushl%ebp 00001d11movl%esp, %ebp 00001d13subl$24, %esp 00001d16movl8(%ebp), %eax 00001d19movl%eax, -4(%ebp) 00001d1cmovl$0, -20(%ebp) 00001d23movl$0, -16(%ebp) 00001d2ajmp0x1d5f 00001d2cmovl-16(%ebp), %eax 00001d2fmovl%eax, %ecx 00001d31movl$1, %eax 00001d36shll%cl, %eax 00001d38movl%eax, -24(%ebp) 00001d3bmovl-4(%ebp), %eax 00001d3emovl-24(%ebp), %edx 00001d41andl%edx, %eax _pcount_for: 00001d10pushl%ebp 00001d11movl%esp, %ebp 00001d13subl$24, %esp 00001d16movl8(%ebp), %eax 00001d19movl%eax, -4(%ebp) 00001d1cmovl$0, -20(%ebp) 00001d23movl$0, -16(%ebp) 00001d2ajmp0x1d5f 00001d2cmovl-16(%ebp), %eax 00001d2fmovl%eax, %ecx 00001d31movl$1, %eax 00001d36shll%cl, %eax 00001d38movl%eax, -24(%ebp) 00001d3bmovl-4(%ebp), %eax 00001d3emovl-24(%ebp), %edx 00001d41andl%edx, %eax
15
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.