lecture 08 virtual machine II

Building a Modern Computer From First Principles
www.nand2tetris.org

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 1

Where we are at:
Human
Thought

Abstract design

Software
hierarchy

abstract interface
Chapters 9, 12

H.L. Language
&

Operating Sys.

Compiler
abstract interface
Chapters 10 - 11

Virtual
Machine

VM Translator
abstract interface
Chapters 7 - 8

Assembly
Language
Assembler
Chapter 6

abstract interface


Machine
Language

Computer
Architecture
abstract interface
Chapters 4 - 5

Hardware
Platform

Hardware
hierarchy

Gate Logic
abstract interface
Chapters 1 - 3

Chips &
Logic Gates


Electrical
Engineering

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

Physics

slide 2

The big picture
Some
language

...

Some
compiler

...


Some Other
language

Jack
language

Implemented in
Projects 7-8

VM language

VM
implementation
over CISC
platforms

CISC
machine
language


RISC
machine
language

VM imp.
over RISC
platforms

CISC
machine

RISC
machine

VM imp.
over the Hack
platform

VM

emulator

A Java-based emulator
is included in the course
software suite

written in
a high-level
language

...

...

Chapters
9-13

Jack
compiler


Some Other
compiler

Chapters
7-8

Hack
machine
language

Chapters
1-6

...

other digital platforms, each equipped
with its VM implementation

Any
computer


Hack
computer

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 3

The VM langauge

$$

%%&&
add
add
sub
sub

label
label

goto
goto

neg
neg
eq
eq
gt
gt
lt
lt

if-goto
if-goto
this
lecture

previous
lecture


and
and
or
or
not
not
pop
pop xx (pop
(popinto
intox,x,which
whichisisaavariable)
variable)
push
push yy (y(ybeing
beingaavariable
variableororaaconstant)
constant)

''
function

function
call
call
return
return

!

"

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

#
slide 4

The compilation challenge
(

+

)

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
int
a,
b,
c;
var int a, b, c;
let
let aa == Keyboard.readInt(“Enter
Keyboard.readInt(“Enter aa number”);
number”);
let
b
=
Keyboard.readInt(“Enter
a
number”);
let b = Keyboard.readInt(“Enter a number”);
let
let cc == Keyboard.readInt(“Enter
Keyboard.readInt(“Enter aa number”);
number”);
let
x
=
solve(a,b,c);
let x = solve(a,b,c);
return;
return;
}}

*
+

)

,

#

Compiler

}}
//
// Solves
Solves aa quadearic
quadearic equation
equation (sort
(sort of)
of)
function
int
solve(int
a,
int
b,
int
c)
function int solve(int a, int b, int c) {{
var
var int
int x;
x;
if
(~(a
=
if (~(a = 0))
0))
x=(-b+sqrt(b*b–4*a*c))/(2*a);
x=(-b+sqrt(b*b–4*a*c))/(2*a);
else
else
x=-c/b;
x=-c/b;
return
return x;
x;
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

0000000000010000
0000000000010000
1110111111001000
1110111111001000
0000000000010001
0000000000010001
1110101010001000
1110101010001000
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000000000
0000000000000000
1111010011010000
1111010011010000
0000000000010010
0000000000010010
1110001100000001
1110001100000001
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000010001
0000000000010001
0000000000010000
0000000000010000
1110111111001000
1110111111001000
0000000000010001
0000000000010001
1110101010001000
1110101010001000
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000000000
0000000000000000
1111010011010000
1111010011010000
0000000000010010
0000000000010010
1110001100000001
1110001100000001
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000010001
0000000000010001
...
...

slide 5

The compilation challenge / two-tier setting
-

"
if
if (~(a
(~(a == 0))
0))
xx == (-b+sqrt(b*b–4*a*c))/(2*a)
(-b+sqrt(b*b–4*a*c))/(2*a)
else
else
xx == -c/b
-c/b
Compiler

.
.

+

))

#

push
push aa
push
push 00
eq
eq
if-goto
if-goto elseLabel
elseLabel
push
b
push b
neg
neg
push
push bb
push
push bb
call
call mult
mult
push
4
push 4
VM translator
push
push aa
call
call mult
mult
push
push cc
call
call mult
mult
call
sqrt
call sqrt
add
add
push
push 22
push
push aa
call
call mult
mult
div
div
pop
pop xx
goto
goto contLable
contLable
elseLabel:
elseLabel:
push
push cc
neg
neg
push
push bb
call
call div
div
pop
pop xx
contLable:
contLable:

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

0000000000010000
0000000000010000
1110111111001000
1110111111001000
0000000000010001
0000000000010001
1110101010001000
1110101010001000
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000000000
0000000000000000
1111010011010000
1111010011010000
0000000000010010
0000000000010010
1110001100000001
1110001100000001
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000010001
0000000000010001
0000000000010000
0000000000010000
1110111111001000
1110111111001000
0000000000010001
0000000000010001
1110101010001000
1110101010001000
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000000000
0000000000000000
1111010011010000
1111010011010000
0000000000010010
0000000000010010
1110001100000001
1110001100000001
0000000000010000
0000000000010000
1111110000010000
1111110000010000
0000000000010001
0000000000010001
0000000000010010
0000000000010010
1110001100000001
1110001100000001
...
...
slide 6

