




版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第3章程序的機(jī)器級(jí)表示
——
過(guò)程
計(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”P(pán)reviousFrameFramefor
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(此為高級(jí)內(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 船舶保養(yǎng)考試題及答案
- 2025年軍隊(duì)文職人員招聘之軍隊(duì)文職法學(xué)考前沖刺試卷A卷含答案
- 小升初科學(xué)測(cè)試題及答案
- 2019-2025年消防設(shè)施操作員之消防設(shè)備基礎(chǔ)知識(shí)能力檢測(cè)試卷B卷附答案
- 2019-2025年消防設(shè)施操作員之消防設(shè)備基礎(chǔ)知識(shí)??寄M試題(全優(yōu))
- 2019-2025年消防設(shè)施操作員之消防設(shè)備基礎(chǔ)知識(shí)基礎(chǔ)試題庫(kù)和答案要點(diǎn)
- 社保知識(shí)培訓(xùn)課件北京
- 語(yǔ)文小說(shuō)文本解讀技巧訓(xùn)練教案:以小說(shuō)圍城為例
- 辦公室人員基本信息表
- 寫(xiě)作技巧大揭秘:高中語(yǔ)文作文指導(dǎo)課程教案
- 2025年共青科技職業(yè)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)完整版
- 2025年上半年潛江市城市建設(shè)發(fā)展集團(tuán)招聘工作人員【52人】易考易錯(cuò)模擬試題(共500題)試卷后附參考答案
- 統(tǒng)編版語(yǔ)文二年級(jí)下冊(cè)15古詩(shī)二首 《曉出凈慈寺送林子方》公開(kāi)課一等獎(jiǎng)創(chuàng)新教學(xué)設(shè)計(jì)
- 旅游電子商務(wù)(第2版) 課件全套 周春林 項(xiàng)目1-8 電子商務(wù)概述-旅游電子商務(wù)數(shù)據(jù)挖掘
- 2025年安徽警官職業(yè)學(xué)院?jiǎn)握新殬I(yè)適應(yīng)性測(cè)試題庫(kù)帶答案
- 廣東廣東省錢(qián)幣學(xué)會(huì)招聘筆試歷年參考題庫(kù)附帶答案詳解
- 2025年福建省中職《英語(yǔ)》學(xué)業(yè)水平考試核心考點(diǎn)試題庫(kù)500題(重點(diǎn))
- 【課件】自然環(huán)境課件-2024-2025學(xué)年七年級(jí)地理下冊(cè)人教版
- 2025年河北省職業(yè)院校技能大賽智能節(jié)水系統(tǒng)設(shè)計(jì)與安裝(高職組)考試題庫(kù)(含答案)
- 2025-2030年中國(guó)蒸發(fā)器冷凝器行業(yè)發(fā)展?fàn)顩r及前景趨勢(shì)分析報(bào)告
- 2024年江西環(huán)境工程職業(yè)學(xué)院高職單招語(yǔ)文歷年參考題庫(kù)含答案解析
評(píng)論
0/150
提交評(píng)論