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



Similar documents
Protocol Analysis / Analysis of Software Artifacts Kevin Bierhoff

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall Final Exam

Hillsborough Township Public Schools Mathematics Department Computer Programming 1

Small Business Networking

Polynomial Functions. Polynomial functions in one variable can be written in expanded form as ( )

How To Network A Smll Business

Small Business Networking

Section 5.2, Commands for Configuring ISDN Protocols. Section 5.3, Configuring ISDN Signaling. Section 5.4, Configuring ISDN LAPD and Call Control

Use Geometry Expressions to create a more complex locus of points. Find evidence for equivalence using Geometry Expressions.

Small Business Networking

Small Business Networking

ClearPeaks Customer Care Guide. Business as Usual (BaU) Services Peace of mind for your BI Investment

Mathematics. Vectors. hsn.uk.net. Higher. Contents. Vectors 128 HSN23100

EasyMP Network Projection Operation Guide

Algebra Review. How well do you remember your algebra?

LINEAR TRANSFORMATIONS AND THEIR REPRESENTING MATRICES

Data replication in mobile computing

Engineer-to-Engineer Note

Engineer-to-Engineer Note

License Manager Installation and Setup

Reasoning to Solve Equations and Inequalities

UNLOCKING TECHNOLOGY IVECO

Space Vector Pulse Width Modulation Based Induction Motor with V/F Control

Object Semantics Lecture 2

Small Business Cloud Services

Vendor Rating for Service Desk Selection

DEVELOPMENT. Introduction to Virtualization E-book. anow is the time to realize all of the benefits of virtualizing your test and development lab.

9.3. The Scalar Product. Introduction. Prerequisites. Learning Outcomes

Source Code verification Using Logiscope and CodeReducer. Christophe Peron Principal Consultant Kalimetrix

Unleashing the Power of Cloud

MATH 150 HOMEWORK 4 SOLUTIONS

Math 135 Circles and Completing the Square Examples

Factoring Polynomials

Enterprise Risk Management Software Buyer s Guide

How To Set Up A Network For Your Business

Section 7-4 Translation of Axes

Graphs on Logarithmic and Semilogarithmic Paper

PROF. BOYAN KOSTADINOV NEW YORK CITY COLLEGE OF TECHNOLOGY, CUNY

Introducing Kashef for Application Monitoring

5 a LAN 6 a gateway 7 a modem

Health insurance marketplace What to expect in 2014

Unambiguous Recognizable Two-dimensional Languages

Health insurance exchanges What to expect in 2014

The Semantics of Graph Programs

Model Checking for Software Architectures

Understanding Life Cycle Costs How a Northern Pump Saves You Money

How To Reduce Telecommunictions Costs

Network Configuration Independence Mechanism

CS99S Laboratory 2 Preparation Copyright W. J. Dally 2001 October 1, 2001

Binary Representation of Numbers Autar Kaw

9 CONTINUOUS DISTRIBUTIONS

Experiment 6: Friction

Regular Sets and Expressions

Homework 3 Solutions

SPECIAL PRODUCTS AND FACTORIZATION

VoIP for the Small Business

Java CUP. Java CUP Specifications. User Code Additions You may define Java code to be included within the generated parser:

Integration. 148 Chapter 7 Integration

VoIP for the Small Business

Operations with Polynomials

SE3BB4: Software Design III Concurrent System Design. Sample Solutions to Assignment 1

VoIP for the Small Business

VoIP for the Small Business

VoIP for the Small Business

VoIP for the Small Business

Basic Analysis of Autarky and Free Trade Models

Engineer-to-Engineer Note

On decidability of LTL model checking for process rewrite systems

AntiSpyware Enterprise Module 8.5

Project 6 Aircraft static stability and control

Example 27.1 Draw a Venn diagram to show the relationship between counting numbers, whole numbers, integers, and rational numbers.

RTL Power Optimization with Gate-level Accuracy

VoIP for the Small Business

VoIP for the Small Business

VoIP for the Small Business

Physics 43 Homework Set 9 Chapter 40 Key

Kofax Reporting. Administrator's Guide

SyGEMe: Integrated Municipal Facilities Management of Water Ressources Swiss Geoscience Meeting, Neuchâtel, 21 novembre 2009 k

