www.nand2tetris.org
Where we are at:
Assembler
Chapter 6 H.L. Language
&
Operating Sys.
abstract interface
Compiler
Chapters 10 - 11
VM Translator
Chapters 7 - 8
Computer Architecture
Chapters 4 - 5
Gate Logic
Chapters 1 - 3 Electrical
Engineering
Physics
Virtual Machine
abstract interface
Software hierarchy
Assembly Language
abstract interface
Hardware hierarchy Machine
Language
abstract interface
Hardware Platform
abstract interface
Chips & Logic Gates
abstract interface Human
Thought
Abstract design
The big picture
. . .
RISC machine
VM language
other digital platforms, each equipped with its VM implementation RISC
over CISC platforms
VM imp. over RISC
platforms
VM imp. over the Hack
platform VM
emulator Some Other
language
Jack language
Some
compiler Some Othercompiler
Jack compiler
. . .
Some
language
. . .
Chapters 1-6
Chapters 7-8
Chapters 9-13
A Java-based emulator is included in the course software suite
The VM langauge
! "
#
$
% &
add
sub
neg
eq
gt
lt
and
or
not
pop x (pop into x, which is a variable)
push y (y being a variable or a constant)
$
% &
add
sub
neg
eq
gt
lt
and
or
not
pop x (pop into x, which is a variable)
push y (y being a variable or a constant)
label
goto
if-goto
'
function
call
return
label
goto
if-goto
'
function
call
return
previous lecture
The compilation challenge
class Main { static int x;
function void main() {
// Inputs and multiplies two numbers var int a, b, c;
let a = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); let c = Keyboard.readInt(“Enter a number”); let x = solve(a,b,c);
return; }
}
// Solves a quadearic equation (sort of) function int solve(int a, int b, int c) {
var int x;
class Main { static int x;
function void main() {
// Inputs and multiplies two numbers var int a, b, c;
let a = Keyboard.readInt(“Enter a number”); let b = Keyboard.readInt(“Enter a number”); let c = Keyboard.readInt(“Enter a number”); let x = solve(a,b,c);
return; }
}
// Solves a quadearic equation (sort of) function int solve(int a, int b, int c) {
The compilation challenge / two-tier setting
if (~(a = 0))
x = (-b+sqrt(b*b–4*a*c))/(2*a) else
x = -c/b if (~(a = 0))
x = (-b+sqrt(b*b–4*a*c))/(2*a) else
if-goto elseLabel push b
neg push b push b call mult push 4 push a call mult push c call mult call sqrt add
push 2 push a call mult div
pop x
goto contLable elseLabel:
push c neg push b call div pop x contLable:
push a push 0 eq
if-goto elseLabel push b
neg push b push b call mult push 4 push a call mult push c call mult call sqrt add
push 2 push a call mult div
pop x
goto contLable elseLabel:
push c neg push b call div pop x
VM translator
.
.
+
))
// Computes x = (-b + sqrt(b^2 -4*a*c)) / 2*a
if (~(a = 0))
x = (-b + sqrt(b * b – 4 * a * c)) / (2 * a) else
x = - c / b
// Computes x = (-b + sqrt(b^2 -4*a*c)) / 2*a
if (~(a = 0))
x = (-b + sqrt(b * b – 4 * a * c)) / (2 * a) else
x = - c / b +
The compilation challenge
, ,
!
)
/
)
0
"
)
1
")
! ")- "
0
)
"
%, %
")
0 # #
Lecture plan
$
% &
add
sub
neg
eq
gt
lt
and
or
not
pop x (pop into x, which is a variable)
push y (y being a variable or a constant)
$
% &
add
sub
neg
eq
gt
lt
and
or
not
pop x (pop into x, which is a variable)
push y (y being a variable or a constant)
label
goto
if-goto
'
function
call
return
label
goto
if-goto
'
function
call
return
Program flow commands in the
VM
language
function mult 1 push constant 0 pop local 0 label loop
push argument 0 push constant 0 eq
if-goto end push argument 0 push 1
sub
pop argument 0 push argument 1 push local 0 add
pop local 0 goto loop label end
push local 0 return
function mult 1 push constant 0 pop local 0 label loop
push argument 0 push constant 0 eq
if-goto end push argument 0 push 1
sub
pop argument 0 push argument 1 push local 0 add
pop local 0 goto loop label end
Lecture plan
$
% &
add
sub
neg
eq
gt
lt
and
or
not
pop x (pop into x, which is a variable)
push y (y being a variable or a constant)
$
% &
add
sub
neg
eq
gt
lt
and
or
not
pop x (pop into x, which is a variable)
push y (y being a variable or a constant)
label
goto
if-goto
'
function
call
return
label
goto
if-goto
'
function
call
return
Subroutines
(
6
2
&
,
)
"
%
%
###
)
")
)
+
,
)
+
0 # #
7$
)
"
,
0
,
"
8
(
" 0
!
+
. "
// Compute x = (-b + sqrt(b^2 -4*a*c)) / 2*a if (~(a = 0))
x = (-b + sqrt(b * b – 4 * a * c)) / (2 * a) else
x = - c / b
// Compute x = (-b + sqrt(b^2 -4*a*c)) / 2*a if (~(a = 0))
x = (-b + sqrt(b * b – 4 * a * c)) / (2 * a) else
Subroutines in the
VM
language
function mult 1
push constant 0
pop local 0 // result (local 0) = 0 label loop
push argument 0 push constant 0 eq
if-goto end // if arg0 == 0, jump to end push argument 0
push 1 sub
pop argument 0 // arg0--push argument 1
push local 0 add
pop local 0 // result += arg1 goto loop
label end
push local 0 // push result
return
function mult 1
push constant 0
pop local 0 // result (local 0) = 0 label loop
push argument 0 push constant 0 eq
if-goto end // if arg0 == 0, jump to end push argument 0
push 1 sub
pop argument 0 // arg0--push argument 1
push local 0 add
pop local 0 // result += arg1 goto loop
label end
push local 0 // push result return
0 " 7 8 ,
...
// computes (7 + 2) * 3 - 5 push constant 7
push constant 2 add
push constant 3
call mult
push constant 5 sub
...
...
// computes (7 + 2) * 3 - 5 push constant 7
push constant 2 add
push constant 3 call mult
push constant 5 sub
...
,
Function commands in the VM language
9 .
,/
$ &
#
function
g nVars %%
g0
%%
nVars
call
g nArgs
%%
"
g
4
%%
nArgs
"
return
%%
,
function
g nVars %%
g0
%%
nVars
call
g nArgs %%
"
g
4
%%
nArgs
"
Function call-and-return conventions
function mult 1
push constant 0
pop local 0 // result (local 0) = 0 label loop
... // rest of code ommitted label end
push local 0 // push result
return
function mult 1
push constant 0
pop local 0 // result (local 0) = 0 label loop
... // rest of code ommitted label end
push local 0 // push result
return
" 7 8 ,
function demo 3 ...
push constant 7 push constant 2 add
push constant 3
call mult
...
function demo 3 ...
push constant 7 push constant 2 add
push constant 3
call mult
...
local, argument, this, that, pointer)
+ ,
The function-call-and-return protocol
+
. , 0 argument 5
local 5 5
+ static static
0 " "
& , 0 " return#
. , 0 argument 5
local 5 5
+ static static
0 " "
& , 0 " return#
& g0 "
g
< , 0 " call g nArgs
$ g
+
"0 ,
"
$ local, argument, this, that,
pointer) #
& g0 "
g
< , 0 " call g nArgs
$ g
+
"0 ,
"
$ local, argument, this, that,
pointer) #
+
g=
& 6
& " 6 " , 0
+ 0
7 " 8 #
function g nVars call g nArgs return
function g nVars
call g nArgs
.
f
g
0
(
f
=
2
call
(
f
$
0
5
>0
g
(
local argumentg
+
g
#
.
g
f0
g
2 "
"
:
f
+
"
f
2
#
9
!
"
" 7 "
8/
$
.
"
#
The function-call-and-return protocol: the VM implementation view
function g nVars call g nArgs return
function g nVars
call g nArgs
The implementation of the VM’s stack on the host Hack RAM
"
RAM
"
. "
"
SP "
$
0
,
4
(
+
"
"0
+
"
5
Implementing the
call
g nArgs
command
0
#
// In the course of implementing the code of f
// (the caller), we arrive to the command call g nArgs. // we assume that nArgs arguments have been pushed
// onto the stack. What do we do next?
// We generate a symbol, let’s call it returnAddress; // Next, we effect the following logic:
push returnAddress // saves the return address push LCL // saves the LCL of f
push ARG // saves the ARG of f push THIS // saves the THIS of f push THAT // saves the THAT of f ARG = SP-nArgs-5 // repositions SP for g LCL = SP // repositions LCL for g goto g // transfers control to g returnAddress: // the generated symbol
// In the course of implementing the code of f
// (the caller), we arrive to the command call g nArgs. // we assume that nArgs arguments have been pushed
// onto the stack. What do we do next?
// We generate a symbol, let’s call it returnAddress; // Next, we effect the following logic:
push returnAddress // saves the return address push LCL // saves the LCL of f
push ARG // saves the ARG of f push THIS // saves the THIS of f push THAT // saves the THAT of f ARG = SP-nArgs-5 // repositions SP for g LCL = SP // repositions LCL for g goto g // transfers control to g returnAddress: // the generated symbol
call g nArgs
call g nArgs
< , ###
Implementing the
function
g nVars
command
0
#
function g nVars
function g nVars
// to implement the command function g nVars,
// we effect the following logic:
g:
repeat nVars times: push 0
// to implement the command function g nVars, // we effect the following logic:
g:
Implementing the
return
command
0
#
// In the course of implementing the code of g, // we arrive to the command return.
// We assume that a return value has been pushed // onto the stack.
// We effect the following logic:
frame = LCL // frame is a temp. variable retAddr = *(frame-5) // retAddr is a temp. variable *ARG = pop // repositions the return value
// for the caller
SP=ARG+1 // restores the caller’s SP THAT = *(frame-1) // restores the caller’s THAT THIS = *(frame-2) // restores the caller’s THIS ARG = *(frame-3) // restores the caller’s ARG LCL = *(frame-4) // restores the caller’s LCL goto retAddr // goto returnAddress
// In the course of implementing the code of g, // we arrive to the command return.
// We assume that a return value has been pushed // onto the stack.
// We effect the following logic:
frame = LCL // frame is a temp. variable retAddr = *(frame-5) // retAddr is a temp. variable *ARG = pop // repositions the return value
// for the caller
SP=ARG+1 // restores the caller’s SP THAT = *(frame-1) // restores the caller’s THAT THIS = *(frame-2) // restores the caller’s THIS ARG = *(frame-3) // restores the caller’s ARG LCL = *(frame-4) // restores the caller’s LCL goto retAddr // goto returnAddress
return
Bootstrapping
SP = 256 // initialize the stack pointer to 0x0100
call Sys.init // call the function that calls Main.main
SP = 256 // initialize the stack pointer to 0x0100
call Sys.init // call the function that calls Main.main
$
)
2 "
"
#
&
- "
0
Main
0
0
main
#
+
,
- "
0
Main.main
$
0
.vm
+
.vm
" 7
8
) ,
.vm
*
*(
0
Sys.vm
0
init
.
+
Sys.init
*(
5
0
*( 0
call Main.main
?, @
+ )
Perspective
&
1
A
)
(
?
,
<
. . .
VM language
RISC machine
language Hack
CISC machine
language
. . .
written in a high-level
language
. . .
VM implementation
over CISC platforms
VM imp. over RISC
platforms Translator
VM emulator Some Other
language Jack
Some
compiler Some Othercompiler compiler
. . .
Some
language