Chapter 8: Virtual Machine II: Program Control

Size: px
Start display at page:

Download "Chapter 8: Virtual Machine II: Program Control"

Transcription

1 Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 Chapter 8: Virtual Machine II: Program Control Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation contains lecture materials that accompany the textbook The Elements of Computing Systems by Noam Nisan & Shimon Schocken, MIT Press, The book web site, features 13 such presentations, one for each book chapter. Each presentation is designed to support about 3 hours of classroom or self-study instruction. You are welcome to use or edit this presentation for instructional and non-commercial purposes. If you use our materials, we will appreciate it if you will include in them a reference to the book s web site. And, if you have any comments, you can reach us at tecs.ta@gmail.com Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 1

2 Efi Arazi School of Computer Science Digital Systems Construction Virtual Machine Part II: Program Control Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 2

3 Where we are at: Human Thought Abstract design Chapters 9, 12 abstract interface H.L. Language & Operating Sys. Compiler Chapters abstract interface Virtual Machine Software hierarchy VM Translator Chapters 7-8 abstract interface Assembly Language Assembler Chapter 6 abstract interface Machine Language Computer Architecture Chapters 4-5 Hardware hierarchy abstract interface Hardware Platform Gate Logic Chapters 1-3 abstract interface Chips & Logic Gates Electrical Engineering Physics Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 3

4 The big picture Some language Some Other language Jack language Some compiler Some Other compiler Jack compiler Chapters 9-13 VM language Implemented in Projects 7-8 VM implementation over CISC platforms VM imp. over RISC platforms VM emulator VM imp. over the Hack platform Chapters 7-8 CISC machine language RISC machine language A Java-based emulator is included in the course software suite written in a high-level language Hack machine language Chapters 1-6 CISC machine RISC machine other digital platforms, each equipped with its VM implementation Any computer Hack computer Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 4

5 Lecture plan Goal: Specify and implement a VM model and language Arithmetic // Boolean commands add add sub sub neg neg eq eq gt gt Previous lt lt lecture and and or or not not Memory access commands pop pop segment i push segment i Program flow flow commands label (declaration) goto goto (label) (label) if-goto (label) (label) Function calling commands function (declaration) call (a (a function) function) This lecture return (from (from a function) function) Method: (a) specify the abstraction (model s constructs and commands) (b) propose how to implement it over the Hack platform. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 5

6 Program structure and translation path (on the Hack-Jack platform) Jack source code: (example): class class Foo Foo static staticslist; static int staticslist; int x1, x1, x2, x2, x3; x3; Compiler method f1(argslist) f1(argslist) method int int f1(int f1(int x) x) var localslist; var int localslist; int a, a, b; b; method f2(argslist) f2(argslist) method void void f2(int f2(int x, x, int int y) y) var localslist; var int localslist; int a, a, b, b, c; c; function f3(argslist) f3(argslist) function int int f3(int f3(int u) u) var localslist; var int localslist; int x; x; class class Bar Bar static staticslist; static int staticslist; int y1, y1, y2; y2; function f1(argslist) function void f1(argslist) void f1(int f1(int u, u, int int v) v) method f2(argslist) f2(argslist) method void void f2(int f2(int x) x) var localslist; var int localslist; int a1, a1, a2; a2; Following compilation: Foo.vm argument local this that pointer Bar.vm f1 f2 f3 f1 f2 static argument local this that pointer VM translator argument local this that pointer temp constant VM translator argument local this that pointer Hack machine language code static argument local this that pointer VM files (one set of virtual segments for each instance of a running function) Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 6

7 The challenge ahead 2 x = ( b + b 4 a c) / 2a if if ~(a ~(a = 0) 0) x = (-b (-b + sqrt(power(b,2) 4 * a * c)) c)) / (2 (2 * a) a) else else x = - c / b In order to enable such high-level code we have to know how to handle: Arithmetic operations (previous lecture) Boolean operations (previous lecture) Program flow (this lecture, easy) Subroutines (this lecture, medium/rare) In the Jack/Hack platform: all these abstractions are delivered by the VM level. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 7

