1 machine-level programming iv: control: loops comp 21000: introduction to computer organization...
TRANSCRIPT
1
Machine-Level Programming IV: Control: loops
Comp 21000: Introduction to Computer Organization & SystemsMarch 2015
Systems 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
C Codeint 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 Versionint 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
Goto Version“Do-While” Loop Compilation
Registers:%edx x%ecx result
movl $0, %ecx # result = 0.L2: # loop:
movl %edx, %eaxandl $1, %eax # t = x & 1addl %eax, %ecx # result += tshrl %edx # x >>= 1jne .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
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
{ Statement1; Statement2; … Statementn;}
6
C Code Goto 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
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:00001d30 pushl %ebp00001d31 movl %esp, %ebp00001d33 subl $16, %esp00001d36 movl 8(%ebp), %eax00001d39 movl %eax, -4(%ebp)00001d3c movl $0, -16(%ebp)00001d43 jmp 0x1d5e00001d45 movl -4(%ebp), %eax00001d48 andl $1, %eax00001d4b movl -16(%ebp), %ecx00001d4e addl %ecx, %eax00001d50 movl %eax, -16(%ebp)00001d53 movl -4(%ebp), %eax00001d56 shrl %eax00001d5e cmpl $0, %eax00001d61 jne 0x1d4500001d63 movl -16(%ebp), %eax00001d72 addl $16, %esp00001d75 popl %ebp00001d76 ret
_pcount_do:00001d30 pushl %ebp00001d31 movl %esp, %ebp00001d33 subl $16, %esp00001d36 movl 8(%ebp), %eax00001d39 movl %eax, -4(%ebp)00001d3c movl $0, -16(%ebp)00001d43 jmp 0x1d5e00001d45 movl -4(%ebp), %eax00001d48 andl $1, %eax00001d4b movl -16(%ebp), %ecx00001d4e addl %ecx, %eax00001d50 movl %eax, -16(%ebp)00001d53 movl -4(%ebp), %eax00001d56 shrl %eax00001d5e cmpl $0, %eax00001d61 jne 0x1d4500001d63 movl -16(%ebp), %eax00001d72 addl $16, %esp00001d75 popl %ebp00001d76 ret
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
While version
while (Test) Bodywhile (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
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
“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 = 0i = 0
i < WSIZEi < WSIZE
i++i++
{ unsigned mask = 1 << i; result += (x & mask) != 0;}
{ unsigned mask = 1 << i; result += (x & mask) != 0;}
Init
Test
Update
Body
11
“For” Loop While Loop
for (Init; Test; Update )
Body
For Version
Init;
while (Test ) {
Body
Update;
}
While Version
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
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
UpdateTest
What if WSIZE < 0?Then compiler will recognize that the loop will never run and will eliminate code
14
“For” Loop Conversion Example
Redundant instructions have been removed, so addresses are not correct.
Assembly Version00001d43 cmpl $0, %eax
00001d46 setne %al00001d49 andb $1, %al00001d4b movzbl %al, %eax00001d4e movl -20(%ebp), %edx00001d51 addl %edx, %eax00001d53 movl %eax, -20(%ebp)00001d56 movl -16(%ebp), %eax00001d59 addl $1, %eax00001d62 cmpl $31, %eax00001d65 jbe 0x1d2c00001d67 movl -20(%ebp), %eax00001d76 addl $24, %esp00001d79 popl %ebp00001d7a ret
00001d43 cmpl $0, %eax00001d46 setne %al00001d49 andb $1, %al00001d4b movzbl %al, %eax00001d4e movl -20(%ebp), %edx00001d51 addl %edx, %eax00001d53 movl %eax, -20(%ebp)00001d56 movl -16(%ebp), %eax00001d59 addl $1, %eax00001d62 cmpl $31, %eax00001d65 jbe 0x1d2c00001d67 movl -20(%ebp), %eax00001d76 addl $24, %esp00001d79 popl %ebp00001d7a ret
_pcount_for:00001d10 pushl %ebp00001d11 movl %esp, %ebp00001d13 subl $24, %esp00001d16 movl 8(%ebp), %eax00001d19 movl %eax, -4(%ebp)00001d1c movl $0, -20(%ebp)00001d23 movl $0, -16(%ebp)00001d2a jmp 0x1d5f00001d2c movl -16(%ebp), %eax00001d2f movl %eax, %ecx00001d31 movl $1, %eax00001d36 shll %cl, %eax00001d38 movl %eax, -24(%ebp)00001d3b movl -4(%ebp), %eax00001d3e movl -24(%ebp), %edx00001d41 andl %edx, %eax
_pcount_for:00001d10 pushl %ebp00001d11 movl %esp, %ebp00001d13 subl $24, %esp00001d16 movl 8(%ebp), %eax00001d19 movl %eax, -4(%ebp)00001d1c movl $0, -20(%ebp)00001d23 movl $0, -16(%ebp)00001d2a jmp 0x1d5f00001d2c movl -16(%ebp), %eax00001d2f movl %eax, %ecx00001d31 movl $1, %eax00001d36 shll %cl, %eax00001d38 movl %eax, -24(%ebp)00001d3b movl -4(%ebp), %eax00001d3e movl -24(%ebp), %edx00001d41 andl %edx, %eax
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