Helicopter Theme and Variations

the printer and check the components

Lectures 8 and 9 1 Rectangular waveguides

Lecture 5. Inner Product

IFC3 India-Android Application Development

One Minute To Learn Programming: Finite Automata

How To Get A Free Phone Line From A Cell Phone To A Landline For A Business

Example A rectangular box without lid is to be made from a square cardboard of sides 18 cm by cutting equal squares from each corner and then folding

Econ 4721 Money and Banking Problem Set 2 Answer Key

Techniques for Requirements Gathering and Definition. Kristian Persson Principal Product Specialist

VoIP for the Small Business

Where is the nearest post office? 1.64 miles from the community, at the intersection of Timmons and Westheimer.

Recognition Scheme Forensic Science Content Within Educational Programmes

VoIP for the Small Business

5.2. LINE INTEGRALS 265. Let us quickly review the kind of integrals we have studied so far before we introduce a new one.

VoIP for the Small Business

Database-Backed Program Analysis for Scalable Error Propagation

All pay auctions with certain and uncertain prizes a comment

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 16 th May Time: 14:00 16:00

Lecture 3 Gaussian Probability Distribution


IaaS Configuration for Virtual Platforms

Transcription:

Virtul Mchine Prt II: Progrm Control Building Modern Computer From First Principles www.nnd2tetris.org Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 1

Where we re t: Humn Thought Abstrct design Chpters 9, 12 bstrct interfce H.L. Lnguge & Operting Sys. Compiler Chpters 10-11 bstrct interfce Virtul Mchine Softwre hierrchy VM Trnsltor Chpters 7-8 bstrct interfce Assembly Lnguge Assembler Chpter 6 bstrct interfce Mchine Lnguge Computer Architecture Chpters 4-5 Hrdwre hierrchy bstrct interfce Hrdwre Pltform Gte Logic Chpters 1-3 bstrct interfce Chips & Logic Gtes Electricl Engineering Physics Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 2

The big picture Some... lnguge Some Other lnguge... Jck lnguge Some compiler Some Other compiler Jck compiler Chpters 9-13 VM lnguge Implemented in Projects 7-8 VM implementtion over CISC pltforms VM imp. over RISC pltforms VM emultor VM imp. over the Hck pltform Chpters 7-8 CISC mchine lnguge RISC mchine lnguge A Jv-bsed emultor is included in the course softwre suite... written in high-level lnguge Hck mchine lnguge...... Chpters 1-6 CISC mchine RISC mchine other digitl pltforms, ech equipped with its VM implementtion Any computer Hck computer Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 3

The VM lnguge Gol: Complete the specifiction nd implementtion of the VM model nd lnguge Arithmetic / Boolen commnds dd dd sub sub neg neg eq eq gt previous gt lecture lt lt nd nd or or not not Memory ccess commnds pop pop x (pop (pop into into x, x, which which is is vrible) vrible) y (y (y being being vrible vrible or or constnt) constnt) Progrm flow flow commnds lbel lbel (declrtion) (declrtion) goto goto (lbel) (lbel) if-goto if-goto (lbel) (lbel) Function clling commnds function function (declrtion) (declrtion) cll cll return return ( ( function) function) this lecture (from (from function) function) Method: () specify the bstrction (model s constructs nd commnds) (b) propose how to implement it over the Hck pltform. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 4

