DOC PREVIEW
Princeton COS 217 - Function Calls

This preview shows page 1-2-3-23-24-25-26-46-47-48 out of 48 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 48 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 48 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 48 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 48 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 48 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 48 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 48 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 48 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 48 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 48 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 48 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Function CallsGoals of Today’s LectureDetailed ExampleSetting the EFLAGS RegisterEFLAGS Register & Condition CodesA Simple Assembly ProgramMain Parts of the ProgramFunction CallsImplementing Function CallsImplementing Function CallsProblem: Nested Function CallsNeed to Use a StackStack FramesHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureHigh-Level PictureFunction Call DetailsCall and Return InstructionsCall and Return InstructionsCall and Return InstructionsCall and Return InstructionsInput ParametersInput ParametersInput ParametersInput ParametersInput ParametersInput ParametersBase Pointer: EBPBase Pointer: EBPBase Pointer: EBPBase Pointer: EBPBase Pointer: EBPBase Pointer: EBPAllocation for Local VariablesUse of RegistersGCC/Linux ConventionA Simple ExampleA Simple ExampleA Simple ExampleConclusion1Function CallsProf. David AugustCOS 217Reading: Chapter 4 of “Programming From the Ground Up”(available online from the course Web site)2Goals of Today’s Lecture• Finishing introduction to assembly languageo EFLAGS register and conditional jumpso Addressing modes• Memory layout of the UNIX processo Data, BSS, roData, Texto Stack frames, and the stack pointer ESP• Calling functionso Call and ret commandso Placing arguments on the stacko Using the base pointer EBP3movl %edx, %eaxandl $1, %eaxje .elsejmp .endif.else:.endif:sarl $1, %edxmovl %edx, %eaxaddl %eax, %edxaddl %eax, %edxaddl $1, %edxaddl $1, %ecx.loop:cmpl $1, %edxjle .endloopjmp .loop.endloop:movl $0, %ecxDetailed Examplecount=0;while (n>1) {count++;if (n&1)n = n*3+1;elsen = n/2;}n %edxcount %ecx4Setting the EFLAGS Register• Comparison cmpl compares two integerso Done by subtracting the first number from the second– Discarding the results, but setting the eflags registero Example:– cmpl $1, %edx (computes %edx – 1)– jle .endloop (looks at the sign flag and the zero flag)• Logical operation andl compares two integerso Example: – andl $1, %eax (bit-wise AND of %eax with 1)– je .else (looks at the zero flag)• Unconditional branch jmpo Example: – jmp .endif and jmp .loop5EFLAGS Register & Condition CodesCF1PF0AF0ZFSFTFIFDFOFIOPLNT0RFVMACVIFVIPIDReserved (set to 0)012345678910111213141516171819202131 22Carry flagIdentification flagVirtual interrupt pendingVirtual interrupt flagAlignment checkVirtual 8086 modeResume flagNested task flagI/O privilege levelOverflow flagInterrupt enable flagDirection flagTrap flagSign flagZero flagAuxiliary carry flag or adjust flagParity flag6A Simple Assembly Program.section .text.globl _start_start:# Program starts executing# here# Body of the program goes# here# Program ends with an# “exit()” system call# to the operating systemmovl $1, %eaxmovl $0, %ebxint $0x80.section .data# pre-initialized # variables go here.section .bss# zero-initialized # variables go here.section .rodata# pre-initialized# constants go here7Main Parts of the Program• Break program into sections (.section)o Data, BSS, RoData, and Text• Starting the programo Making _start a global (.global _start)– Tells the assembler to remember the symbol _start – … because the linker will need ito Identifying the start of the program (_start)– Defines the value of the label _start• Exiting the programo Specifying the exit() system call (movl $1, %eax)– Linux expects the system call number in EAX registero Specifying the status code (movl $0, %ebx)– Linux expects the status code in EBX register o Interrupting the operating system (int $0x80)8Function Calls• Functiono A piece of code with well-defined entry and exit points, and a well-defined interface• “Call” and “Return” abstractionso Call: jump to the beginning of an arbitrary procedureo Return: jump to the instruction immediately following the “most-recently-executed” Call instruction• The jump address in the return operation is dynamically determined9Implementing Function CallsP: # Function P…jmp R # Call RRtn_point1:…Q: # Function Q…jmp R # Call RRtn_point2:…R: # Function R…jmp ??? # ReturnWhat should the return instruction in R jump to?10Implementing Function CallsP: # Proc Pmovl $Rtn_point1, %eaxjmp R # Call RRtn_point1:…Q: # Proc Qmovl $Rtn_point2, %eaxjmp R # Call RRtn_point2:…R: # Proc R…jmp %eax # ReturnConvention: At Call time, store return address in EAX11Problem: Nested Function CallsP: # Function Pmovl $Rtn_point1, %eaxjmp Q # Call QRtn_point1:…Q: # Function Qmovl $Rtn_point2, %eaxjmp R # Call RRtn_point2:…jmp %eax # ReturnR: # Function R…jmp %eax # Return• Problem if P calls Q, and Q calls R• Return address for P to Q call is lost12Need to Use a Stack• A return address needs to be saved for as long as the function invocation continues• Return addresses are used in the reverse order that they are generated: Last-In-First-Out• The number of return addresses that may need to be saved is not statically known• Saving return addresses on a Stack is the most natural solution13Stack Frames• Use stack for all temporary data related to each active function invocationo Return addresso Input parameterso Local variables of functiono Saving registers across invocations• Stack has one Stack Frame per active function invocationStack Frame14High-Level Picture• At Call time, push a new Stack Frame on top of the stack• At Return time, pop the top-most Stack Frame15High-Level Picturemain begins executingmain’sStack Frame0Bottom%ESP16High-Level Picturemain begins executingmain calls Pmain’sStack FrameP’sStack Frame0Bottom%ESP17High-Level Picturemain begins executingmain calls PP calls Qmain’sStack FrameP’sStack FrameQ’sStack Frame0Bottom%ESP18High-Level Picturemain begins executingmain calls PP calls QQ calls Pmain’sStack FrameP’sStack FrameP’sStack FrameQ’sStack Frame0Bottom%ESP19High-Level Picturemain begins executingmain calls PP calls QQ calls PP returnsmain’sStack FrameP’sStack FrameQ’sStack Frame0Bottom%ESP20High-Level Picturemain begins executingmain calls PP calls QQ calls PP returnsQ calls Rmain’sStack FrameP’sStack FrameR’sStack FrameQ’sStack Frame0Bottom%ESP21High-Level Picturemain begins executingmain calls PP calls QQ calls PP returnsQ calls RR returnsmain’sStack FrameP’sStack FrameQ’sStack Frame0Bottom%ESP22High-Level Picturemain begins executingmain calls PP


View Full Document

Princeton COS 217 - Function Calls

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Lecture

Lecture

21 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

Load more
Download Function Calls
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 Function Calls 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 Function Calls 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?