




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第3章程序的機(jī)器級表示
——
過程
計(jì)算機(jī)組成與結(jié)構(gòu)
2016年4月主講教師HunanUniversityTodayIA32ProceduresStackStructureCallingConventionsIllustrationsofRecursion&PointersIA32StackRegionofmemorymanagedwithstackdisciplineGrowstowardloweraddressesRegister%esp
contains
loweststackaddressaddressof“top”elementStackPointer:%espStackGrowsDownIncreasingAddressesStack“Top”Stack“Bottom”IA32Stack:Pushpushl
SrcFetchoperandatSrcDecrement%espby4Writeoperandataddressgivenby%esp-4StackGrowsDownIncreasingAddressesStack“Bottom”StackPointer:%espStack“Top”StackPointer:%espStackGrowsDownIncreasingAddressesStack“Top”Stack“Bottom”IA32Stack:Pop+4popl
DestReadvalueataddressgivenby%espIncrement%espby4StorevalueatDest(mustberegister)ProcedureControlFlowUsestacktosupportprocedurecallandreturnProcedurecall:calllabelPushreturnaddressonstackJumptolabelReturnaddress:AddressofthenextinstructionrightaftercallExamplefromdisassembly804854e:e83d060000call8048b90<main>8048553:50pushl%eaxReturnaddress=0x8048553Procedurereturn:retPopaddressfromstackJumptoaddress%esp%esp0x80485530x104%esp%eip%esp%eip0x8048b900x1080x10c0x1100x1040x804854e123ProcedureCallExample0x1080x10c0x1101230x108call8048b90804854e: e83d060000 call8048b90<main>8048553: 50 pushl%eax%eip:programcounter%esp%esp%esp%eip0x104%esp%eip0x80485910x1040x1080x10c0x1100x8048553123ProcedureReturnExample0x1080x10c0x110123ret8048591: c3 ret 0x1080x80485530x8048553%eip:programcounter0x104804854e: e83d060000 call8048b90<main>8048553: 50 pushl%eaxStack-BasedLanguagesLanguagesthatsupportrecursione.g.,C,Pascal,JavaCodemustbe“Reentrant”MultiplesimultaneousinstantiationsofsingleprocedureNeedsomeplacetostorestateofeachinstantiationArgumentsLocalvariablesReturnpointerStackdisciplineStateforgivenprocedureneededforlimitedtimeFromwhencalledtowhenreturnCalleereturnsbeforecallerdoesStackallocatedinFramesstateforsingleprocedureinstantiationFramePointer:%ebpStackFramesContentsLocalvariablesReturninformationTemporaryspaceManagementSpaceallocatedwhenenterprocedure“Set-up”codeDeallocatedwhenreturn“Finish”codeStackPointer:%espStack“Top”PreviousFrameFramefor
procCallChainExampleyoo(…){??who();??}who(…){???amI();???amI();???}amI(…){??amI();??}yoowhoamIamIamIExampleCallChainamIProcedureamI()
isrecursiveExampleyoowhoamIamIamIamIyoo%ebp%espStackyooyoo(…){??who();??}yoo(…){??who();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhowho(…){???amI();???amI();???}yoo(…){??who();??}who(…){???amI();???amI();???}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoamIamI(…){??amI();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoamIamIyoo(…){??who();??}who(…){???amI();???amI();???}amI(…){??amI();??}amI(…){??amI();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoamIamIamIyoo(…){??who();??}who(…){???amI();???amI();???}amI(…){??amI();??}amI(…){??amI();??}amI(…){??amI();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoamIamIyoo(…){??who();??}who(…){???amI();???amI();???}amI(…){??amI();??}amI(…){??amI();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoamIyoo(…){??who();??}who(…){???amI();???amI();???}amI(…){??amI();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoyoo(…){??who();??}who(…){???amI();???amI();???}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoamIyoo(…){??who();??}who(…){???amI();???amI();???}amI(…){??amI();??}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyoowhoyoo(…){??who();??}who(…){???amI();???amI();???}ExampleyoowhoamIamIamIamIyoo%ebp%espStackyooyoo(…){??who();??}IA32/LinuxStackFrameCurrentStackFrame(“Top”toBottom)“Argumentbuild:”
ParametersforfunctionabouttocallLocalvariables
Ifcan’tkeepinregistersSavedregistercontextOldframepointerCallerStackFrameReturnaddressPushedbycallinstructionArgumentsforthiscallReturnAddrSavedRegisters+LocalVariablesArgumentBuildOld%ebpArgumentsCallerFrameFramepointer
%ebpStackpointer%espCurrentFrameRevisitingswapvoidswap(int*xp,int*yp){intt0=*xp;intt1=*yp;*xp=t1;*yp=t0;}intcourse1=15213;intcourse2=18213;voidcall_swap(){swap(&course1,&course2);}call_swap: ???subl $24,%espmovl $course2,4(%esp)movl $course1,(%esp) call swap ???&course2&course1Rtnadr%espResultingStack???Callingswapfromcall_swap%esp%espsublcallRevisitingswapvoidswap(int*xp,int*yp){intt0=*xp;intt1=*yp;*xp=t1;*yp=t0;}swap:pushl %ebpmovl %esp,%ebppushl %ebxmovl 8(%ebp),%edxmovl 12(%ebp),%ecxmovl (%edx),%ebxmovl (%ecx),%eaxmovl %eax,(%edx)movl %ebx,(%ecx)popl %ebxpopl %ebp retBodySetUpFinishswapSetup#0swap:pushl%ebpmovl%esp,%ebppushl%ebxResultingStack&course2&course1Rtnadr%espEnteringStack???%ebpypxpRtnadr%ebp???%espswapSetup#1swap:pushl%ebpmovl%esp,%ebppushl%ebxResultingStack&course2&course1Rtnadr%espEnteringStack???%ebpypxpRtnadrOld%ebp%ebp???%espswapSetup#2swap:pushl%ebpmovl%esp,%ebppushl%ebxResultingStack&course2&course1Rtnadr%espEnteringStack???%ebpypxpRtnadrOld%ebp%ebp???%espswapSetup#3swap:pushl%ebpmovl%esp,%ebppushl%ebxResultingStack&course2&course1Rtnadr%espEnteringStack???%ebpypxpRtnadrOld%ebp%ebp???%espOld%ebxswapBodymovl8(%ebp),%edx#getxpmovl12(%ebp),%ecx#getyp...ResultingStack&course2&course1Rtnadr%espEnteringStack???%ebpypxpRtnadrOld%ebp%ebp???%espOld%ebxOffsetrelativeto%ebp1284swapFinishStackBeforeFinishpopl %ebxpopl %ebpypxpRtnadrOld%ebp%ebp???%espOld%ebxResultingStackypxpRtnadr???%ebp%espObservationSavedandrestoredregister%ebxNotsofor%eax,%ecx,%edxDisassembledswap08048490<swap>:8048490: 55 push%ebp8048491: 89e5 mov%esp,%ebp8048493: 53 push%ebx8048494: 8b5508 mov0x8(%ebp),%edx8048497: 8b4d0c mov0xc(%ebp),%ecx804849a: 8b1a mov(%edx),%ebx804849c: 8b01 mov(%ecx),%eax804849e: 8902 mov%eax,(%edx)80484a0: 8919 mov%ebx,(%ecx)80484a2: 5b pop%ebx80484a3: 5d pop%ebp80484a4: c3 ret8048426: c7442404189804movl$0x8049818,0x4(%esp)804842d: 08804842e:c704241c980408movl$0x804981c,(%esp) 8048435: e856000000call8048490<swap> 804843a: c9leave 804843b: c3ret CallingCodeTodayIA32ProceduresStackStructureCallingConventionsIllustrationsofRecursion&PointersRegisterSavingConventionsWhenprocedureyoocallswho:yooisthecallerwhoisthecalleeCanregisterbeusedfortemporarystorage?Contentsofregister%edxoverwrittenbywhoThiscouldbetrouble?somethingshouldbedone!Needsomecoordinationyoo: ???movl$15213,%edxcallwhoaddl%edx,%eax ???retwho: ???movl8(%ebp),%edxaddl$18213,%edx ???retRegisterSavingConventionsWhenprocedureyoocallswho:yooisthecallerwhoisthecalleeCanregisterbeusedfortemporarystorage?Conventions“CallerSave”Callersavestemporaryvaluesinitsframebeforethecall“CalleeSave”CalleesavestemporaryvaluesinitsframebeforeusingIA32/Linux+WindowsRegisterUsage%eax,%edx,%ecxCallersavespriortocallifvaluesareusedlater%eaxalsousedtoreturnintegervalue%ebx,%esi,%ediCalleesavesifwantstousethem%esp,%ebpspecialformofcalleesaveRestoredtooriginalvaluesuponexitfromprocedure%eax%edx%ecx%ebx%esi%edi%esp%ebpCaller-SaveTemporariesCallee-SaveTemporariesSpecialTodayIA32ProceduresStackStructureCallingConventions*IllustrationsofRecursion&Pointers(此為高級內(nèi)容)/*Recursivepopcount*/intpcount_r(unsignedx){if(x==0)return0;elsereturn(x&1)+pcount_r(x>>1);}RecursiveFunction黃色:調(diào)用者保存寄存器Registers%eax,%edxusedwithoutfirstsaving%ebxused,butsavedatbeginning&restoredatend綠色:被調(diào)用者保存寄存器pcount_r:pushl %ebpmovl %esp,%ebppushl %ebxsubl $20,%esp;ebx已占用棧4個(gè)字節(jié)movl 8(%ebp),%ebxmovl $0,%eaxtestl %ebx,%ebxje .L3movl %ebx,%eaxshrl %eaxmovl %eax,(%esp)call pcount_rmovl %ebx,%edxandl $1,%edxaddl %edx,%eax.L3:addl $20,%esppopl %ebxPopl %ebpret/*Recursivepopcount*/intpcount_r(unsignedx){if(x==0)return0;elsereturn(x&1)+pcount_r(x>>1);}RecursiveCall#1ActionsSaveoldvalueof%ebxonstackAllocatespaceforargumenttorecursivecallStorexin%ebxpcount_r:pushl %ebpmovl %esp,%ebppushl %ebxsubl $20,%espmovl 8(%ebp),%ebx???xRtnadrOld%ebp%ebp???%espOld%ebxx%ebx/*Recursivepopcount*/intpcount_r(unsignedx){if(x==0)return0;elsereturn(x&1)+pcount_r(x>>1);}RecursiveCall#2ActionsIfx==0,returnwith%eaxsetto0???movl $0,%eaxtestl%ebx,%ebxje .L3???.L3:???retx%ebx/*Recursivepopcount*/intpcount_r(unsignedx){if(x==0)return0;elsereturn(x&1)+pcount_r(x>>1);}RecursiveCall#3ActionsStorex>>1onstackMakerecursivecallEffect%eaxsettofunctionresult%ebxstillhasvalueofx???movl%ebx,%eaxshrl%eaxmovl%eax,(%esp)callpcount_r???RtnadrOld%ebp%ebp???%espOld%ebxx>>1x%ebx/*Recursivepopcount*/intpcount_r(unsignedx){if(x==0)return0;elsereturn(x&1)+pcount_r(x>>1);}RecursiveCall#4Assume%eaxholdsvaluefromrecursivecall%ebxholdsxActionsCompute(x&1)+computedvalueEffect%eaxsettofunctionresult???movl%ebx,%edxandl$1,%edxaddl%edx,%eax???x%ebx/*Recursivepopcount*/intpcount_r(unsignedx){if(x==0)return0;elsereturn(x&1)+pcount_r(x>>1);}RecursiveCall#5ActionsRestorevaluesof%ebxand%ebpRestore%esp???L3:addl $20,%esppopl %ebxpopl %ebpretRtnadrOld%ebp%ebp???%espOld%ebxOld%ebx%ebx%ebp???%espObservationsAboutRecursionHandledWithoutSpecialConsiderationStackframesmeanthateachfunctioncallhasprivatestorageSavedregisters&localvariablesSavedreturnpointerRegistersavingconventionspreventonefunctioncallfromcorruptinganother’sdataStackdisciplinefollowscall/returnpatternIfPcallsQ,thenQreturnsbeforePLast-In,First-OutAlsoworksformutualrecursionPcallsQ;QcallsPPointerCode/*Computex+3*/intadd3(intx){intlocalx=x;incrk(&localx,3);returnlocalx;}GeneratingPointeradd3createspointerandpassesittoincrk/*Incrementvaluebyk*/voidincrk(int*ip,intk){*ip+=k;}ReferencingPointer%espCreatingandInitializingLocalVariableintadd3(intx){intlocalx=x;incrk(&localx,3);returnlocalx;}VariablelocalxmustbestoredonstackBecause:NeedtocreatepointertoitComputepointeras-4(%ebp)Firstpartofadd3xRtnadrOld%ebp%ebp048-4localx=xUnused-12-8-16add3:pushl%ebpmovl %esp,%ebpsubl $24,%esp #Alloc.24bytesmovl 8(%
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 影視制作行業(yè)數(shù)字化后期處理流程
- 企業(yè)培訓(xùn)現(xiàn)場課件圖片
- 茶山茶樹種植與病蟲害防治合作合同
- 車輛貸款反擔(dān)保抵押合同
- 餐飲連鎖品牌加盟店經(jīng)營管理與品牌推廣合同
- 后院環(huán)境改造方案
- 魚池幕墻清洗方案
- 炒股投資風(fēng)險(xiǎn)管理及資金安全評估合同
- 醬酒銷售管理方案
- 柴油價(jià)格風(fēng)險(xiǎn)管理合作協(xié)議范本
- 2024執(zhí)業(yè)獸醫(yī)資格證考試真題及答案
- 鼠標(biāo)操作測試題及答案
- 2023年福建省松溪縣事業(yè)單位公開招聘輔警35名筆試題帶答案
- 浙江國企招聘2025紹興市鏡湖開發(fā)集團(tuán)有限公司下屬國企招聘11人筆試參考題庫附帶答案詳解
- 店鋪轉(zhuǎn)讓帶技術(shù)合同協(xié)議
- 2025年第九屆“學(xué)憲法、講憲法”活動(dòng)知識競賽測試題庫及答案
- 采棉機(jī)操作手冊和維護(hù)指南
- 放射狀角膜切開術(shù)并發(fā)癥的長期隨訪研究-全面剖析
- Excel表格公式培訓(xùn)
- 2025年山西省華遠(yuǎn)國際陸港集團(tuán)有限公司招聘筆試參考題庫含答案解析
- 塞爾維亞語教學(xué)與學(xué)習(xí)作業(yè)指導(dǎo)書
評論
0/150
提交評論