




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第3章程序的機器級表示:
——控制
計算機組成與結構
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,影響標志位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,影響標志位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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 八年級數學下學期期中模擬卷(范圍:一次函數、代數方程、平行四邊形)-2024-2025學年八年級數學下學期期中考點大串講(滬教版)
- 2025年中小學教師面試考試試卷及答案
- 2024-2025學年成都市成華區初三二診英語試題(含答案)
- 幼兒教師個人計劃(16篇)
- 聘用退休人員合同(5篇)
- 《法制安全-護我成長》教學設計 八年級 主題班會
- 教師年度考核個人工作總結(3篇)
- 清明安全主題班會課件
- 家長會的心得(20篇)
- 小學第9課 沿軌跡行走的機器人教案
- 彩票行業風險管理-深度研究
- 加油站的智能監控與安全保障
- 煙草證委托經營管理協議書
- 2025豬場轉讓合同范本
- 生育友好醫院建設指南2024
- 2025年度智能零售貨柜租賃與運營管理合同范本3篇
- 中醫外治法課件
- 常見血管活性藥物的臨床應用及注意事項考核試題及答案
- 碼頭操作管理制度
- 中國成人ICU鎮痛和鎮靜治療指南解讀
- 中國地質大學(北京)《GNSS測量原理及其應用》2022-2023學年第一學期期末試卷
評論
0/150
提交評論