machine level programming ii control flow
play

Machine-Level Programming II: Control Flow Today Condition codes - PowerPoint PPT Presentation

Machine-Level Programming II: Control Flow Today Condition codes Control flow structures Next time Procedures Fall 2011 Monday, October 10, 2011 Condition codes Single bit registers CF Carry Flag SF Sign Flag ZF Zero Flag OF


  1. Machine-Level Programming II: Control Flow Today  Condition codes  Control flow structures Next time  Procedures Fall 2011 Monday, October 10, 2011

  2. Condition codes Single bit registers CF Carry Flag SF Sign Flag ZF Zero Flag OF Overflow Flag Implicitly set by arithmetic operations addl source,destination C analog: t = a + b – CF set if carry out from most significant bit • Used to detect unsigned overflow – ZF set if t == 0 – SF set if t < 0 – OF set if two’s complement overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) Not set by leal instruction 2 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  3. Setting condition codes Explicit setting by compare instruction cmpl Src2,Src1 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 – OF set if two’s complement overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b) >0) 3 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  4. Setting condition codes Explicit setting by test instruction testl Src2 , Src1 – Sets condition codes based on value of Src1 & Src2 • Useful to have one of the operands be a mask – testl b,a like computing a&b without setting destination – ZF set when a&b == 0 – SF set when a&b < 0 4 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  5. Reading condition codes SetX Instructions – Set single byte based on combinations of condition codes %eax %ah %al – One of 8 addressable byte registers %edx %dh %dl • Embedded within first 4 integer registers • Does not alter remaining 3 bytes %ecx %ch %cl • Typically use movzbl to finish job %ebx %bh %bl %esi int gt (int x, int y) { %edi return x > y; } %esp Body %ebp movl 12(%ebp),%eax # eax = y cmpl %eax,8(%ebp) # Compare x : y Note setg %al # al = x > y inverted movzbl %al,%eax # Zero rest of %eax ordering! 5 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  6. Reading condition codes SetX Instructions – Set single byte based on combinations of condition codes 6 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  7. Checkpoint Monday, October 10, 2011

  8. Jumping jX Instructions – Jump to different part of code depending on condition codes 8 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  9. Conditional branch example _max: pushl %ebp Set movl %esp,%ebp Up int max(int x, int y) movl 8(%ebp),%edx { movl 12(%ebp),%eax if (x > y) cmpl %eax,%edx return x; Body jle L9 else movl %edx,%eax return y; L9: } movl %ebp,%esp popl %ebp ret Finish 9 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  10. Conditional branch example int goto_max(int x, int y) C allows “goto” as means of { transferring control int rval = y; int ok = (x <= y); – Closer to machine-level if (ok) programming style goto done; Generally considered bad rval = x; coding style done: return rval; } movl 8(%ebp),%edx # edx = x movl 12(%ebp),%eax # eax = y cmpl %eax,%edx # x : y jle L9 # if <= goto L9 movl %edx,%eax # eax = x Skipped when x ≤ y L9: # Done: 10 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  11. “Do-While” loop example C Code Goto Version int fact_do int fact_goto(int x) (int x) { { int result = 1; int result = 1; loop: do { result *= x; result *= x; x = x-1; x = x-1; if (x > 1) } while (x > 1); goto loop ; return result; return result; } } Use backward branch to continue looping Only take branch when “while” condition holds 11 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  12. “Do-While” loop compilation Assembly Goto Version _fact_goto: int fact_goto pushl %ebp # Setup (int x) movl %esp,%ebp # Setup { movl $1,%eax # eax = 1 int result = 1; movl 8(%ebp),%edx # edx = x loop: result *= x; L11: x = x-1; imull %edx,%eax # result *= x if (x > 1) decl %edx # x-- goto loop ; cmpl $1,%edx # Compare x : 1 return result; jg L11 # if > goto loop } movl %ebp,%esp # Finish popl %ebp # Finish Registers ret # Finish %edx x %eax result 12 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  13. General “Do-While” translation Goto Version C Code do loop : Body Body while ( Test ); if ( Test ) goto loop Body can be any C statement – Typically compound statement: { Statement 1 ; Statement 2 ; … Statement n ; } Test is expression returning integer = 0 interpreted as false ≠ 0 interpreted as true 13 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  14. “While” loop example #1 C Code First Goto Version int fact_while int fact_while_goto (int x) (int x) { { int result = 1; int result = 1; while (x > 1) { loop: result *= x; if (!(x > 1)) x = x-1; goto done ; }; result *= x; return result; x = x-1; } goto loop ; done: return result; } Is this code equivalent to the do-while version? Must jump out of loop if test fails 14 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  15. Actual “While” loop translation C Code Second Goto Version int fact_while int fact_while_goto2 (int x) (int x) { { int result = 1; int result = 1; while (x > 1) { if (!(x > 1)) result *= x; goto done ; x = x-1; loop: }; result *= x; return result; x = x-1; } if (x > 1) goto loop ; done: return result; Uses same inner loop as do- } while version Guards loop entry with extra test 15 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  16. General “While” translation C Code while ( Test ) Body Goto Version Do-While Version if (! Test ) if (! Test ) goto done ; goto done ; loop : do Body Body if ( Test ) while( Test ); goto loop ; done : done : 16 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  17. Checkpoint Monday, October 10, 2011

  18. “For” loop example /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned p) { int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } return result; } Algorithm – Exploit property that p = p 0 + 2 p 1 + 4 p 2 + … 2 n –1 p n –1 – Gives: x p = z 0 · z 1 2 · ( z 2 2 ) 2 · … · (…(( z n –12 ) 2 )…) 2 z i = 1 when p i = 0 Example 3 10 � = 3 2 * 3 8 z i = x when p i = 1 n–1 times � = 3 2 * ((3 2 ) 2 ) 2 – Complexity O(log p ) 18 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  19. ipwr computation /* Compute x raised to nonnegative power p */ int ipwr_for(int x, unsigned p) { int result; for (result = 1; p != 0; p = p>>1) { if (p & 0x1) result *= x; x = x*x; } return result; } 19 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  20. “For” loop example General Form int result; for (result = 1; for ( Init ; Test ; Update ) p != 0; p = p>>1) { Body if (p & 0x1) result *= x; x = x*x; } Init Test Update result = 1 p != 0 p = p >> 1 { if (p & 0x1) Body result *= x; x = x*x; } 20 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  21. “For” → “While” For Version While Version for ( Init ; Test ; Update ) Init ; while ( Test ) { Body Body Update ; } Do-While Version Goto Version Init ; if (! Test ) Init ; goto done ; if (! Test ) do { goto done ; Body loop: Update ; Body } while ( Test ) Update ; done : if ( Test ) goto loop ; done: 21 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  22. “For” loop compilation Goto Version result = 1; if (p == 0) Init ; goto done ; if (! Test ) loop : goto done ; if (p & 0x1) loop : result *= x; Body x = x*x; p = p >> 1; Update ; if (p != 0) if ( Test ) goto loop ; goto loop ; done : done : Init Test Body result = 1 p != 0 { if (p & 0x1) Update result *= x; x = x*x; p = p >> 1 } 22 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

  23. Switch statements typedef enum Implementation options {ADD, MULT, MINUS, DIV, MOD, BAD} op_type; – Series of conditionals • Good if few cases char unparse_symbol(op_type op) { • Slow if many switch (op) { – Jump table case ADD : return '+'; • Lookup branch target case MULT: • Avoids conditionals return '*'; case MINUS: • Possible when cases are return '-'; small integer constants case DIV: – GCC return '/'; case MOD: • Picks one based on case return '%'; structure case BAD: return '?'; – Bug in example code } • No default given } 23 EECS 213 Introduction to Computer Systems Northwestern University Monday, October 10, 2011

Recommend


More recommend