




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第3章程序的機器級表示:
——控制
計算機組成與結(jié)構(gòu)
2016年4月主講教師HunanUniversityTodayControl:ConditioncodesConditionalbranchesLoopsSwitchstatementsHunanUniversityProcessorState(IA32,Partial)InformationaboutcurrentlyexecutingprogramTemporarydata
(%eax,…)Locationofruntimestack
(%ebp,%esp)Locationofcurrentcodecontrolpoint
(%eip,…)Statusofrecenttests
(CF,ZF,SF,OF)%eipGeneralpurposeregistersCurrentstacktopCurrentstackframeInstructionpointerCFZFSFOFConditioncodes%eax%ecx%edx%ebx%esi%edi%esp%ebpHunanUniversityConditionCodes(ImplicitSetting)SinglebitregistersCFCarryFlag(forunsigned)SFSignFlag(forsigned)ZFZeroFlag OFOverflowFlag(forsigned)Implicitlyset(thinkofitassideeffect)byarithmeticoperationsExample:addl/addqSrc,Dest?t=a+bCFsetifcarryoutfrommostsignificantbit(unsignedoverflow)ZFsetift==0SFsetift<0(assigned)OFsetiftwo’s-complement(signed)overflow
(a>0&&b>0&&t<0)||(a<0&&b<0&&t>=0)Notsetbylea/movinstructionHunanUniversityConditionCodes(ExplicitSetting:Compare)ExplicitSettingbyCompareInstructioncmpl/cmpqSrc2,Src1 ;Src1–Src2,影響標(biāo)志位cmplb,alikecomputinga-bwithoutsettingdestinationCFsetifcarryoutfrommostsignificantbit(usedforunsignedcomparisons)ZFsetifa==bSFsetif(a-b)<0(assigned)OFsetiftwo’s-complement(signed)overflow
(a>0&&b<0&&(a-b)<0)||(a<0&&b>0&&(a-b)>0)HunanUniversityConditionCodes(ExplicitSetting:Test)ExplicitSettingbyTestinstructiontestl/testqSrc2,Src1 ;Src1&Src2,影響標(biāo)志位testlb,alikecomputinga&bwithoutsettingdestinationSetsconditioncodesbasedonvalueofSrc1&Src2UsefultohaveoneoftheoperandsbeamaskZFsetwhena&b==0SFsetwhena&b<0OF/CFclearHunanUniversityReadingConditionCodesSetXInstructionsSetsinglebytebasedoncombinationsofconditioncodesSetXConditionDescriptionseteZFEqual/Zerosetne~ZFNotEqual/NotZerosetsSFNegativesetns~SFNonnegativesetg~(SF^OF)&~ZFGreater(Signed)setge~(SF^OF)GreaterorEqual(Signed)setl(SF^OF)Less(Signed)setle(SF^OF)|ZFLessorEqual(Signed)seta~CF&~ZFAbove(unsigned)setbCFBelow(unsigned)HunanUniversitymovl12(%ebp),%eax #eax=ycmpl%eax,8(%ebp) #Comparex:ysetg%al #al=x>ymovzbl%al,%eax #Zerorestof%eaxReadingConditionCodes(Cont.)SetXInstructions:SetsinglebytebasedoncombinationofconditioncodesOneof8addressablebyteregistersDoesnotalterremaining3bytesTypicallyusemovzbltofinishjobintgt(intx,inty){returnx>y;}Body%eax%ah%alHunanUniversityReadingConditionCodes:x86-64intgt(intx,inty){returnx>y;}movl12(%ebp),%eaxcmpl%eax,8(%ebp)setg%almovzbl%al,%eaxBodieslonglgt(longx,longy){returnx>y;}SetXInstructions:SetsinglebytebasedoncombinationofconditioncodesDoesnotalterremaining3bytescmpq%rsi,%rdisetg%almovzbq%al,%raxTodayControl:ConditioncodesConditionalbranches&MovesLoopsSwitchstatementsHunanUniversityJumpingjXInstructionsJumptodifferentpartofcodedependingonconditioncodesjXConditionDescriptionjmp1UnconditionaljeZFEqual/Zerojne~ZFNotEqual/NotZerojsSFNegativejns~SFNonnegativejg~(SF^OF)&~ZFGreater(Signed)jge~(SF^OF)GreaterorEqual(Signed)jl(SF^OF)Less(Signed)jle(SF^OF)|ZFLessorEqual(Signed)ja~CF&~ZFAbove(unsigned)jbCFBelow(unsigned)HunanUniversityConditionalBranchExample(OldStyle)intabsdiff(intx,inty){intresult;if(x>y){result=x-y;}else{result=y-x;}returnresult;}absdiff:pushl%ebpmovl%esp,%ebpmovl8(%ebp),%edxmovl12(%ebp),%eaxcmpl%eax,%edxjle.L6subl%eax,%edxmovl%edx,%eaxjmp.L7.L6:subl%edx,%eax.L7:popl%ebpretBody1SetupFinishBody2bBody2aabsdiff:pushl%ebpmovl%esp,%ebpmovl8(%ebp),%edxmovl12(%ebp),%eaxcmpl%eax,%edxjle.L6subl%eax,%edxmovl%edx,%eaxjmp.L7.L6:subl%edx,%eax.L7:popl%ebpretBody1SetupFinishBody2bBody2aConditionalBranchExample(Cont.)intgoto_ad(intx,inty){intresult;if(x<=y)gotoElse;result=x-y;gotoExit;Else:result=y-x;Exit:returnresult;}Callows“goto”asmeansoftransferringcontrolClosertomachine-levelprogrammingstyleGenerallyconsideredbadcodingstyleCCodeval=Test?Then_Expr:Else_Expr;GotoVersionnt=!Test; if(nt)gotoElse;val=Then_Expr;gotoDone;Else:val=Else_Expr;Done: ...GeneralConditionalExpressionTranslation
(UsingBranches)Testisexpressionreturninginteger=0interpretedasfalse≠0interpretedastrueCreateseparatecoderegionsforthen&elseexpressionsExecuteappropriateoneval=x>y?x-y:y-x;CCodetval=Then_Expr;result=Else_Expr;t=Test;if(t)result=tval;returnresult;UsingConditionalMovesConditionalMoveInstructionsInstructionsupports:if(Test)DestSrcSupportedinpost-1995x86processorsGCCdoesnotalwaysusethemWantstopreservecompatibilitywithancientprocessorsEnabledforx86-64Useswitch–march=686forIA32Why?BranchesareverydisruptivetoinstructionflowthroughpipelinesConditionalmovedonotrequirecontroltransferintcomvdiff(intx,inty){inttval=y-x;
intrval=x-y;inttest=x<y;if(test)rval=tval;resultrval;}absdiff:movl8(%ebp),%ecxmovl12(%ebp),%edxmovl%edx,%ebxsubl%ecx,%ebxmovl%ecx,%eaxsubl%edx,%eaxcmpl%edx,%ecxcmovl%ebx,%eaxConditionalMovesExampleabsdiff:pushl%ebpmovl%esp,%ebpmovl8(%ebp),%edxmovl12(%ebp),%eaxcmpl%eax,%edxjle.L6subl%eax,%edxmovl%edx,%eaxjmp.L7.L6:subl%edx,%eax.L7:popl%ebpretExpensiveComputationsBadCasesforConditionalMoveBothvaluesgetcomputedOnlymakessensewhencomputationsareverysimpleval=Test(x)?Hard1(x):Hard2(x);RiskyComputationsBothvaluesgetcomputedMayhaveundesirableeffectsval=p?*p:0;ComputationswithsideeffectsBothvaluesgetcomputedMustbeside-effectfreeval=x>0?x*=7:x+=3;TodayControl:ConditioncodesConditionalbranchesandmovesLoopsSwitchstatementsHunanUniversityCCodeintpcount_do(unsignedx){intresult=0;
do{result+=x&0x1;x>>=1;}while(x);returnresult;}GotoVersionintpcount_do(unsignedx){intresult=0;loop:result+=x&0x1;x>>=1;
if(x)gotoloop;returnresult;}“Do-While”LoopExampleCountnumberof1’sinargumentx(“popcount”)UseconditionalbranchtoeithercontinueloopingortoexitloopHunanUniversityRegisters:%edxx%ecxresultCCode“Do-While”LoopCompilation
movl $0,%ecx #result=0.L2: #loop:movl %edx,%eaxandl $1,%eax #t=x&1addl %eax,%ecx #result+=tshrl %edx #x>>=1jne .L2 #If!0,gotoloopHunanUniversityintpcount_do(unsignedx){intresult=0;do{result+=x&0x1;x>>=1;}while(x);returnresult;}CCodeforwhileloopCcodefordoloop“While”LoopExampleIswhileloopcodeequivalenttothedo-whileversion?Mustjumpoutofloopiftestfailsintpcount_while(unsignedx){intresult=0;
while(x){result+=x&0x1;x>>=1;}returnresult;}intpcount_do(unsignedx){intresult=0;do{result+=x&0x1;x>>=1;}
while(x)returnresult;}HunanUniversityCCodeGotoversion“While”LoopExampleIsthiscodeequivalenttothegotoversion?Mustjumpoutofloopiftestfailsintpcount_while(unsignedx){intresult=0;
while(x){result+=x&0x1;x>>=1;}returnresult;}intpcount_do(unsignedx){intresult=0;if(!x)gotodoneLoop:result+=x&0x1;x>>=1;if(x)gotoloopDone:returnresult;}HunanUniversityCCode“For”LoopExampleIsthiscodeequivalenttootherversions?#defineWSIZE8*sizeof(int)intpcount_for(unsignedx){inti;intresult=0;
for(i=0;i<WSIZE;i++){unsignedmask=1<<i;result+=(x&mask)!=0;}returnresult;}HunanUniversity“For”LoopFormfor(Init;Test;Update)BodyGeneralFormfor(i=0;i<WSIZE;i++){unsignedmask=1<<i;result+=(x&mask)!=0;}i=0i<WSIZEi++{unsignedmask=1<<i;result+=(x&mask)!=0;}InitTestUpdateBodyHunanUniversity“For”Loop…Gotofor(Init;Test;Update)BodyForVersionInit;while(Test){BodyUpdate;}WhileVersionInit;if(!Test)gotodone;doBodyUpdatewhile(Test);done:Init;if(!Test)gotodone;loop:BodyUpdateif(Test)gotoloop;done:HunanUniversityDo-whileVersionGotoVersionTodayControl:ConditioncodesConditionalbranchesandmovesLoopsSwitchstatementsSwitchStatementExampleMultiplecaselabelsHere:5&6FallthroughcasesHere:2MissingcasesHere:4longswitch_eg(longx,longy,longz){longw=1;switch(x){case1:w=y*z;break;case2:w=y/z;/*FallThrough*/case3:w+=z;break;case5:case6:w-=z;break;default:w=2;}returnw;}JumpTableStructureCodeBlock0Targ0:CodeBlock1Targ1:CodeBlock2Targ2:CodeBlockn–1Targn-1:???Targ0Targ1Targ2Targn-1???jtab:target=JTab[x];goto*target;switch(x){caseval_0:Block0caseval_1:Block1?
?
?caseval_n-1:Blockn–1}SwitchFormApproximateTranslationJumpTableJumpTargetsSwitchStatementExample(IA32)Setup:longswitch_eg(longx,longy,longz){longw=1;switch(x){...}returnw;}switch_eg:pushl %ebp #Setupmovl %esp,%ebp #Setupmovl 8(%ebp),%eax #%eax=xcmpl $6,%eax #Comparex:6ja .L8 #Ifunsigned>gotodefaultjmp *.L4(,%eax,4)#Goto*JTab[x]Whatrangeofvaluestakesdefault?NotethatwnotinitializedhereSwitchStatementExample(IA32)longswitch_eg(longx,longy,longz){longw=1;switch(x){...}returnw;}Indirect
jumpJumptable.section .rodata .align4.L4: .long .L8 #x=0 .long .L3 #x=1 .long .L5 #x=2 .long .L9 #x=3 .long .L8 #x=4 .long .L7 #x=5 .long .L7 #x=6Setup:switch_eg:pushl %ebp #Setupmovl %esp,%ebp #Setupmovl 8(%ebp),%eax #eax=xcmpl $6,%eax #Comparex:6ja .L8 #Ifunsigned>gotodefaultjmp *.L4(,%eax,4) #Goto*JTab[x]AssemblySetupExplanationTableStructureEachtargetrequires4bytesBaseaddressat.L4JumpingDirect:jmp.L2Jumptargetisdenotedbylabel.L2Indirect:jmp*.L4(,%eax,4)Startofjumptable:.L4Mustscalebyfactorof4(labelshave32-bits=4BytesonIA32)FetchtargetfromeffectiveAddress.L4+eax*4Onlyfor0≤x≤6Jumptable.section .rodata .align4.L4: .long .L8 #x=0 .long .L3 #x=1 .long .L5 #x=2 .long .L9 #x=3 .long .L8 #x=4 .long .L7 #x=5 .long .L7 #x=6.section .rodata .align4.L4: .long .L8 #x=0 .long .L3 #x=1 .long .L5 #x=2 .long .L9 #x=3 .long .L8 #x=4 .long .L7 #x=5 .long .L7 #x=6JumpTableJumptableswitch(x){case1://.L3w=y*z;break;case2://.L5w=y/z;/*FallThrough*/case3://.L9w+=z;break;case5:case6://.L7w-=z;break;default://.L8w=2;}CodeBlocks(x==1).L3:#x==1movl 12(%ebp),%eax#yimull 16(%ebp),%eax#w=y*zjmp .L2 #Gotodoneswitch(x){case1: //.L3w=y*z;break;...}HandlingFall-Throughlongw=1;...switch(x){... case2:w=y/z;/*FallThrough*/case3:w+=z;break;...}case2:w=y/z;gotomerge;case3:w=1;merge:w+=z;CodeBlocks(x==2,x==3).L5: #x==2movl 12(%ebp),%eax#ycltdidivl16(%ebp)#y/z
jmp.L6.L9: #x==3movl $1,%eax #w=1.L6: #merge:addl 16(%ebp),%eax#+=zjmp .L2 #Gotodonelongw=1;...switch(x){...case2: //.L3w=y*z;/*FallThrough*/case3://.L5w+=z;break;...}CodeBlocks(x==5,x==6,default).L7: #x==5,6movl $1,%eax#w=1subl 16(%ebp),%eax#w=1-zjmp.L2#gotodone.L8: #defaultmovl$2,%eax#w=2.L2: #doneswitch(x){...case5://.L7case6://.L7w-=z;break;default://.L8w=2;}SwitchCode(Finish)NoteworthyFeaturesJumptableavoidssequencingthroughcasesConstanttime,ratherthanlinearUsejumptabletohandleholesandduplicatetagsUseprogramsequencingtohandlefall-throughDon’tinitializew=1unlessreallyneeditreturnw;.L2: #done:popl
%ebpretIA32ObjectCodeSetupLabel.L8becomesaddress0x80484b8Label.L4becomesaddress0x804868008048480<switch_eg>:...8048489: 772d
ja80484b8<switch_eg+0x38>804848b: ff248580860408jmp*0x8048680(,%eax,4)switch_eg:...ja.L8 #Ifunsigned>gotodefaultjmp*.L4(,%eax,4) #Goto*JTab[x]AssemblyCodeDisassembledObjectCodeIA32ObjectCode(cont.)JumpTableDoesn’tshowupindisassembledcodeCaninspectusingGDBgdbswitch(gdb)x/7xw0x8048680Examine7hexadecimalformat“words”(4-byteseach)Usecommand“helpx”togetformatdocumentation0x8048680: 0x080484b8 0x08048492 0x0804849b 0x080484a40x8048690: 0x080484b8 0x080484ae 0x080484ae.section .rodata .align4.L4: .long .L8 #x=0 .long .L3 #x=1 .long .L5 #x=2 .long .L9 #x=3 .long .L8 #x=4 .long .L7 #x=5 .long .L7 #x=6IA32ObjectCode(cont.)DecipheringJumpTable0x8048680: 0x080484b8 0x08048492 0x0804849b 0x080484a40x8048690: 0x080484b8 0x080484ae 0x080484aeAddressValuex0x80486800x80484b800x80486840x804849210x80486880x804849b20x804868c0x80484a430x80486900x80484b840x80486940x80484ae50x80486980x80484ae6DisassembledTargets8048492:b8450c mov0xc(%ebp),%eax
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 佛山市道廣體育初級游泳救生培訓(xùn)試題復(fù)習(xí)測試卷含答案
- 互聯(lián)網(wǎng)營銷師五級練習(xí)測試題附答案
- 2025年觸電救援考試試題及答案
- 2025年口腔醫(yī)學(xué)三基試題及答案
- 2025年山東合格考歷史試題及答案
- 2025年防排煙自考試題及答案
- 2025年交往能力測試題型及答案
- 2025年專注力燈光測試題及答案
- 2025年戲?qū)Э荚嚨谋硌菰囶}及答案
- 2025年吸氣挑戰(zhàn)測試題及答案
- 小學(xué)體育與健康教育科學(xué)二年級下冊第一章體育基本活動能力立定跳遠教案 省一等獎
- 工程分包管理計劃
- 民事訴訟法學(xué)整套ppt課件完整版教學(xué)教程最全電子講義(最新)
- 2022義務(wù)教育小學(xué)科學(xué)課程標(biāo)準(zhǔn)(2022版)解讀(面向核心素養(yǎng)的科學(xué)教育)
- 河北省自然科學(xué)基金資助項目申請書模板
- 四年級奧數(shù)-容斥問題
- 常用標(biāo)準(zhǔn)波導(dǎo)和法蘭尺寸
- 損益平衡點的計算方法
- 小學(xué)二年級下冊音樂-第4課聆聽《吉祥三寶》3--人音版(簡譜)(10張)ppt課件
- 民用建筑電氣設(shè)計規(guī)范(消防)
- 電廠熱力試驗工試題
評論
0/150
提交評論