St Louis University MachineLevel Programming II Control and

  • Slides: 42
Download presentation
St. Louis University Machine-Level Programming II: Control and Arithmetic CSCI 2400: Computer Architecture Instructor:

St. Louis University Machine-Level Programming II: Control and Arithmetic CSCI 2400: Computer Architecture Instructor: David Ferry Slides adapted from Bryant & O’Hallaron’s slides 1

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢ Control: Condition codes ¢ Conditional branches ¢ While loops ¢ 2

St. Louis University Complete Memory Addressing Modes Most General Form ¢ D(Rb, Ri, S)

St. Louis University Complete Memory Addressing Modes Most General Form ¢ D(Rb, Ri, S) Mem[Reg[Rb]+S*Reg[Ri]+ D] ¢ § D: Constant “displacement” 1, 2, or 4 bytes § Rb: Base register: Any of 8 integer registers § Ri: Index register: Any, except for %esp Unlikely you’d use %ebp, either § S: Scale: 1, 2, 4, or 8 (why these numbers? ) § Special Cases ¢ (Rb, Ri) Mem[Reg[Rb]+Reg[Ri]] ¢ D(Rb, Ri) Mem[Reg[Rb]+Reg[Ri]+D] ¢ (Rb, Ri, S) Mem[Reg[Rb]+S*Reg[Ri]] ¢ 3

St. Louis University Quick Check D(Rb, Ri, S) = Mem[Reg[Rb]+S*Reg[Ri]+ D] %edx 0 xf

St. Louis University Quick Check D(Rb, Ri, S) = Mem[Reg[Rb]+S*Reg[Ri]+ D] %edx 0 xf 000 %ecx 0 x 0100 Expression Address Computation Address 0 x 8(%edx) 0 xf 000 + 0 x 8 0 xf 008 (%edx, %ecx) 0 xf 000 + 0 x 100 0 xf 100 (%edx, %ecx, 4) 0 xf 000 + 4*0 x 100 0 xf 400 0 x 80(, %edx, 2) 2*0 xf 000 + 0 x 80 0 x 1 e 080 4

St. Louis University Address Computation Instruction ¢ leal Src, Dest § Src is address

St. Louis University Address Computation Instruction ¢ leal Src, Dest § Src is address mode expression § Set Dest to address denoted by expression ¢ Uses § Computing addresses without a memory reference E. g. , translation of p = &x[i]; § Computing arithmetic expressions of the form x + k*y § k = 1, 2, 4, or 8 § ¢ Example int mul 12(int x) { return x*12; } Converted to ASM by compiler: leal (%eax, 2), %eax sall $2, %eax ; t <- x+x*2 ; return t<<2 5

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢ Control: Condition codes ¢ Conditional branches ¢ While loops ¢ 6

St. Louis University Some Arithmetic Operations ¢ Two Operand Instructions: Format addl subl imull

St. Louis University Some Arithmetic Operations ¢ Two Operand Instructions: Format addl subl imull sarl shrl xorl andl orl Computation Src, Dest = Dest + Src Src, Dest = Dest * Src, Dest = Dest << Src Src, Dest = Dest >> Src, Dest = Dest ^ Src, Dest = Dest & Src, Dest = Dest | Src Also called shll Arithmetic Logical Watch out for argument order! ¢ No distinction between signed and unsigned int (why? ) ¢ 7

St. Louis University Some Arithmetic Operations ¢ One Operand Instructions incl decl negl notl

St. Louis University Some Arithmetic Operations ¢ One Operand Instructions incl decl negl notl ¢ Dest Dest = Dest + 1 Dest = Dest 1 Dest = ~Dest See book for more instructions 8

