Body Conditional Branch Example int maxint x int y if x y return x else return

# Body conditional branch example int maxint x int y if

• Notes
• 126
• 100% (1) 1 out of 1 people found this document helpful

This preview shows page 69 - 80 out of 126 pages.

Body
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
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
Rutgers University Santosh Nagarakatte 72 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 n C allows “goto” as means of transferring control l Closer to machine-level programming style n Generally considered bad coding style int max(int x, int y) { if (x <= y) return y; else return x; }
Rutgers University Santosh Nagarakatte 73 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 n Use backward branch to continue looping n Only take branch when “while” condition holds
Rutgers University Santosh Nagarakatte 74 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
Rutgers University Santosh Nagarakatte 75 C Code do Body while ( Test ); Goto Version loop : Body if ( Test ) goto loop General “Do-While” Translation n Body can be any C statement l Typically compound statement: n Test is expression returning integer = 0 interpreted as false 0 interpreted as true { Statement 1 ; Statement 2 ; Statement n ; }
Rutgers University Santosh Nagarakatte 76 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 n Is this code equivalent to the do-while version? n Must jump out of loop if test fails
Rutgers University Santosh Nagarakatte 77 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 n Uses same inner loop as do-while version n Guards loop entry with extra test
C Code while ( Test ) Body Do-While Version if (! Test ) goto done ; do Body while( Test ); done : General “While” Translation Goto Version if (! Test ) goto done ; loop : Body if ( Test ) goto loop ; done :
Rutgers University Santosh Nagarakatte 79 Switch Statements Implementation Options n Series of conditionals l Good if few cases l Slow if many n Jump Table l Lookup branch target l

#### You've reached the end of your free preview.

Want to read all 126 pages?

• Spring '08
• Chakraborty
• X86, Processor register, Rutgers University, Addressing mode, eax, Prof. Santosh Nagarakatte