Procedures – Overview Lecture 19 Mon, Mar 28, 2005.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS 536 Spring Code generation I Lecture 20.
Accessing parameters from the stack and calling functions.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Semantics of Calls and Returns
Compiler Construction Recap Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Compiler Construction
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
CPSC 388 – Compiler Design and Construction Runtime Environments.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Recitation 2: Outline Assembly programming Using gdb L2 practice stuff Minglong Shao Office hours: Thursdays 5-6PM Wean Hall.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.
CSC 221 Computer Organization and Assembly Language
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Assembly Language: Function Calls Jennifer Rexford.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Section 5: Procedures & Stacks
Instructions for test_function
Assembly function call convention
Chapter 14 Functions.
Reading Condition Codes (Cont.)
C function call conventions and the stack
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Procedures: Building the Syntax Tree
High-Level Language Interface
Chapter 14 Functions.
Discussion Section – 11/3/2012
Machine-Level Programming 4 Procedures
Stack Frames and Advanced Procedures
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming III: Procedures Sept 18, 2001
EECE.3170 Microprocessor Systems Design I
Procedures – Building the Syntax Tree
Topic 3-a Calling Convention 1/10/2019.
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Miscellaneous Topics.
Computer Organization and Assembly Language
CSC 497/583 Advanced Topics in Computer Security
Corresponds with Chapter 5
Chapter 14 Functions.
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Procedures – Overview Lecture 19 Mon, Mar 28, 2005

Reading Read Chapter 6 of the Intel Developer’s Manual, Vol. 1.

Functions and Function Calls A function definition includes A return type. A function name. A list of formal parameters. A block of local variables. Statements, including optional return statements. Each formal parameter and each local variable is introduced as a declaration.

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; }

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; } Return type

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; } Function name

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; } Formal parameter list

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; } Local variables

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; } Statements

Functions and Function Calls Example double average(int a, int b) { double sum; double avg; sum = a + b; avg = sum/2; return avg; } Return statement

Functions and Function Calls This is summarized in the productions func  fbeg stmts RBRACE fbeg  fname fargs LBRACE dcls fname  type ID | ID fargs  LPAREN args RPAREN | LPAREN RPAREN args  args COMMA arg | arg arg  type ID

Functions and Function Calls A function call includes A function name. A list of actual parameters. Each actual parameter is an expression (of the appropriate type).

Functions and Function Calls Example int a; int b; double c; int main() { a = 8; b = 16; c = average(-a, b + 10); print c; return 0; }

Functions and Function Calls Example int a; int b; double c; int main() { a = 8; b = 16; c = average(-a, b + 10); print c; return 0; } Function name

Functions and Function Calls Example int a; int b; double c; int main() { a = 8; b = 16; c = average(-a, b + 10); print c; return 0; } Actual parameters

Functions and Function Calls This is summarized in the productions expr  ID LPAREN RPAREN | ID LPAREN exprs RPAREN exprs  exprs COMMA expr | expr

Parameter Passing The actual parameters are pushed onto the stack, where they can be accessed by the called procedure. It is the responsibility of the calling procedure to push the parameters onto the stack before the CALL statement. Why?

Parameter Passing Before pushing the parameters. esp

Parameter Passing After pushing the parameters. The leftmost parameter is at the top. Param 3 Param 2 Param 1 Actual Parameters esp

The Procedure Call When the procedure is called, The instruction pointer eip is pushed onto the stack. The address of the procedure is loaded into the instruction pointer. This is handled automatically when the CALL instruction is executed.

The Procedure Call Before the procedure call. Param 3 Param 2 esp

The Procedure Call After the procedure call. Param 3 Param 2 Param 1 esp Return address

Creating the Stack Frame To create a new stack frame (activation record) for the procedure, Push ebp onto the stack to preserve the “old” base pointer. Move esp to ebp to preserve the “old” stack pointer and establish the base pointer of the stack frame. Subtract from esp the size of the block of local variables.

Creating the Stack Frame The code that creates the stack frame. push %ebp # Save old base ptr mov %esp,%ebp # Save old stack ptr sub $n,%esp # Create local block

The Procedure Call Just after executing CALL. ebp Param 3 Param 2 esp Return address ebp

The Procedure Call push %ebp # Save old base ptr ebp Param 3 Param 2 esp ebp Param 3 Param 2 Param 1 Return address Old ebp