The compiltion chllenge Source code (high-level lnguge) clss clss Min Min { { sttic sttic int int x; x; function function void void min() min() { { // // Inputs Inputs nd nd multiplies multiplies two two numbers numbers vr vr int int,, b, b, c; c; let let Keybord.redInt( Enter Keybord.redInt( Enter number ); number ); let let b b Keybord.redInt( Enter Keybord.redInt( Enter number ); number ); let let c c Keybord.redInt( Enter Keybord.redInt( Enter number ); number ); let let x x solve(,b,c); solve(,b,c); return; return; } } } } // // Solves Solves quderic quderic eqution eqution (sort (sort of) of) function function int int solve(int solve(int,, int int b, b, int int c) c) { { vr vr int int x; x; if if (~( (~( 0)) 0)) x(-b+sqrt(b*b 4**c))/(2*); x(-b+sqrt(b*b 4**c))/(2*); else else x-c/b; x-c/b; return return x; x; } } } } Our ultimte gol: Trnslte high-level progrms into executble code. Compiler Trget code 1110111111001000 1110111111001000 1110101010001000 1110101010001000 0000000000000000 0000000000000000 1111010011010000 1111010011010000 0000000000010010 0000000000010010 1110001100000001 1110001100000001 1110111111001000 1110111111001000 1110101010001000 1110101010001000 0000000000000000 0000000000000000 1111010011010000 1111010011010000 0000000000010010 0000000000010010 1110001100000001 1110001100000001...... Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 5

The compiltion chllenge / two-tier setting Jck source code if if (~( (~( 0)) 0)) x x (-b+sqrt(b*b 4**c))/(2*) (-b+sqrt(b*b 4**c))/(2*) else else x x -c/b -c/b Compiler We ll develop the compiler lter in the course We now turn to describe how to complete the implementtion of the VM lnguge Tht is -- how to trnslte ech VM commnd into ssembly commnds tht perform the desired semntics. VM (pseudo) code 0 0 eq eq if-goto if-goto elselbel elselbel b b neg neg b b b b cll cll mult mult 4 4 cll cll mult mult c c cll cll mult mult cll cll sqrt sqrt dd dd 2 2 cll cll mult mult div div pop pop x x goto goto contlble contlble elselbel: elselbel: c c neg neg b b cll cll div div pop pop x x contlble: contlble: VM trnsltor Mchine code 1110111111001000 1110111111001000 1110101010001000 1110101010001000 0000000000000000 0000000000000000 1111010011010000 1111010011010000 0000000000010010 0000000000010010 1110001100000001 1110001100000001 1110111111001000 1110111111001000 1110101010001000 1110101010001000 0000000000000000 0000000000000000 1111010011010000 1111010011010000 0000000000010010 0000000000010010 1110001100000001 1110001100000001 0000000000010010 0000000000010010 1110001100000001 1110001100000001...... Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 6

The compiltion chllenge Typicl compiler s source code input: // // Computes Computes x (-b (-b + sqrt(b^2 sqrt(b^2-4**c)) -4**c))/ 2* 2* if if (~( (~( 0)) 0)) x (-b (-b + sqrt(b sqrt(b * b 4 * * c)) c)) / (2 (2* ) ) else else x - c / b progrm flow logic (brnching) boolen expressions function cll nd return logic rithmetic expressions (this lecture) (previous lecture) (this lecture) (previous lecture) How to trnslte such high-level code into mchine lnguge? In two-tier compiltion model, the overll trnsltion chllenge is broken between front-end compiltion stge nd subsequent bck-end trnsltion stge In our Hck-Jck pltform, ll the bove sub-tsks (hndling rithmetic / boolen expressions nd progrm flow / function clling commnds) re done by the bck-end, i.e. by the VM trnsltor. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 7

Lecture pln Arithmetic / Boolen commnds dd dd sub sub neg neg eq eq gt gt previous lecture lt lt nd nd or or not not Memory ccess commnds pop pop x (pop (pop into into x, x, which which is is vrible) vrible) y (y (y being being vrible vrible or or constnt) constnt) Progrm flow flow commnds lbel lbel (declrtion) (declrtion) goto goto (lbel) (lbel) if-goto if-goto (lbel) (lbel) Function clling commnds function function (declrtion) (declrtion) cll cll return return ( ( function) function) (from (from function) function) Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 8

Progrm flow commnds in the VM lnguge VM code exmple: function function mult mult 1 constnt constnt 0 pop pop locl locl 0 lbel lbel loop loop rgument rgument 0 constnt constnt 0 eq eq if-goto if-goto end end rgument rgument 0 1 sub sub pop pop rgument rgument 0 rgument rgument 1 locl locl 0 dd dd pop pop locl locl 0 goto goto loop loop lbel lbel end end locl locl 0 return return In the VM lnguge, the progrm flow bstrction is delivered using three commnds: lbel c // // lbel lbel declrtion goto goto c // // unconditionl jump jump to to the the // // VM VMcommnd following the the lbel lbel c if-goto c // // pops pops the the topmost stck stck element; // // if if it s it s not not zero, zero, jumps to to the the // // VM VMcommnd following the the lbel lbel c How to trnslte these three bstrctions into ssembly? Simple: lbel declrtions nd goto directives cn be effected directly by ssembly commnds More to the point: given ny one of these three VM commnds, the VM Trnsltor must emit one or more ssembly commnds tht effects the sme semntics on the Hck pltfrom How to do it? see project 8. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 9

Lecture pln Arithmetic / Boolen commnds dd dd sub sub neg neg eq eq gt gt previous lecture lt lt nd nd or or not not Memory ccess commnds pop pop x (pop (pop into into x, x, which which is is vrible) vrible) y (y (y being being vrible vrible or or constnt) constnt) Progrm flow flow commnds lbel lbel (declrtion) (declrtion) goto goto (lbel) (lbel) if-goto if-goto (lbel) (lbel) Function clling commnds function function (declrtion) (declrtion) cll cll return return ( ( function) function) (from (from function) function) Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 10

Subroutines // // Compute Compute x (-b (-b + sqrt(b^2 sqrt(b^2-4**c)) -4**c))/ 2* 2* if if (~( (~( 0)) 0)) x (-b (-b + sqrt(b sqrt(b * b 4 * * c)) c)) / (2 (2* ) ) else else x - c / b Subroutines mjor progrmming rtifct Bsic ide: the given lnguge cn be extended t will by user-defined commnds ( k subroutines / functions / methods...) Importnt: the lnguge s primitive commnds nd the user-defined commnds hve the sme look-nd-feel This trnsprent extensibility is the most importnt bstrction delivered by high-level progrmming lnguges The chllenge: implement this bstrction, i.e. llow the progrm control to flow effortlessly between one subroutine to the other A well-designed system consists of collection of blck box modules, ech executing its effect like mgic (Steven Pinker, How The Mind Works) Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 11

Subroutines in the VM lnguge Clling code (exmple)...... // // computes computes (7 (7 + 2) 2) * 3-5 constnt constnt 7 constnt constnt 2 dd dd constnt constnt 3 cll cll mult mult constnt constnt 5 sub sub...... VM subroutine cll-nd-return commnds The invoction of the VM s primitive commnds nd subroutines follow exctly the sme rules: The cller es the necessry rgument(s) nd clls the commnd / function for its effect The clled commnd / function is responsible for removing the rgument(s) from the stck, nd for popping onto the stck the result of its execution. Clled code, k cllee (exmple) function function mult mult 1 constnt constnt 0 pop pop locl locl 0 // // result result (locl (locl 0) 0) 0 lbel lbel loop loop rgument rgument 0 constnt constnt 0 eq eq if-goto if-goto end end // // if if rg0 rg0 0, 0, jump jump to to end end rgument rgument 0 1 sub sub pop pop rgument rgument 0 // // rg0-- rg0-- rgument rgument 1 locl locl 0 dd dd pop pop locl locl 0 // // result result + + rg1 rg1 goto goto loop loop lbel lbel end end locl locl 0 // // result result return return Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 12

Function commnds in the VM lnguge function g nvrs // // here here strts function clled g, g, // // which hs hs nvrs locl locl vribles cll cll g nargs // // invoke function g for for its its effect; // // nargs rguments hve hve lredy been been ed onto onto the the stck stck return // // terminte execution nd nd return control to to the the cller Q: Why this prticulr syntx? A: Becuse it simplifies the VM implementtion (lter). Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 13

Function cll-nd-return conventions Clling function function function demo demo 3 3...... constnt constnt 7 7 constnt constnt 2 2 dd dd constnt constnt 3 3 cll cll mult mult...... clled function k cllee (exmple) function function mult mult 1 1 constnt constnt 0 0 pop pop locl locl 0 0 // // result result (locl (locl 0) 0) 0 0 lbel lbel loop loop...... // // rest rest of of code code ommitted ommitted lbel lbel end end locl locl 0 0 // // result result return return Although not obvious in this exmple, every VM function hs privte set of 5 memory segments (locl, rgument, this, tht, pointer) These resources exist s long s the function is running. Cll-nd-return progrmming convention The cller must the necessry rgument(s), cll the cllee, nd wit for it to return Before the cllee termintes (returns), it must return vlue At the point of return, the cllee s resources re recycled, the cller s stte is re-instted, execution continues from the commnd just fter the cll Cller s net effect: the rguments were replced by the return vlue (just like with primitive commnds) Behind the scene Recycling nd re-instting subroutine resources nd sttes is mjor hedche Some gent (either the VM or the compiler) should mnge it behind the scene like mgic In our implementtion, the mgic is VM / stck-bsed, nd is considered gret CS gem. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 14

The function-cll-nd-return protocol The cller s view: Before Before clling clling function function g, g, I must must onto onto the the stck stck s s mny mny rguments rguments s s needed needed by by g Next, Next, I invoke invoke the the function function using using the the commnd commnd cll cllg nargs nargs After After g returns: returns: The The rguments rguments tht tht I ed ed before before the the cll cll hve hve disppered disppered from from the the stck, stck, nd nd return return vlue vlue (tht (tht lwys lwys exists) exists) ppers ppers t t the the top top of of the the stck stck All All my my memory memory segments segments (locl, (locl, rgument, rgument, this, this, tht, tht, pointer) pointer) re re the the sme sme s s before before the the cll. cll. function function g nvrs nvrs cll cll g nargs nargs return return Blue VM function writer s responsibility Blck blck box mgic, delivered by the VM implementtion Thus, the VM implementtion writer must worry bout the blck opertions only. The cllee s (g s) view: When When I strt strt executing, executing, my my rgument rgumentsegment hs hs been been initilized initilized with with ctul ctul rgument rgument vlues vlues pssed pssed by by the the cller cller My My locl loclvribles segment segment hs hs been been llocted llocted nd nd initilized initilized to to zero zero The The sttic stticsegment segment tht tht I see see hs hs been been set set to to the the sttic stticsegment segment of of the the VM VM file file to to which which I belong, belong, nd nd the the working working stck stck tht tht I see see is is empty empty Before Before exiting, exiting, I must must vlue vlue onto onto the the stck stck nd nd then then use use the the commnd commnd return. return. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 15

The function-cll-nd-return protocol: the VM implementtion view When function f clls function g, the VM implementtion must: Sve the return ddress within f s code: the ddress of the commnd just fter the cll Sve the virtul segments of f Allocte, nd initilize to 0, s mny locl vribles s needed by g Set the locl nd rgument segment pointers of g Trnsfer control to g. function function g nvrs nvrs cll cll g nargs nargs return return When g termintes nd control should return to f, the VM implementtion must: Cler g s rguments nd other junk from the stck Restore the virtul segments of f Trnsfer control bck to f (jump to the sved return ddress). Q: How should we mke ll this work like mgic? A: We ll use the stck cleverly. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 16

The implementtion of the VM s stck on the host Hck RAM Globl stck: the entire RAM re dedicted to hold the stck Working stck: from SP onwrds: the stck tht the current function sees At ny point of time, only one function (the current function) is executing; other functions my be witing up the clling chin Shded res: irrelevnt to the current function The current function sees only the working stck, s well s its virtul memory segments The rest of the stck holds the frozen sttes of ll the functions up the clling hierrchy. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 17

Implementing the cll g nargs commnd cll cll g nargs nargs // // In In the the course course of of implementing the the code code of of f // //(the cller), cller), we we rrive rrive to to the the commnd commnd cll cll g nargs. nargs. // // we we ssume ssume tht tht nargs nargs rguments rguments hve hve been been ed ed // // onto onto the the stck. stck. Wht Wht do do we we do do next? next? // // We We generte generte symbol, symbol, let s let s cll cll it it returnaddress; // // Next, Next, we we effect effect the the following following logic: logic: returnaddress // // sves sves the the return return ddress ddress LCL LCL // // sves sves the the LCL LCL of of f ARG ARG // // sves sves the the ARG ARG of of f THIS THIS // // sves sves the the THIS THIS of of f THAT THAT // // sves sves the the THAT THAT of of f ARG ARG SP-nArgs-5 SP-nArgs-5 // // repositions SP SP for for g LCL LCL SP SP // // repositions LCL LCL for for g goto goto g // // trnsfers trnsfers control control to to g returnaddress: // // the the generted generted symbol symbol Implementtion: If the VM is implemented s progrm tht trnsltes VM code into ssembly code, the trnsltor must emit the bove logic in ssembly. None of this code is executed yet... At this point we re just generting code (or simulting the VM code on some pltform) Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 18

Implementing the function g nvrs commnd function function g nvrs nvrs // // to to implement implement the the commnd commnd function function g nvrs, nvrs, // // we we effect effect the the following following logic: logic: g: g: repet repet nvrs nvrs times: times: 0 Implementtion: If the VM is implemented s progrm tht trnsltes VM code into ssembly code, the trnsltor must emit the bove logic in ssembly. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 19

Implementing the return commnd return return // // In In the the course course of of implementing the the code code of of g, g, // // we we rrive rrive to to the the commnd commnd return. return. // // We We ssume ssume tht tht return return vlue vlue hs hs been been ed ed // // onto onto the the stck. stck. // // We We effect effect the the following following logic: logic: frme frme LCL LCL // // frme frme is is temp. temp. vrible vrible retaddr retaddr *(frme-5) *(frme-5)// // retaddr retaddr is is temp. temp. vrible vrible *ARG *ARG pop pop // // repositions the the return return vlue vlue // // for for the the cller cller SPARG+1 SPARG+1 // // restores restores the the cller s cller s SP SP THAT THAT *(frme-1) *(frme-1) // // restores restores the the cller s cller s THAT THAT THIS THIS *(frme-2) *(frme-2) // // restores restores the the cller s cller s THIS THIS ARG ARG *(frme-3) *(frme-3) // // restores restores the the cller s cller s ARG ARG LCL LCL *(frme-4) *(frme-4) // // restores restores the the cller s cller s LCL LCL goto goto retaddr retaddr // // goto goto returnaddress Implementtion: If the VM is implemented s progrm tht trnsltes VM code into ssembly code, the trnsltor must emit the bove logic in ssembly. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 20

Bootstrpping A high-level jck progrm (k ppliction) is set of clss files. By Jck convention, one clss must be clled Min, nd this clss must hve t lest one function, clled min. The contrct: when we tell the computer to execute Jck progrm, the function Min.min strts running Implementtion: After the progrm is compiled, ech clss file is trnslted into.vm file The operting system is lso implemented s set of.vm files (k librries ) tht co-exist longside the progrm s.vm files One of the OS librries, clled Sys.vm, includes method clled init. The Sys.init function strts with some OS initiliztion code (we ll del with this lter, when we discuss the OS), then it does cll Min.min Thus, to bootstrp, the VM implementtion hs to effect (e.g. in ssembly), the following opertions: SP SP 256 256 // // initilize the the stck pointer to to 0x0100 cll cll Sys.init // // cll cll the the function tht tht clls Min.min Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 21

VM implementtion over the Hck pltform Extends the VM implementtion described in the lst lecture (chpter 7) The result: single ssembly progrm file with lots of greed-upon symbols: Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 22

Proposed API Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 23

Perspective Benefits of the VM pproch Code trnsportbility: compiling for different pltforms requires replcing only the VM implementtion Lnguge inter-operbility: code of multiple lnguges cn be shred using the sme VM Common softwre librries Code mobility: Internet VM implementtion over CISC pltforms Some... lnguge Some compiler... VM lnguge VM imp. over RISC pltforms Some Other lnguge Some Other compiler CISC RISC mchine mchine... lnguge lnguge... VM emultor... Jck compiler Trnsltor written in high-level lnguge Hck Some virtues of the modulrity implied by the VM pproch to progrm trnsltion: Improvements in the VM implementtion re shred by ll compilers bove it Every new digitl device with VM implementtion gins immedite ccess to n existing softwre bse New progrmming lnguges cn be implemented esily using simple compilers Benefits of mnged code: Security Arry bounds, index checking, Add-on code Etc. VM Cons Performnce. Elements of Computing Systems, Nisn & Schocken, MIT Press, www.nnd2tetris.org, Chpter 8: Virtul Mchine, Prt II slide 24