DOC PREVIEW
Princeton COS 217 - Assembly Functions

This preview shows page 1-2-22-23 out of 23 pages.

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

Unformatted text preview:

1 1 Assembly Language: !Function Calls"2 Goals of this Lecture"• Help you learn:"• The challenges of supporting functions"• Providing information for the called function"• Function arguments and local variables"• Allowing the calling function to continue where it left off"• Return address and contents of registers"• How to use the runtime stack"• Stack frame: args, local vars, return address, registers"• Stack pointer: pointing to the current top of the stack"• How to call functions"• Call and ret instructions, to call and return from functions"• Pushing and popping the stack frame"• Using the base pointer EBP as a reference point"2 3 Challenges of Supporting Functions"• Code with a well-defined entry and exit points"• Call: How does the CPU go to that entry point?"• Return: How does the CPU go back to the right place, when “right place” depends on who called the function?"• With arguments and local variables"• How are the arguments passed from the caller?"• Where should the local variables be stored?"• Providing a return value"• How is the return value returned to the calling function?"• Without changing variables in other functions"• How are the values stored in registers protected?"4 Call and Return Abstractions"• Call a function"• Jump to the beginning of an arbitrary procedure"• I.e., jump to the address of the functionʼs first instruction "• Return from a function"• Jump to the instruction immediately following the “most-recently-executed” Call instruction"P: # Function P … jmp R # Call R Rtn_point1: … R: # Function R … jmp Rtn_point1 # Return3 5 Challenge: Where to Return?"P: # Function P … jmp R # Call R Rtn_point1: … Q: # Function Q … jmp R # Call R Rtn_point2: … R: # Function R … jmp ??? # Return The same function may be called from many places. What addr should return instruction in R jump to? 6 Store Return Address in Register?"P: # Proc P movl $Rtn_point1, %eax jmp R # Call R Rtn_point1: … Q: # Proc Q movl $Rtn_point2, %eax jmp R # Call R Rtn_point2: … R: # Proc R … jmp %eax # Return Convention: At Call time, store return address in EAX4 7 Problem: Nested Function Calls"P: # Function P movl $Rtn_point1, %eax jmp Q # Call Q Rtn_point1: … Q: # Function Q movl $Rtn_point2, %eax jmp R # Call R Rtn_point2: … jmp %eax # Return R: # Function R … jmp %eax # Return • Problem if P calls Q, and Q calls R • Return address for P to Q call is lost 8 Solution: Put Return Address on a Stack"• May need to store many return addresses"• The number of nested functions is not known in advance"• A return address must be saved for as long as the function invocation continues"• Addresses used in reverse order "• E.g., function P calls Q, which then calls R"• Then R returns to Q which then returns to P"• So, need last-in-first-out data structure: A Stack"• Calling function pushes return address on the stack"• … and called function pops return address off the stack"EIP for P EIP for Q5 9 Arguments to the Function"• Calling function needs to pass arguments"• Cannot simply put arguments in a specific register"• Because function calls may $be nested"• So, put the arguments on the stack, too!"• Calling function pushes arguments on the stack"• Called function loads/stores- them on the stack"int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); } 10 Local Variables"• Local variables: called function has local variables"• Short-lived, so donʼt need a permanent location in memory"• Size known in advance, so donʼt need to allocate on the heap"• So, the function just uses the top of the stack"• Store local variables on the top of the stack"• The local variables disappear after the function returns"int add3(int a, int b, int c) { int d; d = a + b + c; return d; } int foo(void) { return add3(3, 4, 5); }6 11 Registers"• Registers"• Small, fast memory (e.g., directly on the CPU chip)"• Used as temporary storage for computations"• Cannot have separate registers per function"• Could have arbitrary number of nested functions"• Want to allow each function to use all the registers"• Could write all registers out to memory"• E.g., save values corresponding to program variables"• Possible, but a bit of a pain…"• E.g., find someplace to stash intermediate results"• Where would we put them?"• Instead, save the registers on the stack, too"12 Stack Frames"• Use stack for all temporary data related to each active function invocation"• Return address"• Input parameters"• Local variables of function"• Saving registers across invocations"• Stack has one Stack Frame per active function invocation"Stack Frame7 13 High-Level Picture"main begins executing main’s Stack Frame 0 Bottom %ESP 14 High-Level Picture"main begins executing main calls P main’s Stack Frame P’s Stack Frame 0 Bottom %ESP8 15 High-Level Picture"main begins executing main calls P P calls Q main’s Stack Frame P’s Stack Frame Q’s Stack Frame 0 Bottom %ESP 16 High-Level Picture"main begins executing main calls P P calls Q Q calls P main’s Stack Frame P’s Stack Frame P’s Stack Frame Q’s Stack Frame 0 Bottom %ESP9 17 High-Level Picture"main begins executing main calls P P calls Q Q calls P P returns main’s Stack Frame P’s Stack Frame Q’s Stack Frame 0 Bottom %ESP 18 High-Level Picture"main begins executing main calls P P calls Q Q calls P P returns Q calls R main’s Stack Frame P’s Stack Frame R’s Stack Frame Q’s Stack Frame 0 Bottom %ESP10 19 High-Level Picture"main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns main’s Stack Frame P’s Stack Frame Q’s Stack Frame 0 Bottom %ESP 20 High-Level Picture"main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns Q returns main’s Stack Frame P’s Stack Frame 0 Bottom %ESP11 21 High-Level Picture"main begins executing main calls P P calls Q Q calls P P returns Q calls R R returns Q returns P returns main’s Stack Frame 0 Bottom %ESP 22 High-Level Picture"main begins


View Full Document

Princeton COS 217 - Assembly Functions

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 Assembly Functions
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 Assembly Functions 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 Assembly Functions 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?