Explicit Setting by Compare Instruction cmpl Src2 Src1 cmpl ba like computing a

Explicit setting by compare instruction cmpl src2

This preview shows page 75 - 86 out of 160 pages.

Setting Condition Codes (cont.) Explicit Setting by Compare Instruction cmpl Src2 , Src1 cmpl b,a like computing a-b without setting destination NOTE: The operands are reversed. Source of confusion 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)
Image of page 75
Rutgers University David Menendez 76 Setting Condition Codes (cont.) 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
Image of page 76
Conditional Branch Example _max: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax cmpl %eax,%edx jle L9 movl %edx,%eax L9: movl %ebp,%esp popl %ebp ret Body Set Up Finish
Image of page 77
Conditional Branch Example int max(int x, int y) { if (x <= y) return y; else return x; } _max: pushl %ebp movl %esp,%ebp movl 8(%ebp),%edx movl 12(%ebp),%eax cmpl %eax,%edx jle L9 movl %edx,%eax L9: movl %ebp,%esp popl %ebp ret Body Set Up Finish
Image of page 78
Rutgers University David Menendez 79 Conditional Branch Example (Cont.) 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 L9: # Done: int goto_max(int x, int y) { int rval = y; int ok = (x <= y); if (ok) goto done; rval = x; done: return rval; } Skipped when x y C allows “goto” as means of transferring control Closer to machine- level programming style Generally considered bad coding style int max(int x, int y) { if (x <= y) return y; else return x; }
Image of page 79
Rutgers University David Menendez 80 Mystery Function . LC0: .string "%d” .text .globl foo .type foo, @function foo: pushl %ebp movl %esp, %ebp subl $40, %esp leal -12(%ebp), %eax movl %eax, 4(%esp) movl $.LC0, (%esp) call scanf cmpl $4, -12(%ebp) je .L3 call explode_bomb .L3: leave .p2align 4,,3 ret
Image of page 80
Rutgers University David Menendez 81 C Code int fact_do(int x) { int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; } “Do-While” Loop Example
Image of page 81
Rutgers University David Menendez 82 C Code int fact_do(int x) { int result = 1; do { result *= x; x = x-1; } while (x > 1); return result; } Goto Version int fact_goto(int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop ; return result; } “Do-While” Loop Example Use backward branch to continue looping Only take branch when “while” condition holds
Image of page 82
Rutgers University David Menendez 83 Goto Version int fact_goto(int x) { int result = 1; loop: result *= x; x = x-1; if (x > 1) goto loop ; return result; } “Do-While” Loop Compilation Registers %edx x %eax result _fact_goto: pushl %ebp # Setup movl %esp,%ebp # Setup movl $1,%eax # eax = 1 movl 8(%ebp),%edx # edx = x L11: imull %edx,%eax # result *= x decl %edx # x-- cmpl $1,%edx # Compare x : 1 jg L11 # if > goto loop movl %ebp,%esp # Finish popl %ebp # Finish ret # Finish Assembly
Image of page 83
Rutgers University David Menendez 84 C Code do Body while ( Test ); Goto Version loop : Body if ( Test ) goto loop General “Do-While” Translation Body can be any C statement Typically compound statement: Test is expression returning integer = 0 interpreted as false 0 interpreted as true { Statement 1 ; Statement 2 ; Statement n ; }
Image of page 84
Rutgers University David Menendez 85 C Code int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } “While” Loop Example #1
Image of page 85
Image of page 86

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture