Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
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)
Binary Trees 2 Prof. Sin-Min Lee Department of Computer Science.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
CS2422 Assembly Language & System Programming October 26, 2006.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Semantics of Calls and Returns
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 20: Binary Trees.
Data Structures Using C++1 Chapter 11 Binary Trees.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
CS 1031 Tree Traversal Techniques; Heaps Tree Traversal Concept Tree Traversal Techniques: Preorder, Inorder, Postorder Full Trees Almost Complete Trees.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Data Structures : Project 5 Data Structures Project 5 – Expression Trees and Code Generation.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Starting at Binary Trees
CSC 221 Computer Organization and Assembly Language
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Decision Structures – Code Generation Lecture 24 Mon, Apr 18, 2005.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
Compiler Construction Code Generation Activation Records
1. Iterative Preorder Traversal Rpreorder(T) 1. [process the root node] if T!= NULL then Write Data(T) else Write “empty Tree” 2. [process the left subtree]
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
1 Assembly Language: Function Calls Jennifer Rexford.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Improvements to the Compiler Lecture 27 Mon, Apr 26, 2004.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Chapter 7 Trees_ Part2 TREES. Depth and Height 2  Let v be a node of a tree T. The depth of v is the number of ancestors of v, excluding v itself. 
SUYASH BHARDWAJ FACULTY OF ENGINEERING AND TECHNOLOGY GURUKUL KANGRI VISHWAVIDYALAYA, HARIDWAR.
Chapter 12 – Data Structures
Lecture No.14 Data Structures Dr. Sohail Aslam
Binary Search Tree (BST)
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
Procedures: Building the Syntax Tree
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
Stack Frames and Advanced Procedures
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Trees.
Assembly Language Programming II: C Compiler Calling Sequences
Microprocessor and Assembly Language
EECE.3170 Microprocessor Systems Design I
Procedures – Building the Syntax Tree
Chapter 6 - Procedures and Macros
Binary Tree Traversals
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Miscellaneous Topics.
CSC 497/583 Advanced Topics in Computer Security
Procedures and Macros.
Presentation transcript:

Procedures – Generating the Code Lecture 21 Mon, Apr 4, 2005

Code for a FUNC Syntax Tree The code generated by a FUNC syntax tree should output the text.text.globl _fname.def _fname;.scl 2;.type 32;.endef _fname: which the assembler uses to define the function name _fname.

Code for a FUNC Syntax Tree A FUNC syntax tree must also Save the old base pointer. Save the old stack pointer. Create the local block. The code should be push %ebp # Save base ptr mov %esp,%ebp # Save stack ptr sub $n,%esp # n bytes for locals

Code for a FEND Syntax Tree The code generated by a FEND syntax tree should Restore the old stack pointer (thereby clearing the local block). Restore the old base pointer. Return to the calling function. The code should be mov %ebp,%esp # Restore stack ptr pop %ebp # Restore base ptr ret # Return

Code for a RET Syntax Tree The code for a return is similar to the code for the end of a function, except There is a return value. The block level should not be changed. If the return value is an integer, it should be placed in register eax. Why not push it onto the stack? If the return value is a double, it should be placed (or left) in FPU register st(0).

Code for a CALL Syntax Tree Function calls are a bit more complicated since we must process the LIST tree. We must push onto the stack each parameter in the LIST tree. If we do a pre-order traversal, pushing as we go, in which order will the parameters be pushed?

Code for a CALL Syntax Tree First, we must distinguish the three cases: No parameter. One parameter. More than one parameter. What distinguishes the case of no parameter? What distinguishes the case of exactly one parameter? Anything else must be more than one parameter and will be handled recursively.

Code for a CALL Syntax Tree If the right subtree is null, then there is no parameter. If the right subtree is not a LIST tree, then it must be an expression tree representing one parameter. Push that parameter onto the runtime stack. If the right subtree is a LIST tree, then call on generateNodeCode() to handle it under the LIST case.

Code for a CALL Syntax Tree Incidentally, the CALL node may be either unary or binary. It is listed in the Utility class as binary. Therefore, when a CALL tree is printed, we will get the warning “attempted to print a null tree.” We may ignore it or we can prevent it by making CALL a special case.

Code for a LIST Syntax Tree While processing the parameter list, we must count the total number of bytes used by the parameters. Call this paramBlockSize. Before processing any parameters, The current value of paramBlockSize should be pushed onto a stack since a parameter list may include a function call itself. paramBlockSize should be set to 0.

Code for a LIST Syntax Tree In the LIST case, we simply do right-to-left pre-order traversal of the tree, pushing the parameters as we go. Each parameter is an expression, so we must call on traverseTree() to do a left-to-right post-order traversal of the expression tree.

Pseudocode for the LIST Case Call traverseTree() to handle the right subtree as an expression tree. Push the value onto the runtime stack. If the left subtree is a LIST tree, then Call generateNodeCode() to handle it recursively as a LIST tree. (Recursive case) Else Call traverseTree() to handle it as an expression tree. (Non-recursive case) Push the value onto the runtime stack.

Code for a CALL Syntax Tree Now, back to the CALL case. We still have to Do the call instruction Clear the parameters off the stack. Pass the return value back. The call instruction is of the form call _fname The name is stored in the left subtree.

Code for a CALL Syntax Tree To “clear” the parameters off the stack, we simply move the stack pointer. Add to the stack pointer the number of bytes occupied by the parameters. That value has been stored on the special stack designated for that purpose. Pop the number off the stack. Use it to adjust the stack pointer.

Code for a CALL Syntax Tree If the return value is a double, it is already in st(0), where it belongs. If it is a int, then it is on the stack and must be popped into eax.