The compilation challenge
+
//
// Computes
Computes xx == (-b
(-b ++ sqrt(b^2
sqrt(b^2 -4*a*c))
-4*a*c)) // 2*a
2*a
if
if (~(a
(~(a == 0))
0))
xx == (-b
(-b ++ sqrt(b
sqrt(b ** bb –– 44 ** aa ** c))
c)) // (2
(2 ** a)
a)
else
else
xx == -- cc // bb

,

!

,

)

/

)

0

)

"

")

1
!

")-

"

0

,

)

"

%

%

")

0 # #

#
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 7

Lecture plan

$$

%%&&
add
add
sub
sub

label
label
goto
goto

neg
neg
eq
eq
gt
gt
lt
lt

if-goto
if-goto
previous
lecture

and
and
or
or
not
not
pop
pop xx (pop
(popinto
intox,x,which
whichisisaavariable)
variable)
push
push yy (y(ybeing
beingaavariable
variableororaaconstant)
constant)

''
function
function
call
call
return
return

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 8

Program flow commands in the VM language
0

,
function
function mult
mult 11
push
push constant
constant 00
pop
pop local
local 00
label
label loop
loop
push
push argument
argument 00
push
push constant
constant 00
eq
eq
if-goto
if-goto end
end
push
push argument
argument 00
push
push 11
sub
sub
pop
pop argument
argument 00
push
push argument
argument 11
push
push local
local 00
add
add
pop
pop local
local 00
goto
goto loop
loop
label
label end
end
push
push local
local 00
return
return

label
label cc

%%
%%

goto
goto cc

%%
%%
%%
%%

22

if-goto
if-goto cc %%
%%
%%
%%
%%
%%

55

0 022

""

!

44

/
(

0
!
!

+
"
/

2

3#

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 9

Lecture plan

$$

%%&&
add
add
sub
sub

label
label
goto
goto

neg
neg
eq
eq
gt
gt
lt
lt

if-goto
if-goto
previous
lecture

and
and
or
or
not
not
pop
pop xx (pop
(popinto
intox,x,which
whichisisaavariable)
variable)
push
push yy (y(ybeing
beingaavariable
variableororaaconstant)
constant)

''
function
function
call
call
return
return

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 10

Subroutines
//
// Compute
Compute xx == (-b
(-b ++ sqrt(b^2
sqrt(b^2 -4*a*c))
-4*a*c)) // 2*a
2*a
if
if (~(a
(~(a == 0))
0))
xx == (-b
(-b ++ sqrt(b
sqrt(b ** bb –– 44 ** aa ** c))
c)) // (2
(2 ** a)
a)
else
else
xx == -- cc // bb

(

6

2

&

,
"

%

)
%

###
)

")
+

)

,
)

+
7$

0 # #
)

"
,

(

"
"

0!

+

,

0

8

. "

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 11

Subroutines in the VM language
,

0 " 7

...
...
//
// computes
computes (7
(7 ++ 2)
2) ** 33 -- 55
push
push constant
constant 77
push
push constant
constant 22
add
add
push
push constant
constant 33
call
call mult
mult
)
push
constant
5
push constant 5
sub
sub
...
...

)

+
,
+
%
+

%

8

,

function
function mult
mult 11
push
push constant
constant 00
pop
pop local
local 00 //
// result
result (local
(local 0)
0) == 00
label
label loop
loop
push
push argument
argument 00
push
push constant
constant 00
eq
eq
if-goto
if-goto end
end //
// if
if arg0
arg0 ==
== 0,
0, jump
jump to
to end
end
push
argument
0
push argument 0
push
push 11
sub
sub
pop
pop argument
argument 00 //
// arg0-arg0-push
argument
1
push argument 1
push
push local
local 00
add
add
pop
pop local
local 00 //
// result
result +=
+= arg1
arg1
goto
goto loop
loop
label
end
label end
push
push local
local 00 //
// push
push result
result
return
return

"0
"

,

#

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 12

Function commands in the VM language
function
function gg nVars
nVars %%
%%
%%
%%

nVars
nVars

call
call gg nArgs
nArgs

%%
""
%%
%%
%%nArgs
nArgs

return
return

%%
%%

9 .
$ &

g0
g0
44

gg

""

,,

,/
#

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

slide 13

Function call-and-return conventions
" 7
function
function demo
demo 33
...
...
push
push constant
constant 77
push
push constant
constant 22
add
add
push
push constant
constant 33
call
call mult
mult
...
...

)

8

,
$
,

function
function mult
mult 11
push
push constant
constant 00
pop
pop local
local 00 //
// result
result (local
(local 0)
0) == 00
label
label loop
loop
...
//
...
// rest
rest of
of code
code ommitted
ommitted
label
end
label end
push
push local
local 00 //
// push
push result
result
return
return

0

;
local, argument,
this, that, pointer)
+

,
#

)

+

0

&

0

0

$
,

0

0
call

2
2

)

0

"

&
:

)

2

(

7 "
0

%

")

0

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 8: Virtual Machine, Part II

(

8
#
slide 14

function
function gg nVars
nVars
call
call gg nArgs
nArgs

The function-call-and-return protocol
+

return
return
&&

""

g0g0
gg