8 Program flow label c goto c if-goto c (pop (pop the the topmost element from from the the stack. If If it s it s not not zero, zero, jump) Implementation (by translation to assembly): Simple. label declarations and goto directives can be effected directly by assembly commands. Example: function function mult mult 2 push push constant constant 0 pop pop local local 0 push push argument argument 1 pop pop local local 1 label label loop loop push push local local 1 push push constant constant 0 eq eq if-goto if-goto end end push push local local 0 push push argument argument 0 add add pop pop local local 0 push push local local 1 push push constant constant 1 sub sub pop pop local local 1 goto goto loop loop label label end end push push local local 0 return return Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 8

9 Subroutines if if ~(a ~(a = 0) 0) x = (-b (-b + sqrt(power(b,2) 4 * a * c)) c)) / (2 (2 * a) a) else else x = - c / b Subroutines = a major programming artifact The primitive (given) language can be extended at will by user-defined commands ( AKA subroutines / functions / methods...) The primitive commands and the user-defined commands have the same look-and-feel Perhaps the most important abstraction delivered by programming languages. The challenge: to make the implementation of this abstraction as transparent as possible: A well-deigned system consists of a collection of black box modules, each executing its effect like magic (Steven Pinker, How The Mind Works) Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 9

10 Subroutines usage at the VM level (pseudo code) Call-and-return convention The caller pushes the arguments, calls the callee, then waits for it to return Before the callee terminates (returns), it must push a return value At the point of return, the callee s resources are recycled, and the caller s state is re-instated Caller s net effect: the arguments were replaced by the return value (just like with primitive operations) Behind the scene Recycling and re-instating subroutine resources and states is a major headache The VM implementation should manage it like magic The magic is stack-based, and is considered a great CS gem. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 10

11 Subroutine commands function g nvars (Here starts a function called g, g, which has has nvars local local variables) call g nargs (Invoke function g for for its its effect; nargs arguments have have been been pushed onto onto the the stack) Return (Terminate execution and and return control to to the the calling function) Implementation: Next few slides. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 11

12 Aside: The VM emulator (Java-based, included in the course software suite) Calling hierarchy Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 12

13 The calling protocol The caller s view: Before Before calling calling the the function, function, I must must push push as as many many arguments arguments as as necessary necessary onto onto the the stack stack Next, Next, I invoke invoke the the function function using using the the call callcommand command After After the the called called function function returns: returns: The The arguments arguments that that I pushed pushed before before the the call call have have disappeared from from the the stack, stack, and and a a return return value value (that (that always always exists) exists) appears appears at at the the top top of of the the stack stack All All my my memory memory segments segments (argument, (argument, local, local, static, static, ) ) are are the the same same as as before before the the call. call. function g nvars call call g nargs return Blue = function writer s responsibility Black = black box magic, supplied by the VM implementation. The callee s view: When When I start start executing, executing, my my argument argumentsegment has has been been initialized initialized with with actual actual argument argument values values passed passed by by the the caller caller My My local localvariables segment segment has has been been allocated allocated and and initialized initialized to to zero zero The The static staticsegment segment that that I see see has has been been set set to to the the static staticsegment segment of of the the VM VM file file to to which which I belong, belong, and and the the working working stack stack that that I see see is is empty empty Before Before returning, returning, I must must push push a a value value onto onto the the stack. stack. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 13

14 VM implementation view of the calling protocol When function f calls function g, I must: function g nvars call call g nargs return Save the return address Save the segment pointers of f Allocate, and initialize to 0, as many local variables as needed by g Set the local and argument segment pointers of g Transfer control to g. When g terminates and control should return to f, I must: Clear the arguments and other junk from the stack Restore the segments of f Transfer control back to f (jump to the saved return address). Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 14

15 The VM implementation housekeeping storage = the stack ARG LCL SP frames of all the functions up the calling chain argument 0 argument 1 argument n-1 return address saved LCL saved ARG saved THIS saved THAT local 0 local 1 local k-1 arguments pushed for the current function saved state of the calling function, used to return to and restore the segments of, the calling function upon returning from the current function local variables of the current function working stack of the current function Remember: at any typical point of time, some functions are waiting, and only the current function is running Shaded areas: irrelevant to the current function The current function sees only the top of the stack (AKA working stack) The rest of the stack holds the frozen states of all the functions up the calling hierarchy Physical storage details depend on the VM implementation. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 15

16 Example: a typical calling scenario function p(...) fact(4)... function fact(n) vars result,j; result=1; j=1; while j<=n result=mult(result,j); return result; function mult(x,y) vars sum,j; sum=0; j=y; while j>0 sum=sum+x; return sum; p call fact(4) waiting time call mult(1,2) call mult(2,3) call mult(6,4) 24 fact waiting waiting waiting return mult return mult return mult return Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 16

