DOC PREVIEW
Harvey Mudd CS 105 - Machine­Level Programming II: Control Flow

This preview shows page 1-2-16-17-18-34-35 out of 35 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 35 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Machine-Level Programming II: Control FlowCondition CodesSetting Condition Codes (cont.)Slide 4Reading Condition CodesReading Condition Codes (Cont.)JumpingConditional Branch ExampleConditional Branch Ex. (Cont.)“Do-While” Loop Example“Do-While” Loop CompilationGeneral “Do-While” Translation“While” Loop Example #1Actual “While” Loop TranslationGeneral “While” Translation“For” Loop Exampleipwr ComputationSlide 18“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 TargetsSparse Switch ExampleSparse Switch CodeSparse Switch Code StructureSummarizingMachine-Level Programming II:Control FlowMachine-Level Programming II:Control FlowTopicsTopicsCondition CodesSettingTestingControl FlowIf-then-elseVarieties of LoopsSwitch StatementsX86.2.pptCS 105“Tour of the Black Holes of Computing”– 2 –CS 105Condition CodesCondition CodesSingle-Bit RegistersSingle-Bit RegistersCF Carry Flag SF Sign FlagZF Zero Flag OF Overflow FlagImplicitly Set By Arithmetic OperationsImplicitly Set By Arithmetic Operationsaddl Src,DestC analog: t = a + b; b = t;CF set if carry out from most significant bitDetects unsigned overflow; also used for multiprecision arithmetic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)NotNot Set by Set by lealleal instruction instruction– 3 –CS 105Setting Condition Codes (cont.)Setting Condition Codes (cont.)Explicit Setting by Compare InstructionExplicit 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)– 4 –CS 105Setting Condition Codes (cont.)Setting Condition Codes (cont.)Explicit Setting by Test instructionExplicit 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 < 0– 5 –CS 105Reading Condition CodesReading 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 InstructionsSetX InstructionsSet single byte based on combinations of condition codes– 6 –CS 105Reading Condition Codes (Cont.)Reading Condition Codes (Cont.)SetX InstructionsSetX 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 movzbl 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 : ysetg %al # al = x > ymovzbl %al,%eax # Zero rest of %eaxNote inverted ordering!Body– 7 –CS 105JumpingJumpingjX 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 InstructionsjX InstructionsJump to different part of code depending on condition codes– 8 –CS 105Conditional Branch ExampleConditional 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 %ebpretBodySetUpFinish– 9 –CS 105Conditional Branch Ex. (Cont.)Conditional Branch Ex. (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 style– 10 –CS 105C 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“Do-While” Loop ExampleUse backward branch to continue loopingOnly take branch when “while” condition holds– 11 –CS 105Goto Versionint fact_goto (int x){ int result = 1;loop: result *= x; x = x-1; if (x > 1) goto loop; return result;}“Do-While” Loop Compilation“Do-While” Loop CompilationRegistersRegisters%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 # FinishAssembly– 12 –CS 105C Codedo Body while (Test);Goto Versionloop: Body if (Test) goto loopGeneral “Do-While” TranslationGeneral “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;}– 13 –CS 105C 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“While” Loop Example #1Is this code equivalent to the do-while version?Must jump out of loop if test fails– 14 –CS 105C 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


View Full Document

Harvey Mudd CS 105 - Machine­Level Programming II: Control Flow

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Machine­Level Programming II: Control Flow
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Machine­Level Programming II: Control Flow and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Machine­Level Programming II: Control Flow 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?