Machine-Level Programming II Control Flow Sept. 14, 2000Condition CodesSetting Condition Codes (cont.)Reading Condition CodesReading Condition Codes (Cont.)JumpingConditional Branch ExampleConditional Branch Example (Cont.)“Do-While” Loop Example“Do-While” Loop CompilationGeneral “Do-While” Translation“While” Loop Example #1Actual “While” Loop TranslationGeneral “While” Translation“While” Loop Example #2ipwr Computation“While” “Do-While ” “Goto ”Example #2 Compilation“For” Loop Example“For” “While”“For” Loop CompilationSwitch StatementsJump Table StructureSwitch Statement ExampleAssembly Setup ExplanationJump TableSwitch Statement CompletionObject CodeObject Code (cont.)Extracting Jump Table from BinaryDisassembled TargetsMatching Disassembled TargetsSummarizingMachine-Level Programming IIControl FlowSept. 14, 2000Topics•Condition Codes–Setting–Testing•Control Flow–If-then-else–Varieties of Loops–Switch Statementsclass06.ppt15-213“The course that gives CMU its Zip!”CS 213 F’00– 2 –class06.pptCondition CodesSingle Bit RegistersCF Carry FlagZF Zero FlagSF Sign FlagOF Overflow FlagImplicit Setting By Arithmetic Operationsaddl Src,DestC 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 instructionCS 213 F’00– 3 –class06.pptSetting Condition Codes (cont.)Explicit Setting by Compare Instructioncmpl 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)Explicit Setting by Test instructiontestl 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 < 0CS 213 F’00– 4 –class06.pptReading Condition CodesSetX Condition Descriptionsete ZFEqual / Zerosetne ~ZFNot Equal / Not Zerosets SFNegativesetns ~SFNonnegativesetg ~(SF^OF)&~ZFGreater (Signed)setge ~(SF^OF)Greater or Equal (Signed)setl (SF^OF)Less (Signed)setle (SF^OF)|ZFLess or Equal (Signed)seta ~CF&~ZFAbove (unsigned)setb CFBelow (unsigned)SetX Instructions•Set single byte based on combinations of condition codesCS 213 F’00– 5 –class06.pptReading Condition Codes (Cont.)SetX Instructions•Set single byte based on combinations of condition codes•One of 8 addressable byte registers–Embedded within first 4 integer registers–Does not alter remaining 3 bytes–Typically use andl 0xFF,%eax to finish job%eax%edx%ecx%ebx%esi%edi%esp%ebp%al%ah%dl%dh%cl%ch%bl%bhint gt (int x, int y){ return x > y;}movl 12(%ebp),%eax # eax = ycmpl %eax,8(%ebp) # Compare x : eaxsetg %al # al = x > yandl $255,%eax # Zero rest of %eaxNote inverted ordering!BodyCS 213 F’00– 6 –class06.pptJumpingjX Condition Descriptionjmp 1Unconditionalje ZFEqual / Zerojne ~ZFNot Equal / Not Zerojs SFNegativejns ~SFNonnegativejg ~(SF^OF)&~ZFGreater (Signed)jge ~(SF^OF)Greater or Equal (Signed)jl (SF^OF)Less (Signed)jle (SF^OF)|ZFLess or Equal (Signed)ja ~CF&~ZFAbove (unsigned)jb CFBelow (unsigned)jX Instructions•Jump to different part of code depending on condition codesCS 213 F’00– 7 –class06.pptConditional Branch Exampleint max(int x, int y){ if (x > y) return x; else return y;}_max:pushl %ebpmovl %esp,%ebpmovl 8(%ebp),%edxmovl 12(%ebp),%eaxcmpl %eax,%edxjle L9movl %edx,%eaxL9:movl %ebp,%esppopl %ebpretBodySetUpFinishCS 213 F’00– 8 –class06.pptConditional Branch Example (Cont.)movl 8(%ebp),%edx # edx = xmovl 12(%ebp),%eax # eax = ycmpl %eax,%edx # x : yjle L9 # if <= goto L9movl %edx,%eax # eax = xL9: # 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 styleCS 213 F’00– 9 –class06.pptC Codeint fact_do (int x){ int result = 1; do { result *= x; x = x-1; } while (x > 1); return result;}Goto Versionint 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 holdsCS 213 F’00– 10 –class06.pptGoto Versionint fact_goto (int x){ int result = 1;loop: result *= x; x = x-1; if (x > 1) goto loop; return result;}“Do-While” Loop CompilationRegisters%edx x%eax result_fact_goto:pushl %ebp # Setupmovl %esp,%ebp # Setupmovl $1,%eax # eax = 1movl 8(%ebp),%edx # edx = xL11:imull %edx,%eax # result *= xdecl %edx # x--cmpl $1,%edx # Compare x : 1jg L11 # if > goto loopmovl %ebp,%esp # Finishpopl %ebp # Finishret # FinishAssemblyCS 213 F’00– 11 –class06.pptC Codedo Body while (Test);Goto Versionloop: Body if (Test) goto loopGeneral “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{ Statement1; Statement2; … Statementn;}CS 213 F’00– 12 –class06.pptC Codeint fact_while (int x){ int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;}First Goto Versionint 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 failsCS 213 F’00– 13 –class06.pptC Codeint fact_while(int x){ int result = 1; while (x > 1) { result *= x; x = x-1; }; return result;}Second Goto Versionint 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 testCS 213 F’00– 14 –class06.pptC Codewhile (Test) BodyDo-While Version if (!Test) goto done; do Body while(Test);done:General “While” TranslationGoto Version if (!Test) goto
View Full Document