17 Behind the scene: just before "call mult" just after mult is entered just after mult returns ARG argument 0 (fact) argument 0 (fact) ARG argument 0 (fact) return addr (p) return addr (p) return addr (p) function p(...) fact(4)... LCL (p) ARG (p) THIS (p) THAT (p) LCL (p) ARG (p) THIS (p) THAT (p) LCL (p) ARG (p) THIS (p) THAT (p) LCL local 0 (fact) local 0 (fact) LCL local 0 (fact) function fact(n) vars result,j; result=1; j=1; while j<=n result=mult(result,j); return result; SP local 1 (fact) working stack (fact) argument 0 (mult) argument 1 (mult) ARG local 1 (fact) working stack (fact) argument 0 (mult) argument 1 (mult) return addr (fact) LCL (fact) SP local 1 (fact) working stack (fact) return value function mult(x,y) vars sum,j; sum=0; j=y; while j>0 sum=sum+x; return sum; LCL SP ARG (fact) THIS (fact) THAT (fact) local 0 (mult) local 1 (mult) Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 17

18 Implementing the call f n command frames of all the functions up the calling chain ARG argument 0 argument 1 argument n-1 return address saved LCL saved ARG saved THIS saved THAT LCL SP local 0 local 1 local k-1 Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 18

19 Implementing the function f k command frames of all the functions up the calling chain ARG argument 0 argument 1 argument n-1 return address saved LCL saved ARG saved THIS saved THAT LCL SP local 0 local 1 local k-1 Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 19

20 Implementing the return command frames of all the functions up the calling chain ARG argument 0 argument 1 argument n-1 return address saved LCL saved ARG saved THIS saved THAT LCL SP local 0 local 1 local k-1 Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 20

21 One more detail: bootstrapping A high-level jack program (AKA application) is a set of class files. By convention, one class must be called Main, and this class must have at least one function called main. The contract is such that when we tell the computer to execute the program, the function Main.main starts running Implementation: After the program is compiled, each class file is translated into a.vm file From the host platform s standpoint, the operating system is also a set of.vm files (AKA libraries ) that co-exist alongside the user s.vm files One of the OS libraries is called Sys.vm, which includes a function called init. This function starts with some OS initialization code (explained in Ch. 12), then it does call f and enters an infinite loop; if the code originates from Jack, f is Main.main Thus, to bootstrap, the VM implementation has to effect (e.g. in assembly), the following operations: SP SP = // // initialize the the stack pointer to to 0x0100 call call Sys.init // // the the initialization function Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 21

22 VM implementation over the Hack platform Extends the VM implementation proposed in the last lecture (Chapter 7) The result: a big assembly program with lots of agreed-upon symbols: Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 22

23 Proposed API Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 23

24 Perspective Benefits of the VM approach Code transportability: compiling for different platforms require replacing only the VM implementation Language inter-operability: code of multiple languages can be shared using the same VM Common software libraries Code mobility: Internet VM implementation over CISC platforms Some language Some compiler VM language VM imp. over RISC platforms Some Other language Some Other compiler CISC RISC machine machine language language VM emulator Jack compiler Translator written in a high-level language Hack Modularity: Improvements in the VM implementation are shared by all compilers above it Every new digital device with a VM implementation gains immediate access to an existing software base New programming languages can be implemented easily using simple compilers Benefits of managed code: Security VM Cons Array bounds, index checking, Add-on code Etc. Performance. Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 24

Virtual Machine (Part II)

Virtual Machine (Part II) Harvard University CS 101 Fall 2005, Shimon Schocken Virtual Machine (Part II) Elements of Computing Systems 1 Virtual Machine II (Ch. 8) Where we are at: Human Thought Abstract design Chapters 9, 12 H.L.

More information

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles.

Virtual Machine. Part II: Program Control. Building a Modern Computer From First Principles. Virtual Machine Part II: Program Control Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 8:

More information

Virtual Machine (Part II)

Virtual Machine (Part II) IDC Herzliya Shimon Schocken Virtual Machine (Part II) Shimon Schocken Spring 2005 Elements of Computing Systems 1 Virtual Machine II (Ch. 8) Lecture plan 2 x = ( b + b 4 a c) / 2a if if ~(a = 0) 0) x

More information

Motivation. Compiler. Our ultimate goal: Hack code. Jack code (example) Translate high-level programs into executable code. return; } } return

