In this lecture Global variables Local variables System stack

Slides:



Advertisements
Similar presentations
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Advertisements

Procedure Calls Prof. Sirer CS 316 Cornell University.
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
Procedure call frame: Hold values passed to a procedure as arguments
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
CSS 372 Lecture 1 Course Overview: CSS 372 Web page Syllabus Lab Ettiquette Lab Report Format Review of CSS 371: Simple Computer Architecture Traps Interrupts.
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)
Intro to Computer Architecture
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
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.
Computer Architecture Lecture 13 – part 2 by Engineer A. Lecturer Aymen Hasan AlAwady 7/4/2014 University of Kufa - Information Technology Research and.
Stack Operations LIFO structure (last-in,first-out) –The last value put into the stack is the first value taken out Runtime stack –A memory array that.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Computer Architecture Lecture 13 – part 1 by Engineer A. Lecturer Aymen Hasan AlAwady 31/3/2014 University of Kufa - Information Technology Research and.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
CSC 8505 Compiler Construction Runtime Environments.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois, Abinashi Dhungel.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Lecture 3 Translation.
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
The Stack.
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Microprocessor and Assembly Language
Functions and Procedures
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012
MIPS Instructions.
The University of Adelaide, School of Computer Science
Data structures.
Computer Architecture
Program and memory layout
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Procedures and Calling Conventions
Problem: ! bell ! help ! ! 1 ! 2 ! ! Bal help ! ! ! !
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Program and memory layout
Runtime Environments What is in the memory?.
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Computer Organization and Assembly Language
© Craig Zilles (adapted from slides by Howard Huang)
Basic Guarantees Right off the bat… what I'm going to describe is what almost certainly happens on contemporary machines, but there is absolutely nothing.
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

In this lecture Global variables Local variables System stack 18/09/2018 CMPUT 229

Global variables Exist for duration of program Accessible from any part of program registers too volatile Use memory data segment # ASSEMBLY PROGRAM # Global variables # follow. .data 18/09/2018 CMPUT 229

.word allocates 4-byte value Global variables Initialized global variables are declared with initial value int (32 bits) with .word and value other types according to size /* C PROGRAM */ /* declare var = -42 */ int var = -42; # ASSEMBLY PROGRAM # Global variables # follow. .data var: .word -42 value to store .word allocates 4-byte value 18/09/2018 CMPUT 229

.space allocates empty space Global variables Uninitialized global variables are allocated by specifying size only use .space directive with bytes /* C PROGRAM */ /* declare var = -42 */ int var = -42; /* empty has no initial value. */ int empty; # ASSEMBLY PROGRAM # Global variables # follow. .data var: .word -42 empty: .space 4 number of bytes .space allocates empty space 18/09/2018 CMPUT 229

contents (some uninitialized) Memory diagrams /* global variables */ int n = 42; int i; int *iptr = &i; start of data segment contents (some uninitialized) 0x10000000 0x10000004 0x10000008 lower addresses higher addresses variable names 42 n ??? i 0x10000004 iptr when variables contain addresses of other variables, helpful to draw arrow (pointer) 18/09/2018 CMPUT 229

Local variables Properties of local variables accessible only within function may have more than one variable with same name (in different functions) may have more than one version of the same function’s variables existing (recursion) Properties of data segment accessible from all of program all labels must be different each location can hold only one discrete value Data segment is not suited to storing local variables 18/09/2018 CMPUT 229

Local variables Properties of local variables must be allocated at function entry must be destroyed at function exit other functions may be called in between, with same rules void A() { int a; /* create a */ B(); /* destroy a */ } void B() { int b; /* create b */ C(); /* destroy b */ } void C() { int c; /* create c */ ... /* destroy c */ } 18/09/2018 CMPUT 229

Local variables Properties of local variables allocation/destruction obeys LIFO (last in, first out) principle like stack data structure stack is ideal data structure for storing local variables allocate a variable by pushing it on the stack destroy a variable by popping it off the stack stack also helpful for storing other function information, for same reason saving registers storing return address passing function arguments 18/09/2018 CMPUT 229

System stack Function call/return mechanism is very widespread among computers Most computers provide a stack in memory for programs to use called system stack or runtime stack or stack in MIPS, stack resides in its own segment of memory stack segment, to address 0x7FFFFFFF stack is initialized by operating system user programs push/pop stack as needed instruction set provides operations for doing this Which ? 18/09/2018 CMPUT 229

System stack Register $sp (stack pointer) indicates top of stack contains address of word of memory at top of stack (with lowest address) 18/09/2018 CMPUT 229

System stack lower addresses this is free space for the stack to grow into 0x7FFFB308 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB310 0x7FFFB314 all of these words are part of the stack 0x7FFFB318 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

to push another word (this one) onto the stack ... System stack: pushing lower addresses to push another word (this one) onto the stack ... 0x7FFFB308 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB310 0x7FFFB314 0x7FFFB318 subtract 4 from $sp ... 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

to push another word (this one) onto the stack ... System stack: pushing lower addresses to push another word (this one) onto the stack ... 0x7FFFB308 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB30C 0x7FFFB314 0x7FFFB318 subtract 4 from $sp ... 0x7FFFB31C higher addresses then store a value here 18/09/2018 CMPUT 229

System stack: popping lower addresses to pop a word (this one) off the stack ... 0x7FFFB308 ... fetch this word into a register ... 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB30C 0x7FFFB314 0x7FFFB318 ... then add 4 to $sp 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

System stack: popping lower addresses to pop a word (this one) off the stack ... 0x7FFFB308 ... fetch this word into a register ... 0x7FFFB30C 0x7FFFB310 $sp 0x7FFFB310 0x7FFFB314 0x7FFFB318 ... then add 4 to $sp 0x7FFFB31C higher addresses 18/09/2018 CMPUT 229

Examples programs stack1.a stack6.a 18/09/2018 CMPUT 229