Storage in Programs. largest. address. address

Similar documents
Review of Activation Frames. FP of caller Y X Return value A B C

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Functions and Procedures

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Functions in MIPS. Functions in MIPS 1

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Course Administration

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Lecture 5. Announcements: Today: Finish up functions in MIPS

Today. Putting it all together

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

MIPS Procedure Calls. Lecture 6 CS301

CSE Lecture In Class Example Handout

CS64 Week 5 Lecture 1. Kyle Dewey

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

Instruction Set Architectures (4)

Implementing Procedure Calls

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Compiling Code, Procedures and Stacks

Procedures and Stacks

Run-time Environment

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

2. dead code elimination (declaration and initialization of z) 3. common subexpression elimination (temp1 = j + g + h)

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

ECE232: Hardware Organization and Design

CSE Lecture In Class Example Handout

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

EE 361 University of Hawaii Fall

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

MIPS Functions and the Runtime Stack

Lecture 6: Assembly Programs

Common Problems on Homework

ECE331: Hardware Organization and Design

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

ECE 30 Introduction to Computer Engineering

SPIM Procedure Calls

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls

MIPS Functions and Instruction Formats

CA Compiler Construction

6.004 Tutorial Problems L3 Procedures and Stacks

Code Generation. Lecture 19

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

MIPS Instruction Set

Instructions: Assembly Language

CS61C : Machine Structures

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Code Generation. Lecture 12

CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions

Q1: /14 Q2: /12 Q3: /8 Q4: /8. Total: /42

CS 61c: Great Ideas in Computer Architecture

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Lecture 7: Procedures and Program Execution Preview

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

LAB C Translating Utility Classes

Assembly Language: Function Calls

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

MIPS%Assembly% E155%

CS153: Compilers Lecture 8: Compiling Calls

Stacks and Procedures

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Assembly Language: Function Calls" Goals of this Lecture"

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Computer Architecture I Midterm I

Assembler. Lecture 8 CS301

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Assembly Language: Function Calls" Goals of this Lecture"

Instruction Set Architecture

CS3350B Computer Architecture MIPS Introduction

MIPS Assembly (Functions)

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

Procedure Call and Return Procedure call

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Compilers and computer architecture: A realistic compiler to MIPS

Compiling Techniques

Thomas Polzer Institut für Technische Informatik

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

CS 316: Procedure Calls/Pipelining

Anne Bracy CS 3410 Computer Science Cornell University

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

COMP2611: Computer Organization MIPS function and recursion

CENG3420 Lecture 03 Review

Transcription:

Storage in rograms Almost all operand storage used by programs is provided by memory. Even though registers are more efficiently accessed by instructions, there are too few registers to hold the stored values or working set of a program during execution. emory appears as a large, linear addressed array of storage locations. However the storage needs of a program are varied; so this large array is broken into regions that are managed differently. Figure 1 gives a typical organization of a program s storage in memory. Global variables are statically defined and accessible by all procedures. Since their number, size, and type are known at compile time, the compiler can generate extremely efficient code to access them. Unfortunately, heavy use of global variables leads to non-modular code that is difficult to maintain. Globals also lack the flexibility to support program mechanisms where the amount and type of storage required is not known at compile time. Dynamic (run-time) storage management allows proper allocation and deallocation of needed operand storage resulting from unpredictable execution behavior (e.g., calling patterns of procedures), varying input data set sizes, and dynamic storage needs not tied to procedure invocations. largest address smallest address rogram Storage stack storage heap storage static storage code storage Figure 1: rogram Storage ap A program s storage map begins with code storage followed by statically allocated variables (typically globals) which are fully defined, allocated, and possibly initialized by the compiler. The remaining space is divided between the heap which grows towards larger addresses and the stack which begins at the largest address and grows towards smaller addresses. Although large virtual address spaces obviate concerns over sharing available space between these two dynamically allocated spaces, most embedded platforms face this finite storage resource. Since much of data storage is associated with procedure invocations (procedure calling), a natural place to allocate this storage in on the stack. The stack s Last-n, 1 2/2/27

First-Out (LFO) behavior perfectly accommodates the needs of nested procedure calls. Consider a person working on a problem that contains many parts where the information needed to perform each part is placed on a separate piece of paper. The first page contains information for the highest level procedure in the problem. When a subproblem is reached, a new page is placed on top that contains information for that task. This process continues as lower level tasks are undertaken and more pages are added to the stack. When the top task is completed, the associated page is removed from the stack and work on the new top page is resumed. Additional subtasks (and pages) may be added and then removed until all work is completed and the initial page is removed. At all times, the information associated with the current task is on the page on top of the stack. This is exactly how storage is managed in procedural programming environments. n programs, storage required by a procedure is created as a block on the stack called an. This block includes storage for input parameters, the return value, and local variables. The functions Bar and Foo, shown in Figure 2, will illustrate how s are created and used. The function Bar requires an that contains two input parameters and, its return value, and three local variables,, and. Function Foo s contains two input parameters X and Y, a return value, and two locals A and B. int Bar(int, int ) { int Foo(int X, int *Y) { int,, ; int A, B = 15; int Foo(int, int *); A = X; = Foo(, &); *Y = B; return (); } return (A); } Figure 2: C functions Bar and Foo Activation frames are created on the stack in the order of procedure calls. The current procedure s is accessible by the frame pointer, typically a dedicated register in the processor. n S convention, the frame pointer is $3, while the stack pointer is $29. The use of a frame pointer allows compiler generated code to describe an unambiguous location for variables, regardless of how many calls to the procedure may be active at one time. For example, in recursive procedures, the stack may contain many s for the same procedure; each containing a complete set of required storage locations and a corresponding frame pointer. Figure 3 shows the state of the stack just before Bar initiates the call of Foo. 2 2/2/27