Motivation. Compiler. Our ultimate goal: Hack code. Jack code (example) Translate high-level programs into executable code. return; } } return Motivation Jack code (example) class class Main Main { { static static int int x; x; function function void void main() main() { { Inputs Inputs and and multiplies multiplies two two numbers numbers var

More information

Chapter 11: Compiler II: Code Generation

Chapter 11: Compiler II: Code Generation Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 Chapter 11: Compiler II: Code Generation www.idc.ac.il/tecs Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This

More information

Introduction: Hello, World Below

Introduction: Hello, World Below Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 www.idc.ac.il/tecs Introduction: Hello, World Below Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation

More information

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles. Virtual Machine Part I: Stack Arithmetic Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 7:

More information

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation: Where we are at: Virtual Machine Part I: Stack Arithmetic Human Thought Abstract design Chapters 9, 12 H.L. Language & Operating Sys. Compiler Chapters 10-11 Virtual Machine Software hierarchy Translator

More information

Chapter 10: Compiler I: Syntax Analysis

Chapter 10: Compiler I: Syntax Analysis Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 Chapter 10: Compiler I: Syntax Analysis www.idc.ac.il/tecs Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This

More information

Chapter 6: Assembler

Chapter 6: Assembler Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 www.idc.ac.il/tecs Chapter 6: Assembler Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation contains

More information

7. The Virtual Machine II: Flow Control 1

7. The Virtual Machine II: Flow Control 1 Chapter 7: The Virtual Machine 1 7. The Virtual Machine II: Flow Control 1 It s like building something where you don t have to order the cement. You can create a world of your own, your own environment,

More information

Compiler I: Sytnax Analysis

Compiler I: Sytnax Analysis Elements of Computing Systems, Nisan & Schocken, MIT Press www.idc.ac.il/tecs Compiler I: Sytnax Analysis Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation contains

More information

Chapter 9: High Level Language

Chapter 9: High Level Language Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 www.idc.ac.il/tecs Chapter 9: High Level Language Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation

More information

Compiler II: Code Generation Human Thought

Compiler II: Code Generation Human Thought Course map Compiler II: Code Generation Human Thought Abstract design Chapters 9, 12 abstract interface H.L. Language & Operating Sys. Compiler Chapters 1-11 abstract interface Virtual Machine Software

More information

Assembler Human Thought

Assembler Human Thought Where we are at: Assembler Human Thought Abstract design Chapters 9, 12 H.L. Language & Operating Sys. Compiler Chapters 10-11 Virtual Machine Software hierarchy VM Translator Chapters 7-8 Assembly Language

More information

Assembler. Building a Modern Computer From First Principles.

Assembler. Building a Modern Computer From First Principles. Assembler Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 6: Assembler slide 1 Where we are

More information

7. The Virtual Machine

7. The Virtual Machine Chapter 7: The Virtual Machine 1 7. The Virtual Machine The programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form

More information

Introduction: From Nand to Tetris

Introduction: From Nand to Tetris Introduction: From Nand to Tetris Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Introduction slide

More information

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken ( Course overview Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken (www.nand2tetris.org) Logistics Meeting time: 2:20pm-5:20pm, Tuesday Classroom: CSIE Room 101 Instructor: 莊永裕 Yung-Yu

More information

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (www.nand2tetris.org)

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (www.nand2tetris.org) Course overview Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken (www.nand2tetris.org) Logistics Meeting time: 2:20pm-5:20pm, Tuesday Classroom: CSIE Room 104 Instructor: 莊永裕 Yung-Yu

More information

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken ( Course overview Introduction to Computer Yung-Yu Chuang with slides by Nisan & Schocken (www.nand2tetris.org) Logistics Meeting time: 2:20pm-5:20pm, Tuesday Instructor: 莊永裕 Yung-Yu Chuang Webpage: http://www.csie.ntu.edu.tw/~cyy/introcs

More information

CSC 2400: Computer Systems. Using the Stack for Function Calls

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

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

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

Implementing Functions at the Machine Level

Implementing Functions at the Machine Level Subroutines/Functions Implementing Functions at the Machine Level A subroutine is a program fragment that... Resides in user space (i.e, not in OS) Performs a well-defined task Is invoked (called) by a

More information

IWKS 3300: NAND to Tetris Spring John K. Bennett. Assembler

IWKS 3300: NAND to Tetris Spring John K. Bennett. Assembler IWKS 3300: NAND to Tetris Spring 2018 John K. Bennett Assembler Foundations of Global Networked Computing: Building a Modern Computer From First Principles This course is based upon the work of Noam Nisan

More information

Elements of Computing Systems, Nisan & Schocken, MIT Press. Boolean Arithmetic

Elements of Computing Systems, Nisan & Schocken, MIT Press. Boolean Arithmetic Elements of Computing Systems, Nisan & Schocken, MIT Press www.idc.ac.il/tecs Boolean Arithmetic Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation contains lecture

More information

Machine (Assembly) Language Human Thought

Machine (Assembly) Language Human Thought Where we are at: Machine (Assembly) Language Human Thought Abstract design hapters 9, 12 abstract interface H.L. Language & Operating Sys. ompiler hapters 10-11 abstract interface Virtual Machine Software

More information

Compiler I: Syntax Analysis

Compiler I: Syntax Analysis Compiler I: Syntax Analysis Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 10: Compiler I:

More information

Machine (Assembly) Language

Machine (Assembly) Language Machine (Assembly) Language Building a Modern Computer From First Principles www.nand2tetris.org Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org, Chapter 4: Machine Language

More information

Course Administration

Course Administration Fall 2018 EE 3613: Computer Organization Chapter 2: Instruction Set Architecture Introduction 4/4 Avinash Karanth Department of Electrical Engineering & Computer Science Ohio University, Athens, Ohio 45701

More information

10. The Compiler II: Code Generation 1

10. The Compiler II: Code Generation 1 Chapter 10: The Compiler II: Code Generation 1 10. The Compiler II: Code Generation 1 This document describes the usage and input syntax of the Unix Vax-11 assembler As. As is designed for assembling code

More information

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame University of Illinois at Urbana-Champaign Dept. of Electrical and Computer Engineering ECE 220: Computer Systems & Programming Stack Frames Revisited Recall Why ISAs Define Calling Conventions A compiler

More information

Wednesday, October 15, 14. Functions

Wednesday, October 15, 14. Functions Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:

More information

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

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS Lectures 5 Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS 1 OOPS - What does this C code do? int foo(char *s) { int L = 0; while (*s++) { ++L; } return L; } 2

More information

CSCE 5610: Computer Architecture

CSCE 5610: Computer Architecture HW #1 1.3, 1.5, 1.9, 1.12 Due: Sept 12, 2018 Review: Execution time of a program Arithmetic Average, Weighted Arithmetic Average Geometric Mean Benchmarks, kernels and synthetic benchmarks Computing CPI

More information

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

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12 Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

More information

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

Lecture 5. Announcements: Today: Finish up functions in MIPS Lecture 5 Announcements: Today: Finish up functions in MIPS 1 Control flow in C Invoking a function changes the control flow of a program twice. 1. Calling the function 2. Returning from the function In

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

CSC 2400: Computer Systems. Using the Stack for Function Calls

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

CIT Week13 Lecture

CIT Week13 Lecture CIT 3136 - Week13 Lecture Runtime Environments During execution, allocation must be maintained by the generated code that is compatible with the scope and lifetime rules of the language. Typically there

More information

Functions in MIPS. Functions in MIPS 1

Functions in MIPS. Functions in MIPS 1 Functions in MIPS We ll talk about the 3 steps in handling function calls: 1. The program s flow of control must be changed. 2. Arguments and return values are passed back and forth. 3. Local variables

More information

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

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:

More information

Today. Putting it all together

Today. Putting it all together Today! One complete example To put together the snippets of assembly code we have seen! Functions in MIPS Slides adapted from Josep Torrellas, Craig Zilles, and Howard Huang Putting it all together! Count

More information

CSC 8400: Computer Systems. Using the Stack for Function Calls

CSC 8400: Computer Systems. Using the Stack for Function Calls CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

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

Review of Activation Frames. FP of caller Y X Return value A B C Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. See P&H 2.8 and 2.12, and

More information

MIPS Functions and Instruction Formats

MIPS Functions and Instruction Formats MIPS Functions and Instruction Formats 1 The Contract: The MIPS Calling Convention You write functions, your compiler writes functions, other compilers write functions And all your functions call other

More information

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.

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. 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.5 6 compute jump/branch targets memory PC +4 new pc Instruction Fetch

More information

MIPS Procedure Calls. Lecture 6 CS301

MIPS Procedure Calls. Lecture 6 CS301 MIPS Procedure Calls Lecture 6 CS301 Function Call Steps Place parameters in accessible location Transfer control to function Acquire storage for procedure variables Perform calculations in function Place

More information

ECE331: Hardware Organization and Design

ECE331: Hardware Organization and Design ECE331: Hardware Organization and Design Lecture 7: MIPs Decision-Making Instructions Working with Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Computers

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Procedures have different names in different languages Java:

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #9: MIPS Procedures 2006-07-11 CS 61C L09 MIPS Procedures (1) Andy Carle C functions main() { int i,j,k,m;... i = mult(j,k);... m =

More information

CS 61c: Great Ideas in Computer Architecture

CS 61c: Great Ideas in Computer Architecture MIPS Functions July 1, 2014 Review I RISC Design Principles Smaller is faster: 32 registers, fewer instructions Keep it simple: rigid syntax, fixed instruction length MIPS Registers: $s0-$s7,$t0-$t9, $0

More information

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

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

SPIM Procedure Calls

SPIM Procedure Calls SPIM Procedure Calls 22C:60 Jonathan Hall March 29, 2008 1 Motivation We would like to create procedures that are easy to use and easy to read. To this end we will discuss standard conventions as it relates

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls 1 Lecture Goals Challenges of supporting functions Providing information for the called function Function arguments and local variables Allowing the

More information

ECS 142 Project: Code generation hints

ECS 142 Project: Code generation hints ECS 142 Project: Code generation hints Winter 2011 1 Overview This document provides hints for the code generation phase of the project. I have written this in a rather informal way. However, you should

More information

Boolean Arithmetic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 2

Boolean Arithmetic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 2 From Nand to Tetris Building a Modern Computer from First Principles Chapter 2 Boolean Arithmetic These slides support chapter 2 of the book The Elements of Computing Systems By Noam Nisan and Shimon Schocken

More information

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to Stack Frames EE 357 Unit 8 Stack Frames Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to Every call to a subroutine will create a data structure

More information

EP1200 Introduktion till datorsystemteknik Tentamen tisdagen den 3 juni 2014, till 18.00

EP1200 Introduktion till datorsystemteknik Tentamen tisdagen den 3 juni 2014, till 18.00 EP1200 Introduktion till datorsystemteknik Tentamen tisdagen den 3 juni 2014, 14.00 till 18.00 Inga hjälpmedel är tillåtna utom de som följer med tentamenstexten Skriv kurskod, namn och personnummer på

More information

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University CS558 Programming Languages Winter 2018 Lecture 4a Andrew Tolmach Portland State University 1994-2018 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information

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

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

CS153: Compilers Lecture 8: Compiling Calls

CS153: Compilers Lecture 8: Compiling Calls CS153: Compilers Lecture 8: Compiling Calls Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 2 out Due Thu Oct 4 (7 days) Project 3 out Due Tuesday Oct 9 (12 days) Reminder:

More information

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

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

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

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs Slide Set 2 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section

More information

Compilers and computer architecture: A realistic compiler to MIPS

Compilers and computer architecture: A realistic compiler to MIPS 1 / 1 Compilers and computer architecture: A realistic compiler to MIPS Martin Berger November 2017 Recall the function of compilers 2 / 1 3 / 1 Recall the structure of compilers Source program Lexical

More information

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program Fall 2006 CS333: Computer Architecture University of Virginia Computer Science Michele Co Announcements Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly Homework #2 Due next Wednesday, Sept.

More information

EE 361 University of Hawaii Fall

EE 361 University of Hawaii Fall C functions Road Map Computation flow Implementation using MIPS instructions Useful new instructions Addressing modes Stack data structure 1 EE 361 University of Hawaii Implementation of C functions and

More information

Subprograms, Subroutines, and Functions

Subprograms, Subroutines, and Functions Subprograms, Subroutines, and Functions Subprograms are also called subroutines, functions, procedures and methods. A function is just a subprogram that returns a value; say Y = SIN(X). In general, the

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

Chapter 6: The Assembler The Assembler Hack Assembly-to-Binary Translation Specification

Chapter 6: The Assembler The Assembler Hack Assembly-to-Binary Translation Specification Chapter 6: The Assembler 1 1. Introduction Work in progress. 6. The Assembler 1 2. Hack Assembly-to-Binary Translation Specification This section gives a complete specification of the translation between

More information

Run-Time Data Structures

Run-Time Data Structures Run-Time Data Structures Static Structures For static structures, a fixed address is used throughout execution. This is the oldest and simplest memory organization. In current compilers, it is used for:

More information

CS64 Week 5 Lecture 1. Kyle Dewey

CS64 Week 5 Lecture 1. Kyle Dewey CS64 Week 5 Lecture 1 Kyle Dewey Overview More branches in MIPS Memory in MIPS MIPS Calling Convention More Branches in MIPS else_if.asm nested_if.asm nested_else_if.asm Memory in MIPS Accessing Memory

More information

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) Function Calls COS 217 Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site) 1 Goals of Today s Lecture Finishing introduction to assembly language o EFLAGS register

More information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

More information

CS143 - Written Assignment 4 Reference Solutions

CS143 - Written Assignment 4 Reference Solutions CS143 - Written Assignment 4 Reference Solutions 1. Consider the following program in Cool, representing a slightly over-engineered implementation which calculates the factorial of 3 using an operator

More information

High Level Language (Jack)

High Level Language (Jack) IWKS 3300: NAND to Tetris Spring 2018 John K. Bennett High Level Language (Jack) Foundations of Global Networked Computing: Building a Modern Computer From First Principles This course is based upon the

More information

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220 CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays 1 Memory Alignment Perhaps at some point in your MIPS assembly programming you tried to perform a lw and received an error

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #8: MIPS Procedures 2005-06-30 CS 61C L08 MIPS Procedures (1) Andy Carle Topic Outline Functions More Logical Operations CS 61C L08

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-17/cc/ Generation of Intermediate Code Outline of Lecture 15 Generation

More information

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

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types 1 Lecture 08 Introduction to the MIPS ISA + Procedure Calls in MIPS Longer instructions = more bits to address registers MIPS Datapath 6 bit opcodes... 2 MIPS Instructions are 32 bits More ways to address

More information

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls? Memory Allocation in C Functions in C Global data pointer: R4 Global and static variables Specify positive offsets Frame pointer: Points to current code block Negative offset Stack Pointer: Top of stack

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions Instructors: John Wawrzynek & Vladimir Stojanovic http://inst.eecs.berkeley.edu/~cs61c/fa15 1 Machine Interpretation Levels of Representation/Interpretation

More information

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

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H 2.8 and 2.12, and A.5-6 Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University Upcoming agenda PA1 due yesterday PA2 available and discussed during lab section this week

More information

Lectures 3-4: MIPS instructions

Lectures 3-4: MIPS instructions Lectures 3-4: MIPS instructions Motivation Learn how a processor s native language looks like Discover the most important software-hardware interface MIPS Microprocessor without Interlocked Pipeline Stages

More information

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design

Subroutines and Stack Usage on the MicroBlaze. ECE 3534 Microprocessor System Design Subroutines and Stack Usage on the MicroBlaze ECE 3534 Microprocessor System Design 1 MicroBlaze Subroutines Same idea as a C/C++ function There s no call instruction Instead, branch and link Example:

More information

CS 316: Procedure Calls/Pipelining

CS 316: Procedure Calls/Pipelining CS 316: Procedure Calls/Pipelining Kavita Bala Fall 2007 Computer Science Cornell University Announcements PA 3 IS out today Lectures on it this Fri and next Tue/Thu Due on the Friday after Fall break

More information

Topic 7: Activation Records

Topic 7: Activation Records Topic 7: Activation Records Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Storage Organization Stack Free Memory Heap Static Code 2 ELF file format example Executable Object

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. See P&H 2.8 and 2.12, and

More information

Lecture 7: Procedures

Lecture 7: Procedures Lecture 7: Procedures CSE 30: Computer Organization and Systems Programming Winter 2010 Rajesh Gupta / Ryan Kastner Dept. of Computer Science and Engineering University of California, San Diego Outline

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

More information

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

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9 Ziad Matni Dept. of Computer Science, UCSB Lecture Outline More on MIPS Calling Convention Functions calling functions

More information

IWKS 2300/5300 Fall John K. Bennett. Machine Language

IWKS 2300/5300 Fall John K. Bennett. Machine Language IWKS 2300/5300 Fall 2017 John K. Bennett Machine Language Assembly Language / Machine Code Abstraction implementation duality: Assembly language (the instruction set architecture) can be viewed as a (sort-of)

More information