DOC PREVIEW
Harvey Mudd CS 105 - Machine­Level Programming III: Procedures

This preview shows page 1-2-3-21-22-23-42-43-44 out of 44 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Machine-Level Programming III: ProceduresIA32 StackIA32 Stack PushingIA32 Stack PoppingStack Operation ExamplesProcedure Control FlowProcedure Call ExampleProcedure Return ExampleStack-Based LanguagesCall Chain ExampleStack FramesStack OperationSlide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19IA32/Linux Stack FrameRevisiting swapSlide 22swap Setup #1swap Setup #2swap Setup #3Effect of swap Setupswap Finish #1swap Finish #2swap Finish #3swap Finish #4Register Saving ConventionsSlide 32IA32/Linux Register UsageRecursive FactorialRfact Stack SetupRfact BodyRfact RecursionRfact ResultRfact CompletionPointer CodeCreating & Initializing PointerPassing PointerUsing PointerSummaryMachine-Level Programming III:ProceduresMachine-Level Programming III:ProceduresTopicsTopicsIA32 stack disciplineRegister saving conventionsCreating pointers to local variablesX86.3.pptCS 105“Tour of the Black Holes of Computing– 2 –CS 105IA32 StackIA32 StackRegion of memory managed with stack disciplineGrows toward lower addressesRegister %esp indicates lowest stack addressaddress of top elementStackPointer%espStack GrowsDownIncreasingAddressesStack “Top”Stack “Bottom”– 3 –CS 105IA32 Stack PushingIA32 Stack PushingPushingPushingpushl SrcFetch operand at SrcDecrement %esp by 4Write operand at address given by %espStack GrowsDownIncreasingAddressesStack “Top”Stack “Bottom”StackPointer%esp-4– 4 –CS 105IA32 Stack PoppingIA32 Stack PoppingPoppingPoppingpopl DestRead operand at address given by %espIncrement %esp by 4Write to DestStackPointer%espStack GrowsDownIncreasingAddressesStack “Top”Stack “Bottom”+4– 5 –CS 105%esp%eax%edx%esp%eax%edx%esp%eax%edx0x1045550x1080x1080x10c0x1100x104555213213123Stack Operation ExamplesStack Operation Examples0x1080x10c0x1105552131230x108 0x104pushl %eax0x1080x10c0x1102131230x104213popl %edx0x108213– 6 –CS 105Procedure Control FlowProcedure Control FlowUse stack to support procedure call and returnProcedure call:Procedure call:call label Push return address on stack; Jump to labelReturn address valueReturn 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:Procedure return:ret Pop address from stack; Jump to address– 7 –CS 105%esp%eip%esp%eip 0x804854e0x1080x1080x10c0x1100x1040x804854e0x8048553123Procedure Call ExampleProcedure Call Example0x1080x10c0x1101230x108804854e: e8 3d 06 00 00 call 8048b90 <main>8048553: 50 pushl %eax0x104%eip is program countercall 8048b900x80485530x8048b90– 8 –CS 105%esp%eip0x104%esp%eip 0x80485910x80485910x1040x1040x1080x10c0x1100x8048553123Procedure Return ExampleProcedure Return Example0x1080x10c0x110123ret8048591: c3 ret0x108%eip is program counter0x80485530x8048553– 9 –CS 105Stack-Based LanguagesStack-Based LanguagesLanguages that Support RecursionLanguages 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 DisciplineStack DisciplineState for given procedure needed for limited timeFrom when called to when returnCallee returns before caller doesStack Allocated in Stack Allocated in FramesFramesstate for single procedure instantiation– 10 –CS 105Call Chain ExampleCall Chain ExampleCode StructureCode Structureyoo(…){••who();••}who(…){• • •amI();• • •amI();• • •}amI(…){••amI();••}yoowhoamIamIamICall ChainProcedure amI recursiveamI– 11 –CS 105StackPointer%espyoowhoprocFramePointer%ebpStack“Top”Stack FramesStack FramesContentsContentsLocal variablesReturn informationTemporary spaceManagementManagementSpace allocated when enter procedure“Set-up” code (p rologue)Deallocated when return“Finish” code (epilogue)PointersPointersStack pointer %esp indicates stack topFrame pointer %ebp indicates start of current frameamI– 12 –CS 105StackPointer%espyoo•••FramePointer%ebpStack OperationStack OperationyooCall Chainyoo(…){••who();••}– 13 –CS 105StackPointer%espyoowho•••FramePointer%ebpStack OperationStack OperationyoowhoCall Chainwho(…){• • •amI();• • •amI();• • •}– 14 –CS 105StackPointer%espyoowhoamI•••FramePointer%ebpStack OperationStack OperationyoowhoamICall ChainamI(…){••amI();••}– 15 –CS 105StackPointer%espyoowhoamI•••FramePointer%ebpStack OperationStack OperationyoowhoamICall ChainamI(…){••amI();••}amIamI– 16 –CS 105StackPointer%espyoowhoamI•••FramePointer%ebpStack OperationStack OperationyoowhoamICall ChainamI(…){••amI();••}amIamI– 17 –CS 105StackPointer%espyoowho•••FramePointer%ebpStack OperationStack OperationyoowhoCall Chainwho(…){• • •amI();• • •amI();• • •}amIamIamI– 18 –CS 105StackPointer%espyoowho•••FramePointer%ebpStack OperationStack OperationyoowhoCall Chainwho(…){• • •amI();• • •amI();• • •}amIamIamIamI– 19 –CS 105yoo(…){••who();••}StackPointer%espyoo•••FramePointer%ebpStack OperationStack OperationyoowhoCall ChainamIamIamIamI– 20 –CS 105IA32/Linux Stack FrameIA32/Linux Stack FrameCurrent Stack Frame (“Top” Current Stack Frame (“Top” to Bottom)to Bottom)Parameters for function about to call“Argument build”Local variablesIf can’t keep in registersSaved register contextOld frame pointerCaller Stack FrameCaller Stack FrameReturn addressPushed by call instructionArguments for this callStack Pointer(%esp)Frame Pointer(%ebp)Return AddrSavedRegisters+LocalVariablesArgumentBuildOld %ebpArgumentsCallerFrame– 21 –CS 105Revisiting swapRevisiting 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•••Calling swap from call_swap– 22 –CS 105Revisiting swapRevisiting swapvoid swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0;}swap:pushl %ebpmovl %esp,%ebppushl %ebxmovl


View Full Document

Harvey Mudd CS 105 - Machine­Level Programming III: Procedures

Documents in this Course
Processes

Processes

25 pages

Processes

Processes

27 pages

Load more
Download Machine­Level Programming III: Procedures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Machine­Level Programming III: Procedures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Machine­Level Programming III: Procedures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?