Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Chapter 4 - MicroArchitecture
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
Computer Architecture CSCE 350
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.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Informationsteknologi Saturday, September 29, 2007 Computer Architecture I - Class 41 Today’s class More assembly language programming.
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)
MIPS Assembly Language I Computer Architecture CPSC 321 Andreas Klappenecker.
CS 536 Spring Code generation I Lecture 20.
Computer Structure - The Instruction Set (2) Goal: Implement Functions in Assembly  When executing a procedure (function in C) the program must follow.
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved The Microarchitecture Level.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
RISC Concepts, MIPS ISA and the Mini–MIPS project
Mic-1: Microarchitecture University of Fribourg, Switzerland System I: Introduction to Computer Architecture WS January 2006
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
The Microarchitecture Level The level above the digital logic level is the microarchitecture level.  Its job is to implement the ISA (Instruction Set.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
1 Introduction to JVM Based on material produced by Bill Venners.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
CS 352 : Computer Organization and Design University of Wisconsin-Eau Claire Dan Ernst Execution of Machine Code Machine State and Operations Apps O/S.
Computer Architecture Instruction Set Architecture Lynn Choi Korea University.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1.
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
The Microarchitecture Level
Lecture 4: MIPS Instruction Set
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
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.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Tanenbaum, Structured Computer Organization, Fifth Edition, (c) 2006 Pearson Education, Inc. All rights reserved The Microarchitecture Level.
ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation.
The Micro Architecture Level
Computer Architecture CSE 3322 Lecture 4 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/10/09
COMPUTER ORGANIZATION LECTURE 3: ISA YASSER MOHAMMAD.
Recap: Printing Trees into Bytecodes To evaluate e 1 *e 2 interpreter –evaluates e 1 –evaluates e 2 –combines the result using * Compiler for e 1 *e 2.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
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 Architecture Instruction Set Architecture
Computer Architecture Instruction Set Architecture
CS216: Program and Data Representation
MIC-1 SIMULATOR Programming in IJVM
CE 454 Computer Architecture
Lecture 4: MIPS Instruction Set
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
A Closer Look at Instruction Set Architectures
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Functions and Procedures
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
MIPS Instructions.
The University of Adelaide, School of Computer Science
Where is all the knowledge we lost with information? T. S. Eliot
Presentation transcript:

Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are no more accessible when the procedure has returned. The problem: Where should these variables be kept in memory?

Solutions (2) Solution 1: Give each variable its own memory address: but what if the procedures calls itself? Solution 2: Use a data structure called execution stack

Execution stack (1) (3) The execution stack is stored in an area of memory It is reserved for storing variables It is possible to push values on the top of the stack or pop values from the top A special register (SP = Stack Pointer) always contains the address of the top of the stack

Execution stacks (2) (4) How a stack solves the problem of procedure call ? A special register (LV) stores the base absolute address (position) in the stack from where local variables of the current procedure are stored. Local variables are referred by mean of a relative distance (offset) from the absolute address pointed by LV. The data structure between LV and SP is called local variable frame of the current procedure.

Example (1) (5) A procedure 'A', which has local variables a1,a2 and a3 is executing: a1 is at address LV, a2 is at LV+1, a3 is at LV+2 Execution stack Memory

Example (2) (6) 'A' calls procedure 'B', which has local variables b1, b2, b3, b4: 'B' local variables are pushed on the stack. LV is updated to point to the address where 'B' local variables start.

Example (3) (7) 'B' calls procedure 'C', which has local variables c1 and c2:

Example (4) (8) 'A' calls procedure 'D', which has local variables d1, d2, d3, d4 and d5, which are stored at the same location of the “not still available” 'B' and 'C' local variables:

Stack based machine (9) IJVM is a stack based machine The stack is also used to store operands during the computation of arithmetic expressions as well as the result; such use is called operand stack Beside access to the main memory, the IJVM only exposes the stack structure to the programmer (no registers are available and in any way directly accessible with the IJVM instruction set)

Operand stack (1) (10) Let a1, a2 and a3 be local variables representing integer numbers How to compute a1 = a2 + a3 on a stack based architecture? Note: We suppose to have an instruction that sums the two values on the top of the stack, and pushes the result back on the stack

Operand stack (2) (11) First values of a2 and a3 are pushed on the stack:

Operand stack (3) (12) Then we execute the instruction for the sum: it will pop the two top-most values on the stack, compute their sum and push the result back on the stack.

Operand stack (4) (13) Finally we save back the value at the top of the stack in a1:

Memory organization (14) CPP, LV and SP points to 4-byte words, PC points to a byte

IJVM ISA (1) (15) 20 Instructions Integer Arithmetic Instruction are composed of an operation code (opcode) and an optional operand (a memory offset or a constant)

IJVM ISA (2) (16) Types of instruction: Push a word on stack (LDC_W, ILOAD, BIPUSH) Pop a word from the stack, assign its value to a local variable (POP, ISTORE) Arithmetic (integer) and logic operations (IADD,ISUB,IAND,IOR) Conditional Branching, Jumps (IFEQ, IFLT, IF_ICMPEQ, GOTO) Swapping of values on top of the stack (SWAP) Duplicate value on top of the stack (DUP) Format conversion (prefix instruction WIDE) Method/Procedure call (INVOKEVIRTUAL) Return from a method (IRETURN) NOP INCC Adding a constant to a local variable (INCC)

IJVM ISA (3) (17)

Example Program (18) Instructions are replaced by Hex opcodes Local variables are stored on the stack and referenced by mean of an offset starting from the LV pointer Operand Stack evolution during execution Instructions are replaced by Hex opcodes JavaJava assembly IJVM program Labels in the assembly code are replaced by effective offsets in the IJVM code (0x0F = 15) F

INVOKEVIRTUAL (19) INVOKEVIRTUAL is used to call another method. This instruction allocates the space for the reference of the object (OBJREF) to be called, for the parameters and local variables (Parameter 1, 2, and 3), for the return address (INVOKEVIRTUAL's following instruction) and a pointer to the Caller's LV (pointer to the previous local variable frame) Finally it changes the value of the PC register so that it points to the code of the called method.

INVOKEVIRTUAL: Example (1) (20) The program calls the cmp(p1,p2) method, which accepts two parameters, compares them and returns: -1 if p1 < p2 0 if p1 = p2 1 if p1 > p2 The method is called with p1 = -1 and p2 = -10 : we expect that it returns 1..main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV Stack before INVOKEVIRTUAL, current procedure is main

INVOKEVIRTUAL: Example (2) (21) objref is a reference to the method stored in the constant pool: it's use is related to the way the Java Virtual Machine operates, but it is not really necessary in our IJVM.main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV objref

INVOKEVIRTUAL: Example (3) (22) The first parameter is pushed on the stack: p1 = -1.main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV objref

INVOKEVIRTUAL: Example (4) (23) The second parameter is pushed on the stack: p2 = -10.main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV objref -10

INVOKEVIRTUAL: Example (5) (24) The method cmp(-1, -10) is invoked.main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV objref -10

INVOKEVIRTUAL: Example (6) (25) Before executing the method, some operation must be done: allocate space for and assign values to local variables (temp), save the address of the instruction following the method call (return address) and the LV register (base pointer of the local variable frame of the caller), objref is overwritten with a pointer to address of the return address in the stack. LV is then updated to the current local variable frame start..method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp return address LV caller's LV (in this case main's LV) address of the instruction that follows INVOKEVIRTUAL

INVOKEVIRTUAL: Example (7) (26) The value of the first parameter is loaded on the stack: p1 = -1.method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp return address LV

INVOKEVIRTUAL: Example (8) (27) The value of the second parameter is loaded on the stack: p2 = -10.method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp return address LV -10

INVOKEVIRTUAL: Example (9) (28) Compute the difference (ISUB) between the two values on top of the stack. Push the result back on the stack..method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp return address LV 9

INVOKEVIRTUAL: Example (10) (29) Save the value on top of the stack in the temp variable. Note: This pulls the top-most value off the stack, so after we need to re-push it..method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address 9 LV

INVOKEVIRTUAL: Example (11) (30) Load the value of temp on the stack.method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address LV 9

INVOKEVIRTUAL: Example (12) (31) If the value at the top of the stack is negative (less than 0) jump to 'lt', else continue. The value on top is popped. In this case 9 > 0: we continue....method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address 9 LV

INVOKEVIRTUAL: Example (13) (32) temp is loaded back on the stack.method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address LV 9

INVOKEVIRTUAL: Example (14) (33) If the top of the stack is equal to zero jump to 'eq', else continue As we have 9 we continue....method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address LV 9

INVOKEVIRTUAL: Example (14) (34) The constant value 1 is pushed on the stack (this will be the return value of the cmp method).method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address LV 1

INVOKEVIRTUAL: Example (15) (35) We jump to 'done'. On top of the stack the return value 1 remains.method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address LV 1

IRETURN (36) IRETURN is used to exit a method and return to the caller This instruction deallocates the stack space reserved for the INVOKEVIRTUAL call, restores the values of the LV and PC registers and assures that the return value is on top of the stack.

IRETURN: Example (16) (37) We return to the caller....method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV link -10 temp: 9 return address LV 1

IRETURN: Example (17) (38) Values of the LV and SP registers are restored, the top of the stack contains the return value of the method, PC is restored to the saved value (return address) so that execution continues from the instruction following the INVOKEVIRTUAL call..method cmp(p1,p2).var temp.end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt:BIPUSH 1 GOTO done lt:BIPUSH -1 GOTO done eq:BIPUSH 0 done:IRETURN.end-method... SP LV temp: 9 return address LV 1

IRETURN: Example (18) (39) We push the constant 1 on the top of the stack.main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV 1 1

IRETURN: Example (19) (40) We compare the two top-most values on the stack. If they are equal (that's the case in this example) jump to 'EQ'..main... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ... EQ:HALT.end-main... SP LV 1 1