St. Louis University Arithmetic Expression Example arith: pushl %ebp %esp, %ebp int arith(int x,

St. Louis University Arithmetic Expression Example arith: pushl %ebp %esp, %ebp int arith(int x, int y, int z) movl { movl 8(%ebp), %ecx int t 1 = x+y; movl 12(%ebp), %edx int t 2 = z+t 1; leal (%edx, 2), %eax int t 3 = x+4; sall $4, %eax int t 4 = y * 48; leal 4(%ecx, %eax), %eax int t 5 = t 3 + t 4; addl %ecx, %edx int rval = t 2 * t 5; addl 16(%ebp), %edx return rval; imull %edx, %eax } popl ret %ebp Set Up Body Finish 9

St. Louis University Understanding arith int arith(int x, int y, int z) { int

St. Louis University Understanding arith int arith(int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl leal sall leal addl imull • • Offset • 16 z 12 y 8 x 4 Rtn Addr 0 Old %ebp 8(%ebp), %ecx 12(%ebp), %edx (%edx, 2), %eax $4, %eax 4(%ecx, %eax), %eax %ecx, %edx 16(%ebp), %edx, %eax 10

St. Louis University Understanding arith int arith(int x, int y, int z) { int

St. Louis University Understanding arith int arith(int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl leal sall leal addl imull 8(%ebp), %ecx 12(%ebp), %edx (%edx, 2), %eax $4, %eax 4(%ecx, %eax), %eax %ecx, %edx 16(%ebp), %edx, %eax # # # # ecx edx eax eax edx eax • Stack • Offset • 16 z 12 y 8 x 4 Rtn Addr 0 Old %ebp = x = y*3 *= 16 (t 4) = t 4 +x+4 (t 5) = x+y (t 1) += z (t 2) = t 2 * t 5 (rval) 11

St. Louis University Observations about arith § Instructions in different order int arith(int x,

St. Louis University Observations about arith § Instructions in different order int arith(int x, int y, int z) { int t 1 = x+y; int t 2 = z+t 1; int t 3 = x+4; int t 4 = y * 48; int t 5 = t 3 + t 4; int rval = t 2 * t 5; return rval; } movl leal sall leal addl imull 8(%ebp), %ecx 12(%ebp), %edx (%edx, 2), %eax $4, %eax 4(%ecx, %eax), %eax %ecx, %edx 16(%ebp), %edx, %eax # # # # § § ecx edx eax eax edx eax from C code Some expressions require multiple instructions Some instructions cover multiple expressions Get exact same code when compile: (x+y+z)*(x+4+48*y) = x = y*3 *= 16 (t 4) = t 4 +x+4 (t 5) = x+y (t 1) += z (t 2) = t 2 * t 5 (rval) 12

St. Louis University Another Example int logical(int x, int y) { int t 1

St. Louis University Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax popl %ebp ret # # eax eax = = Set Up Body Finish y x^y (t 1) t 1>>17 (t 2) t 2 & mask (rval) 13

St. Louis University Another Example int logical(int x, int y) { int t 1

St. Louis University Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax popl %ebp ret # # eax eax = = Set Up Body Finish y x^y (t 1) t 1>>17 (t 2) t 2 & mask (rval) 14

St. Louis University Another Example int logical(int x, int y) { int t 1

St. Louis University Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax popl %ebp ret # # eax eax = = Set Up Body Finish y x^y (t 1) t 1>>17 (t 2) t 2 & mask (rval) 15

St. Louis University Another Example int logical(int x, int y) { int t 1

St. Louis University Another Example int logical(int x, int y) { int t 1 = x^y; int t 2 = t 1 >> 17; int mask = (1<<13) - 7; int rval = t 2 & mask; return rval; } logical: pushl %ebp movl %esp, %ebp movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax popl %ebp ret Set Up Body Finish 213 = 8192, 213 – 7 = 8185 movl xorl sarl andl 12(%ebp), %eax 8(%ebp), %eax $17, %eax $8185, %eax # # eax eax = = y x^y (t 1) t 1>>17 (t 2) t 2 & mask (rval) 16

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢ Control: Condition codes ¢ Conditional branches ¢ Loops ¢ 17

St. Louis University Processor State (IA 32, Partial) ¢ Information about currently executing program

St. Louis University Processor State (IA 32, Partial) ¢ Information about currently executing program § Temporary data ( %eax, … ) § Location of runtime stack ( %ebp, %esp ) § Location of current code control point ( %eip, … ) § Status of recent tests ( CF, ZF, SF, OF ) %eax %ecx %edx %ebx General purpose registers %esi %edi %esp %ebp Current stack top %eip Instruction pointer Current stack frame CF ZF SF OF Condition codes 18

St. Louis University Condition Codes (Implicit Setting) ¢ Single bit registers §CF §ZF ¢

St. Louis University Condition Codes (Implicit Setting) ¢ Single bit registers §CF §ZF ¢ Carry Flag (for unsigned) SF Sign Flag (for signed) Zero Flag OF Overflow Flag (for signed) Implicitly set (think of it as side effect) by arithmetic operations Example: addl/addq Src, Dest ↔ t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) Not set by lea instruction ¢ Full documentation (IA 32), link on course website ¢ 19

St. Louis University Condition Codes (Explicit Setting: Compare) ¢ Explicit Setting by Compare Instruction

St. Louis University Condition Codes (Explicit Setting: Compare) ¢ Explicit Setting by Compare Instruction §cmpl/cmpq Src 2, Src 1 §cmpl b, a like computing a-b without setting destination §CF set if carry out from most significant bit (used for unsigned comparisons) §ZF set if a == b §SF set if (a-b) < 0 (as signed) §OF set if two’s-complement (signed) overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) 20

St. Louis University Condition Codes (Explicit Setting: Test) ¢ Explicit Setting by Test instruction

St. Louis University Condition Codes (Explicit Setting: Test) ¢ Explicit Setting by Test instruction §testl/testq Src 2, Src 1 testl b, a like computing a&b without setting destination §Sets condition codes based on value of Src 1 & Src 2 §Useful to have one of the operands be a mask §ZF set when a&b == 0 §SF set when a&b < 0 21

St. Louis University Reading Condition Codes ¢ Set. X Instructions § Set single byte

St. Louis University Reading Condition Codes ¢ Set. X Instructions § Set single byte based on combinations of condition codes Set. X sete setne sets setns setge setle seta setb Condition ZF ~ZF SF ~(SF^OF)&~ZF ~(SF^OF)|ZF ~CF&~ZF CF Description Equal / Zero Not Equal / Not Zero Negative Nonnegative Greater (Signed) Greater or Equal (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned) 22

St. Louis University Reading Condition Codes (Cont. ) ¢ %eax %ah %al %ecx %ch

St. Louis University Reading Condition Codes (Cont. ) ¢ %eax %ah %al %ecx %ch %cl § Does not alter remaining 3 bytes § Typically use movzbl to finish job %edx %dh %dl int gt (int x, int y) { return x > y; } %ebx %bh %bl Set. X Instructions: § Set single byte based on combination of condition codes ¢ One of 8 addressable byte registers Body movl 12(%ebp), %eax cmpl %eax, 8(%ebp) setg %al movzbl %al, %eax %esi %edi # # eax = y Compare x : y al = x > y Zero rest of %eax %esp %ebp 23

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢ Control: Condition codes ¢ Conditional branches & Moves ¢ Loops ¢ 25

St. Louis University Jumping ¢ j. X Instructions § Jump to different part of

St. Louis University Jumping ¢ j. X Instructions § Jump to different part of code depending on condition codes j. X Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) 26

St. Louis University Conditional Branch Example int absdiff(int x, int y) { int result;

St. Louis University Conditional Branch Example int absdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle. L 6 subl %eax, %edx movl %edx, %eax jmp. L 7. L 6: subl %edx, %eax. L 7: popl %ebp ret Setup Body 1 Body 2 a Body 2 b Finish 27

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) {

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: return result; } ¢ C allows “goto” as means of transferring control § Closer to machine-level absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle. L 6 subl %eax, %edx movl %edx, %eax jmp. L 7. L 6: subl %edx, %eax. L 7: popl %ebp ret Setup Body 1 Body 2 a Body 2 b Finish programming style ¢ Generally considered 28

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) {

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: return result; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle. L 6 subl %eax, %edx movl %edx, %eax jmp. L 7. L 6: subl %edx, %eax. L 7: popl %ebp ret Setup Body 1 Body 2 a Body 2 b Finish 29

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) {

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: return result; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle. L 6 subl %eax, %edx movl %edx, %eax jmp. L 7. L 6: subl %edx, %eax. L 7: popl %ebp ret Setup Body 1 Body 2 a Body 2 b Finish 30

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) {

St. Louis University Conditional Branch Example (Cont. ) int goto_ad(int x, int y) { int result; if (x <= y) goto Else; result = x-y; goto Exit; Else: result = y-x; Exit: return result; } absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle. L 6 subl %eax, %edx movl %edx, %eax jmp. L 7. L 6: subl %edx, %eax. L 7: popl %ebp ret Setup Body 1 Body 2 a Body 2 b Finish 31

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢

St. Louis University Today Complete addressing mode, address computation (leal) ¢ Arithmetic operations ¢ x 86 -64 ¢ Control: Condition codes ¢ Conditional branches and moves ¢ Loops ¢ 36

St. Louis University “Do-While” Loop Example C Code int pcount_do(unsigned x) { int result

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

St. Louis University “Do-While” Loop Compilation Goto Version int pcount_do(unsigned x) { int result

St. Louis University “Do-While” Loop Compilation Goto Version int pcount_do(unsigned x) { int result = 0; loop: result += x & 0 x 1; x >>= 1; if (x) goto loop; return result; } Registers: %edx x %ecx result ¢ movl. L 2: movl andl addl shrl jne $0, %ecx %edx, %eax $1, %eax, %ecx %edx. L 2 # result = 0 # loop: # # t = x & 1 result += t x >>= 1 If !0, goto loop 38

St. Louis University General “Do-While” Translation C Code do Body while (Test); ¢ Body:

St. Louis University General “Do-While” Translation C Code do Body while (Test); ¢ Body: { } ¢ Goto Version loop: Body if (Test) goto loop Statement 1; Statement 2; … Statementn; Test returns integer § = 0 interpreted as false § ≠ 0 interpreted as true 39

St. Louis University “While” Loop Example C Code int pcount_while(unsigned x) { int result

St. Louis University “While” Loop Example C Code int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0 x 1; x >>= 1; } return result; } ¢ Goto Version int pcount_do(unsigned x) { int result = 0; if (!x) goto done; loop: result += x & 0 x 1; x >>= 1; if (x) goto loop; done: return result; } Is this code equivalent to the do-while version? § Must jump out of loop if test fails 40

St. Louis University General “While” Translation While version while (Test) Body Do-While Version if

St. Louis University General “While” Translation While version while (Test) Body Do-While Version if (!Test) goto done; do Body while(Test); done: Goto Version if (!Test) goto done; loop: Body if (Test) goto loop; done: 41

St. Louis University “For” Loop Example C Code #define WSIZE 8*sizeof(int) int pcount_for(unsigned x)

St. Louis University “For” Loop Example C Code #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; } ¢ Is this code equivalent to other versions? 42

St. Louis University “For” Loop Form General Form for (Init; Test; Update ) Body

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

St. Louis University “For” Loop While Loop For Version for (Init; Test; Update )

St. Louis University “For” Loop While Loop For Version for (Init; Test; Update ) Body While Version Init; while (Test ) { Body Update; } 44

St. Louis University “For” Loop … Goto For Version for (Init; Test; Update )

St. Louis University “For” Loop … Goto For Version for (Init; Test; Update ) Body Init; if (!Test) goto done; loop: Body Update if (Test) goto loop; done: While Version Init; while (Test ) { Body Update; } Init; if (!Test) goto done; do Body Update while(Test); done: 45

St. Louis University “For” Loop Conversion Example C Code #define WSIZE 8*sizeof(int) int pcount_for(unsigned

St. Louis University “For” Loop Conversion Example C Code #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; } ¢ Initial test can be optimized away Goto Version int pcount_for_gt(unsigned x) { int i; int result = 0; Init i = 0; if (!(i < WSIZE)) !Test goto done; loop: Body { unsigned mask = 1 << i; result += (x & mask) != 0; } i++; Update if (i < WSIZE) Test goto loop; done: return result; } 46

St. Louis University Summary ¢ Today § § § ¢ Complete addressing mode, address

St. Louis University 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 47