At this point, it would be possible to use the stack pointer access all activation frame slots. However there are several circumstances in which additional values are stored on the stack during Bar s execution, which would change stack offsets. Since the position of the frame pointer relative to the does not change, procedure variables can always be easily accessed relative to it. When Bar calls Foo, an must be allocated and initialized for Foo s Figure 3: Stack before initiating call of Foo input parameters, return value, and local variables. Since the compiler does not always have a called procedure s code when the calling procedure is processed, the natural place to put code to generate the is at the beginning of the procedure that will use the new frame. n this example, one might assume that Foo will create its own. This approach presents problems since Foo s input parameters must be initialized by Bar before Foo is called. f Foo deallocates its at its completion (when control is returned to Bar), Foo s return value will not persist for Bar to access it. For this reason, s are created in two parts: a caller generated part and a callee generate part. n this example, before Bar calls Foo, it allocates and initializes the caller portion of Foo s containing copies of passed parameters and a slot for the return value. Then Foo is called. When Foo begins execution, it creates the callee generation portion of the which contains room for local variables. Since the calling of Foo will overwrite Bar s frame pointer ($3) and return address ($31), a copy of these values must be preserved on the stack before Foo s is created. Figure 4 shows the stack just before Foo is called by Bar and after Foo is called and has generated the complete. f Foo should call another procedure, for example Bar, then another would be created on the stack below Foo s. Once Foo completes its execution, it places its return value in the last slot of the caller generated part of its. Then it deallocates the callee generate part which it created and returns to Bar. After restoring its frame pointer and return address, Bar can access Foo s returned value and deallocate the caller generate part of Foo s. 3 2/2/27

Bar s frame pointer +16 Bar s return address +12 X (copy of ) Y (address of ) Foo s return value Bar s frame pointer Bar s return address X (copy of ) Y (address of ) Foo s F Foo s return value A B Figure 4: Stack before Foo is called and after Foo s is complete. S assembly code for the calling of Foo by Bar is shown in Figure 5. t begins by allocating space for copies of Bar s frame pointer and return address plus storage for Foo s caller generate. As integers and pointers are four bytes long, this requires 5 x 4 = 2 bytes. Then Bar s frame pointer and return address are saved. statements in Bar addi $29, $29, -2 # allocate stack space sw $3, 16($29) # preserve sw $31, 12($29) # preserve Bar s RA lw $1, ($3) # copy into temp sw $1, 8($29) # store in Foo s AF addi $1, $3, # compute EA of sw $1, 4($29) # store in Foo s AF jal Foo # call Foo lw $3, 16($29) # restore lw $31, 12($29) # restore Bar s RA lw $1, ($29) # load Foo s RV sw $1, ($3) # store in statements in Bar Figure 5: S code for Bar's call of Foo n the C language, parameter passing is normally call-by-value. So a copy of is stored in the X slot of Foo s. Since Y is a pointer to support call 4 2/2/27

by reference, the effective address of is computed and stored in the Y slot. This pointer can be used by Foo to directly affect the storage in Bar s activation frame. Finally Foo is called using a jump and link instruction. When control returns from Foo, Bar restores its frame pointer and return address. Then it copies Foo s return value from Foo s into its own as. Finally, Foo s caller generated and preservation slots are deallocated. ote that when Bar is accessing its own storage, it uses its frame pointer. But when Bar is building Foo s, it uses the stack pointer. S code for Foo is shown in Figure 6. t begins by setting Foo s frame pointer to the current stack pointer. This is the bottom slot of the created by Bar (the return value). Foo then extends the frame with space for its local variables, in this example A and B. The remainder of Foo s code references operands using the frame pointer. B is initialized to 15 and then the statements of Foo are executed. When A = X; is executed, these variables are accessed in the. Caller accessible variables have positive offsets while local variables have negative offsets. When *Y = B; is executed, a value is stored using the pointer Y which points to in Bar s frame. This mechanism supports call-byreference parameter passing in the C language. At Foo s end, the return value slot is set and the stack space is deallocated. Foo: add $3, $29, $ # set Foo s F addi $29, $29, # allocate stack space addi $1, $, 15 # load init constant sw $1, ($3) # B = 15 lw $1, 8($3) # load X sw $1, ($3) # A = X lw $1, ($3) # load B lw $2, 4($3) # load Y sw $1, ($2) # store B in Bar s lw $1, ($3) # load A sw $1, ($3) # return (A) add $29, $3, $ # deallocate stack space jr $31 # return to caller Figure 6: S code for Foo 5 2/2/27