Body Rutgers University 60 Jumping jX Instructions Jump to different part of

Body rutgers university 60 jumping jx instructions

This preview shows page 59 - 69 out of 75 pages.

Body
Image of page 59
Rutgers University 60 Jumping jX Instructions Jump to different part of code depending on condition codes jX 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)
Image of page 60
Rutgers University 61 Conditional Branch Example (Q): What does the following code return? A. Sum of arguments B. Diff of arguments C. Larger of arguments D. Smaller of arguments Hint: return value of a function is stored in %eax. _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 61
Rutgers University 62 Conditional Branch Example int max(int x, int y) { if (x > y) return x; else return y; } _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 62
Rutgers University 63 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
Image of page 63
Rutgers University 64 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 64
Rutgers University 65 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 %edxx %eaxresult _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 65
Rutgers University 66 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 66
Rutgers University 67 C Code int fact_while (int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } First Goto Version int fact_while_goto (int x) { int result = 1; loop: if (!(x > 1)) goto done ; result *= x; x = x-1; goto loop ; done: return result; } “While” Loop Example #1 Is this code equivalent to the do-while version? Must jump out of loop if test fails
Image of page 67
Rutgers University 68 C Code int fact_while(int x) { int result = 1; while (x > 1) { result *= x; x = x-1; }; return result; } Second Goto Version int fact_while_goto2 (int x) { int result = 1; if (!(x > 1)) goto done ; loop: result *= x; x = x-1; if (x > 1) goto loop ; done: return result; } Actual “While” Loop Translation Uses same inner loop  as do-while version Guards loop entry with  extra test
Image of page 68
Image of page 69

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture