Machine-Level Programming III:ProceduresSept 18, 2001Topics• IA32 stack discipline• Register saving conventions• Creating pointers to localvariablesclass07.ppt15-213“The course that gives CMU its Zip!”CS 213 F’01– 2 –class07.pptIA32 Stack• Region of memory managed withstack discipline• Register %esp indicates lowestallocated position in stack–i.e., address of top elementPushing• pushl Src• Fetch operand at Src• Decrement %esp by 4• Write operand at address givenby %espPopping• popl Dest• Read operand at address givenby %esp• Increment %esp by 4• Write to DestStackPointer%espStack GrowsDownIncreasingAddressesStack “Top”Stack “Bottom”CS 213 F’01– 3 –class07.ppt0x1080x10c0x1100x104555%esp213%eax2131230x104%edxStack Operation Examples0x1080x10c0x110555%esp213%eax1230x108%edxpushl %eax0x1080x10c0x110213%esp213%eax1230x108%edxpopl %edxCS 213 F’01– 4 –class07.pptProcedure Control FlowUse stack to support procedure call and returnProcedure call:call label Push return address on stack; Jump to labelReturn address value• Address of instruction beyond call• Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax–Return address = 0x8048553Procedure return:• ret Pop address from stack; Jump to addressCS 213 F’01– 5 –class07.ppt0x1080x10c0x1100x1040x804854e0x8048553123Procedure Call / Return Example0x1080x10c0x110%esp%eip1230x108call 8048b900x1080x10c0x110123ret804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax0x8048b900x104%esp%eip0x80485530x108%esp%eip%eip is program counterCS 213 F’01– 6 –class07.pptStack-Based LanguagesLanguages that Support Recursion• e.g., C, Pascal, Java• Code must be “Reentrant”–Multiple simultaneous instantiations of single procedure• Need some place to store state of each instantiation–Arguments–Local variables–Return pointerStack Discipline• State for given procedure needed for limited time–From when called to when return• Callee returns before caller doesStack Allocated in Frames• state for single procedure instantiationCS 213 F’01– 7 –class07.pptCall Chain ExampleCode Structureyoo(…){••who();••}who(…){••amI();••}amI(…){••amI();••}yoowhoamIamIamICall Chain• Procedure amI recursiveCS 213 F’01– 8 –class07.pptStackPointer%espyoowhoamIamIamI•••FramePointer%ebpStack GrowsIncreasingAddressesStack“Top”IA32 Stack StructureStack Growth• Toward lower addressesStack Pointer• Address of next availablelocation in stack• Use register %espFrame Pointer• Start of current stack frame• Use register %ebpCS 213 F’01– 9 –class07.pptIA32/Linux Stack FrameCallee Stack Frame (“Top” toBottom)• Parameters for called functions• Local variables–If can’t keep in registers• Saved register context• Old frame pointerCaller Stack Frame• Return address–Pushed by call instruction• Arguments for this callStack Pointer(%esp)Frame Pointer(%ebp)Return AddrSavedRegisters+LocalVariablesArgumentBuildOld %ebpArgumentsCallerFrameCS 213 F’01– 10 –class07.pptRevisiting swapvoid swap(int *xp, int *yp){ int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}int zip1 = 15213;int zip2 = 91125;void call_swap(){ swap(&zip1, &zip2);}call_swap:• • •pushl $zip2 # Global Varpushl $zip1 # Global Varcall swap• • •&zip2&zip1Rtn adr%espResultingStack•••CS 213 F’01– 11 –class07.pptRevisiting swapvoid swap(int *xp, int *yp){ int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}swap:pushl %ebpmovl %esp,%ebppushl %ebxmovl 12(%ebp),%ecxmovl 8(%ebp),%edxmovl (%ecx),%eaxmovl (%edx),%ebxmovl %eax,(%edx)movl %ebx,(%ecx)movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpretBodySetUpFinishCS 213 F’01– 12 –class07.pptswap Setup #1swap:pushl %ebpmovl %esp,%ebppushl %ebxypxpRtn adrOld %ebp%ebpResultingStack•••&zip2&zip1Rtn adr%espEnteringStack•••%ebp%espCS 213 F’01– 13 –class07.pptswap Setup #2swap:pushl %ebpmovl %esp,%ebppushl %ebxypxpRtn adrOld %ebp%ebpResultingStack•••&zip2&zip1Rtn adr%espEnteringStack•••%ebp%espCS 213 F’01– 14 –class07.pptswap Setup #3swap:pushl %ebpmovl %esp,%ebppushl %ebxypxpRtn adrOld %ebp%ebpResultingStack•••&zip2&zip1Rtn adr%espEnteringStack•••%ebpOld %ebx%espCS 213 F’01– 15 –class07.pptEffect of swap Setupswap:pushl %ebpmovl %esp,%ebppushl %ebxypxpRtn adrOld %ebp%ebp 0 4 8 12 OffsetResultingStack•••&zip2&zip1Rtn adr%espEnteringStack•••%ebpOld %ebx%espCS 213 F’01– 16 –class07.pptswap Finish #1movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpretypxpRtn adrOld %ebp%ebp 0 4 8 12 Offsetswap’sStack•••Old %ebx%esp-4 Observation• Saved & restored register %ebxypxpRtn adrOld %ebp%ebp 0 4 8 12 Offset•••Old %ebx%esp-4CS 213 F’01– 17 –class07.pptswap Finish #2movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpretypxpRtn adrOld %ebp%ebp 0 4 8 12 Offsetswap’sStack•••Old %ebx%esp-4 ypxpRtn adrOld %ebp%ebp 0 4 8 12 Offsetswap’sStack•••%espCS 213 F’01– 18 –class07.pptswap Finish #3movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpretypxpRtn adr%ebp 4 8 12 Offsetswap’sStack•••ypxpRtn adrOld %ebp%ebp 0 4 8 12 Offsetswap’sStack•••%esp%espCS 213 F’01– 19 –class07.pptswap Finish #4movl -4(%ebp),%ebxmovl %ebp,%esppopl %ebpret&zip2&zip1%espExitingStack•••%ebpObservation• Saved & restored register %ebx• Didn’t do so for %eax, %ecx, or %edxypxpRtn adr%ebp 4 8 12 Offsetswap’sStack•••%espCS 213 F’01– 20 –class07.pptRegister Saving ConventionsWhen procedure yoo calls who:• yoo is the caller, who is the calleeCan Register be Used for Temporary Storage?• Contents of register %edx overwritten by whoConventions• “Caller Save”–Caller saves temporary in its frame before calling• “Callee Save”–Callee saves temporary in its frame before usingyoo:• • •movl $15213, %edxcall whoaddl %edx, %eax• • •retwho:• • •movl 8(%ebp), %edxaddl $91125, %edx• • •retCS 213 F’01– 21 –class07.pptIA32/Linux Register Usage• Surmised by looking atcode examplesInteger Registers• Two have special uses%ebp, %esp• Three managed as callee-save%ebx, %esi, %edi–Old values saved onstack prior to using• Three managed as caller-save%eax, %edx, %ecx–Do what you please, butexpect any callee to doso, as well• Register %eax also storesreturned
View Full Document