lec7_machine-control

Lec7_machine-control - Carnegie Mellon Carnegie Mellon Assembly: Arithme.c& Control Marco Gruteser 331 Computer Architecture and

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Carnegie Mellon Carnegie Mellon Assembly: Arithme.c & Control Marco Gruteser 331 Computer Architecture and Assembly Language Slides adapted from Bryant, O’Hallaron, and Nath. 1 Carnegie Mellon Understanding arith • • int arith(int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } movl movl leal sall leal addl addl imull 8(%ebp), %ecx 12(%ebp), %edx (%edx,%edx,2), %eax $4, %eax 4(%ecx,%eax), %eax %ecx, %edx 16(%ebp), %edx %edx, %eax # # # # # # # # Stack Offset 16 z 12 y 8 x 4 Rtn Addr 0 ecx edx eax eax eax edx edx eax • Old %ebp =x =y = y*3 *= 16 (t4) = t4 +x+4 (t5) = x+y (t1) += z (t2) = t2 * t5 (rval) %ebp 2 Carnegie Mellon Observa.ons about arith   Instruc/ons in different int arith(int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } movl movl leal sall leal addl addl imull 8(%ebp), %ecx 12(%ebp), %edx (%edx,%edx,2), %eax $4, %eax 4(%ecx,%eax), %eax %ecx, %edx 16(%ebp), %edx %edx, %eax # # # # # # # #         ecx edx eax eax eax edx edx eax order from C code Some expressions require mul/ple instruc/ons Some instruc/ons cover mul/ple expressions Get exact same code when compile: (x+y+z)*(x+4+48*y) =x =y = y*3 *= 16 (t4) = t4 +x+4 (t5) = x+y (t1) += z (t2) = t2 * t5 (rval) 3 Carnegie Mellon Another Example logical: pushl %ebp movl %esp,%ebp int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } movl xorl sarl andl 12(%ebp),%eax 8(%ebp),%eax $17,%eax $8185,%eax movl xorl sarl andl 12(%ebp),%eax 8(%ebp),%eax $17,%eax $8185,%eax popl %ebp ret # # # # eax eax eax eax = = = = Set Up Body Finish y x^y (t1) t1>>17 (t2) t2 & mask (rval) 4 Carnegie Mellon Another Example logical: pushl %ebp movl %esp,%ebp int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } movl xorl sarl andl 12(%ebp),%eax 8(%ebp),%eax $17,%eax $8185,%eax movl xorl sarl andl 12(%ebp),%eax 8(%ebp),%eax $17,%eax $8185,%eax popl %ebp ret # # # # eax eax eax eax = = = = Set Up Body Finish y x^y (t1) t1>>17 (t2) t2 & mask (rval) 5 Carnegie Mellon Another Example logical: pushl %ebp movl %esp,%ebp int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } movl xorl sarl andl 12(%ebp),%eax 8(%ebp),%eax $17,%eax $8185,%eax movl xorl sarl andl 12(%ebp),%eax 8(%ebp),%eax $17,%eax $8185,%eax popl %ebp ret # # # # eax eax eax eax = = = = Set Up Body Finish y x^y (t1) t1>>17 (t2) t2 & mask (rval) 6 Carnegie Mellon Another Example logical: pushl %ebp movl %esp,%ebp int logical(int x, int y) { int t1 = x^y; int t2 = t1 >> 17; int mask = (1<<13) - 7; int rval = t2 & mask; return rval; } 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 eax eax = = = = y x^y (t1) t1>>17 (t2) t2 & mask (rval) 7 Carnegie Mellon Today Complete addressing mode, address computa.on (leal)   Arithme.c opera.ons   Control: Condi.on codes   Condi.onal branches   Loops   8 Carnegie Mellon Processor State (IA32, Par.al)   Informa.on about currently execu.ng program   Temporary data ( %eax, … )   Loca/on of run/me stack ( %ebp,%esp )   Loca/on of current code control point ( %eip, … )   Status of recent tests ( CF, ZF, SF, OF ) %eax %ecx %edx General purpose registers %ebx %esi %edi %esp %ebp Current stack top %eip Instruc/on pointer CF ZF Current stack frame SF OF Condi/on codes 9 Carnegie Mellon Condi.on Codes (Implicit SeIng)   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 arithme.c opera.ons 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 instruc.on   Full documenta.on (IA32), link on course website   10 Carnegie Mellon Condi.on Codes (Explicit SeIng: Compare)   Explicit SeIng by Compare Instruc.on  cmpl/cmpq Src2, Src1 cmpl b,a like compu/ng a-b without se\ng des/na/on    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) 11 Carnegie Mellon Condi.on Codes (Explicit SeIng: Test)   Explicit SeIng by Test instruc.on  testl/testq Src2, Src1 testl b,a like compu/ng a&b without se\ng des/na/on  Sets condi/on codes based on value of Src1 & Src2 Useful to have one of the operands be a mask    ZF set when a&b == 0 SF set when a&b < 0   12 Carnegie Mellon Reading Condi.on Codes   SetX Instruc.ons   Set single byte based on combina/ons of condi/on codes SetX sete setne sets setns setg setge setl setle seta setb Condi/on ZF ~ZF SF ~SF ~(SF^OF)&~ZF ~(SF^OF) (SF^OF) (SF^OF)|ZF ~CF&~ZF CF Descrip/on Equal / Zero Not Equal / Not Zero Nega/ve Nonnega/ve Greater (Signed) Greater or Equal (Signed) Less (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned) 13 Carnegie Mellon Reading Condi.on Codes (Cont.) SetX Instruc.ons: %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 single byte based on combina/on of condi/on 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 14 Carnegie Mellon Reading Condi.on Codes: x86 ­64   SetX Instruc.ons:   Set single byte based on combina/on of condi/on codes   Does not alter remaining 3 bytes int gt (long x, long y) { return x > y; } long lgt (long x, long y) { return x > y; } Bodies cmpl %esi, %edi setg %al movzbl %al, %eax cmpq %rsi, %rdi setg %al movzbl %al, %eax Is %rax zero? Yes: 32 ­bit instruc/ons set high order 32 bits to 0! 15 Carnegie Mellon Today Complete addressing mode, address computa.on (leal)   Arithme.c opera.ons   x86 ­64   Control: Condi.on codes   Condi.onal branches & Moves   Loops   16 Carnegie Mellon Jumping   jX Instruc.ons   Jump to different part of code depending on condi/on codes jX Condi/on Descrip/on jmp 1 Uncondi/onal je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Nega/ve jns ~SF Nonnega/ve 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) 17 Carnegie Mellon Condi.onal 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 .L6 subl %eax, %edx movl %edx, %eax jmp .L7 .L6: subl %edx, %eax .L7: popl %ebp ret Setup Body1 Body2a Body2b Finish 18 Carnegie Mellon Condi.onal 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 programming style   absdiff: pushl %ebp movl %esp, %ebp movl 8(%ebp), %edx movl 12(%ebp), %eax cmpl %eax, %edx jle .L6 subl %eax, %edx movl %edx, %eax jmp .L7 .L6: subl %edx, %eax .L7: popl %ebp ret Setup Body1 Body2a Body2b Finish Generally considered bad coding style 19 Carnegie Mellon Condi.onal 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 .L6 subl %eax, %edx movl %edx, %eax jmp .L7 .L6: subl %edx, %eax .L7: popl %ebp ret Setup Body1 Body2a Body2b Finish 20 Carnegie Mellon Condi.onal 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 .L6 subl %eax, %edx movl %edx, %eax jmp .L7 .L6: subl %edx, %eax .L7: popl %ebp ret Setup Body1 Body2a Body2b Finish 21 Carnegie Mellon Condi.onal 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 .L6 subl %eax, %edx movl %edx, %eax jmp .L7 .L6: subl %edx, %eax .L7: popl %ebp ret Setup Body1 Body2a Body2b Finish 22 Carnegie Mellon General Condi.onal Expression Transla.on C Code val = Test ? Then_Expr : Else_Expr; val = x>y ? x-y : y-x; Goto Version nt = !Test; if (nt) goto Else; val = Then_Expr; goto Done; Else: val = Else_Expr; Done: ...   Test is expression returning integer = 0 interpreted as false   ≠ 0 interpreted as true   Create separate code regions for then & else expressions   Execute appropriate one   23 Carnegie Mellon Today Complete addressing mode, address computa.on (leal)   Arithme.c opera.ons   x86 ­64   Control: Condi.on codes   Condi.onal branches and moves   Loops   24 Carnegie Mellon “Do ­While” Loop Example C Code 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; } Count number of 1’s in argument x (“popcount”)   Use condi.onal branch to either con.nue looping or to exit loop   25 Carnegie Mellon “Do ­While” Loop Compila.on Goto Version int pcount_do(unsigned x) { int result = 0; loop: result += x & 0x1; x >>= 1; if (x) goto loop; return result; } Registers: %edx x %ecx result   movl .L2: movl andl addl shrl jne $0, %ecx %edx, %eax $1, %eax %eax, %ecx %edx .L2 # result = 0 # loop: # # # # t=x&1 result += t x >>= 1 If !0, goto loop 26 Carnegie Mellon General “Do ­While” Transla.on C Code do Body while (Test);   Body: { Statement1; Statement2; … Statementn; }   Goto Version loop: Body if (Test) goto loop Test returns integer   = 0 interpreted as false   ≠ 0 interpreted as true 27 Carnegie Mellon “While” Loop Example C Code int pcount_while(unsigned x) { int result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; }   Goto Version 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; } Is this code equivalent to the do ­while version?   Must jump out of loop if test fails 28 Carnegie Mellon General “While” Transla.on 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: 29 Carnegie Mellon “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? 30 Carnegie Mellon “For” Loop Form General Form for (Init; Test; Update ) Body for (i = 0; i < WSIZE; i++) { unsigned mask = 1 << i; result += (x & mask) != 0; } Init i = 0 Test i < WSIZE Update i ++ Body { unsigned mask = 1 << i; result += (x & mask) != 0; } 31 Carnegie Mellon “For” Loop While Loop For Version for (Init; Test; Update ) Body While Version Init; while (Test ) { Body Update; } 32 Carnegie Mellon “For” Loop … Goto For Version for (Init; Test; Update ) Body While Version Init; while (Test ) { Body Update; } Init; if (!Test) goto done; loop: Body Update if (Test) goto loop; done: Init; if (!Test) goto done; do Body Update while(Test); done: 33 Next Class       Stacks Procedure calls Prac/ce Problems in sec/ons 3.6 ­3.6.5 34 Carnegie Mellon Using Condi.onal Moves   Condi/onal Move Instruc/ons   Instruc/on supports: if (Test) Dest  Src   Supported in post ­1995 x86 processors   GCC does not always use them   Wants to preserve compa/bility with ancient processors   Enabled for x86 ­64   Use switch –march=686 for IA32   Why?   Branches are very disrup/ve to instruc/on flow through pipelines   Condi/onal move do not require control transfer C Code val = Test ? Then_Expr : Else_Expr; Goto Version tval = Then_Expr; result = Else_Expr; t = Test; if (t) result = tval; return result; 35 Carnegie Mellon Condi.onal Move Example: x86 ­64 int absdiff(int x, int y) { int result; if (x > y) { result = x-y; } else { result = y-x; } return result; } x in %edi y in %esi absdiff: movl subl movl subl cmpl cmovg ret %edi, %esi, %esi, %edi, %esi, %edx, %edx %edx %eax %eax %edi %eax # tval = x-y # result = y-x # Compare x:y # If >, result = tval 36 Carnegie Mellon Bad Cases for Condi.onal Move Expensive Computa/ons val = Test(x) ? Hard1(x) : Hard2(x);     Both values get computed Only makes sense when computa/ons are very simple Risky Computa/ons val = p ? *p : 0;     Both values get computed May have undesirable effects Computa/ons with side effects val = x > 0 ? x*=7 : x+=3;     Both values get computed Must be side ­effect free 37 Carnegie Mellon “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; }   Ini/al test can be op/mized 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; } 38 ...
View Full Document

This document was uploaded on 10/20/2011.

Ask a homework question - tutors are online