The Procedure Call mov %esp,%ebp # Save old stack ptr Param 3 Param 2 ebp, esp Param 3 Param 2 Param 1 Return address Old ebp

The Procedure Call sub $n,%esp # Create local block Param 3 Param 2 ebp Return address Old ebp Local variables esp

The Procedure Call sub $n,%esp # Create local block Param 3 Param 2 ebp esp Stack Frame Param 3 Param 2 Param 1 Return address Old ebp Local variables

Accessing the Parameters With each parameter, there is associated an offset. The offset refers to the byte of the value that has the lowest address. The offset of a parameter is positive since the parameter is “below” the base pointer in the stack. For example, to access the parameter with offset 12, we use the indexed addressing mode. 12(%ebp)

Accessing the Parameters lea 12(%ebp),%eax Param 3 Param 2 Param 1 ebp Return address Old ebp Local variables esp ebp + 12 eax

Accessing Local Variables With each local variable, there is associated an offset. The offset of a local variable is negative since the local variable is “above” the base pointer in the stack. For example, to access the parameter with offset -4, we use the indexed addressing mode. -4(%ebp)

Accessing Local Variables lea -4(%ebp),%eax Param 3 Param 2 Param 1 ebp Return address Old ebp Local variables esp ebp - 4 eax

The Return Value When we return from a procedure, we first must pass the return value to the calling procedure. If the return value is an integer, then we move it to eax. If the return value is a double, then we move it to st(0) in the FPU. The calling procedure will go to eax or st(0) to retrieve the return value.

Returning from a Procedure When execution returns to the calling procedure, we must Clear the block of local variables from the stack. Restore the old stack pointer. Restore the old base pointer.

Returning from a Procedure The code that clears the stack frame. mov %ebp,%esp # Restore old stack ptr pop %ebp # Restore old base ptr ret # Restore old instr ptr

Returning from a Procedure Just before returning. ebp esp Stack Frame Param 3 Param 2 Param 1 Return address Old ebp Local variables

Returning from a Procedure mov %ebp,%esp # Restore old stack ptr Param 3 Param 2 Param 1 ebp, esp Return address Old ebp

Returning from a Procedure pop %ebp # Restore old base ptr esp ebp Param 3 Param 2 Param 1 Return address

Returning from a Procedure ret # Restore old instr ptr Param 3 Param 2 Param 1 esp ebp

Returning from a Procedure After execution returns to the calling function, the calling function must remove the parameters from the stack. This is done by simply adjusting the stack pointer. We must add to the stack pointer the number of bytes in the parameter block.

Returning from a Procedure Before removing the parameters. Param 3 Param 2 Param 1 esp

Returning from a Procedure After “removing” the parameters. Param 3 Param 2 Param 1 esp

Computing the Parameter Offsets When parsing the argument list of a function definition, we must keep count of the number of bytes used by the arguments. The SymbolTable class variable fArgSize is used for this. When we begin parsing the argument list, we must initialize fArgSize to 8. Why 8?

Computing the Parameter Offsets For each argument, we must Store the value of fArgSize as the offset of that argument in its IdEntry object. Add the argument size to fArgSize. Because the production args  args COMMA arg is left-recursive, the arguments are parsed from left to right.

Computing the Parameter Offsets The syntax tree for the actual parameter list is built from the bottom up and later traversed from the top down. Therefore, the parameters will be pushed onto the stack in reverse order, from right to left.

Computing the Parameter Offsets For example, if the parameter list is (int a, double b) then the offset of a is 8 and the offset of b is 12. int a double b ebp Return address Old ebp ebp + 8 ebp + 12

Computing the Local Variable Offsets When parsing the local variable declarations in the function definition, we must keep count of the number of bytes used by the local variables. The SymbolTable class variable dclSize is used for this. When we begin parsing the declarations, we must initialize dclSize to 0. Why 0?

Computing the Parameter Offsets For each local variable, we must Add the argument size to dclSize. Store the value of -dclSize as the offset of that argument in its IdEntry object. Because the production dcls  dcls dcl is left-recursive, the arguments are parsed from left to right.

Computing the Parameter Offsets For example, if the local variables are int a; double b; then the offset of b is -4 and the offset of a is -12. ebp Return address Old ebp double b int a ebp - 4 ebp - 12