lecture 09 high level language

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

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

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 9: High-Level Language

Physics

slide 2

A brief evolution of object-oriented languages

Fortran Basic Pascal C

Jack

! Visual Basic JavaScript
"

#


C++, Java C#

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 3

The Jack programming language
$

%#

$
$

%

&

%#


'
(

)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 4

Disclaimer
$
+

%

*
$

%


!

#

!
!
$

!

%

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 5

Hello world

/**

/** Hello
Hello World
World program.
program. */
*/
class
class Main
Main {{
function
function void
void main
main ()
() {{
//
// Prints
Prints some
some text
text using
using the
the standard

standard library
library
do
do Output.printString("Hello
Output.printString("Hello World");
World");
do
//
do Output.println();
Output.println();
// New
New line
line
return;
return;
}}
}}

#
$


%

&

)

!

!

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 6

Typical programming tasks in Jack
$

%


!

'

&

#

/

,-.#

-

%
#

2

#


!

#

% .01
%

3
4

&
*

!
5

)

&

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

$

%

slide 7

Procedural programming example
class
class Main
Main {{
/**
/** Sums
Sums up
up 11 ++ 22 ++ 33 ++ ...
... ++ nn */
*/
function
function int
int sum
sum (int
(int n)
n) {{
var
var int
int sum,
sum, i;
i;
let
sum
=
0;
let sum = 0;
let
let ii == 1;
1;
while
(~(i
while (~(i >> n))
n)) {{
let
let sum
sum == sum
sum ++ i;
i;
let
i
=
i
+
1;
let i = i + 1;
}}
return
return sum;
sum;
}}

}}

$

%

$

%

6
6

3&
$ %
$

!
!

#
&
Main.main()

%

#

method
constructor

function
function void
void main
main ()
() {{
var
var int
int n;
n;
let
n
=
Keyboard.readInt("Enter
let n = Keyboard.readInt("Enter n:
n: ");
");
do
Output.printString("The
result
is:
do Output.printString("The result is: ");
");
do
Output.printInt(sum(n));
do Output.printInt(sum(n));
return;
return;
}}

function
&

!
7

!

#

! OS
9

!

8

:

# Math String Array
Output Keyboard Screen Memory Sys

OS

'(

%

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 8

Object-oriented programming example
)

BankAccount

'(

/**
/** Represents
Represents aa bank
bank account.
account.
AA bank
account
has
bank account has an
an owner,
owner, an
an id,
id, and
and aa balance.
balance.
The
id
values
start
at
0
and
increment
by
1
The id values start at 0 and increment by 1 each
each
time
time aa new
new account
account is
is created.
created. */
*/
class
class BankAccount
BankAccount {{
/**
/** Constructs
Constructs aa new
new bank
bank account
account with
with aa 00 balance.
balance. */
*/
constructor
constructor BankAccount
BankAccount new(String
new(String owner)
owner)
/**
/** Deposits
Deposits the
the given
given amount
amount in
in this
this account.
account. */
*/
method
void
deposit(int
amount)
method void deposit(int amount)
/**
/** Withdraws
Withdraws the
the given
given amount
amount from
from this
this account.
account. */
*/
method
void
withdraw(int
amount)
method void withdraw(int amount)
/**
/** Prints
Prints the
the data
data of
of this
this account.
account. */
*/
method
void
printInfo()
method void printInfo()
/**
/** Disposes
Disposes this
this account.
account. */
*/
method
void
dispose()
method void dispose()
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 9

Object-oriented programming example (continues)
//
// Code
Code in
in any
any other
other class:
class:
var
int
x;
var int x;
1
var
var BankAccount
BankAccount b;
b;
3 let
let bb == BankAccount.new("joe");
BankAccount.new("joe");

/**
/** Represents
Represents aa bank
bank account.
account. */
*/
class
class BankAccount
BankAccount {{
//
// class-level
class-level variable
variable
static
int
newAcctId;
static int newAcctId;
//
// Private
Private variables
variables (aka
(aka fields
fields // properties)
properties)
field
int
id;
field int id;
field
field String
String owner;
owner;
field
int
balance;
field int balance;
/**
/** Constructs
Constructs aa new
new bank
bank account
account */
*/
constructor
BankAccount
new
(String
constructor BankAccount new (String owner)
owner) {{
let
let id
id == newAcctId;
newAcctId;
let
newAcctId
let newAcctId == newAcctId
newAcctId ++ 1;
1;
let
this.owner
=
owner;
let this.owner = owner;
let
let balance
balance == 0;
0;
return
this;
return this; 2
}}
//
// More
More BankAccount
BankAccount methods.
methods.

3&

# return this

)

!

RAM

%

!

BankAccount

3&

# b = BankAccount.new("joe")

;
BankAccount

*

b

<

!

#

// b = BankAccount.new("joe")
push "joe"
call BankAccount.new
pop b

3&

#

VM

}}

=
%

=

)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 10

Object-oriented programming example (continues)
...
...
var
var BankAccount
BankAccount b1,
b1, b2;
b2;
...
...
let
let b1
b1 == BankAccount.new("joe");
BankAccount.new("joe");
let
b2
=
BankAccount.new("jane");
let b2 = BankAccount.new("jane");
do
do b1.deposit(5000);
b1.deposit(5000);
do
b1.withdraw(1000);
do b1.withdraw(1000);
...
...

class
class BankAccount
BankAccount {{
static
static int
int nAccounts;
nAccounts;
field
field int
int id;
id;
field
String
field String owner;
owner;
field
int
balance;
field int balance;
//
// Constructor
Constructor ...
... (omitted)
(omitted)
/**
/** Handles
Handles deposits
deposits */
*/
method
void
deposit
(int
method void deposit (int amount)
amount) {{
let
let balance
balance == balance
balance ++ amount;
amount;
return;
return;
}}
/**
/** Handles
Handles withdrawls
withdrawls */
*/
method
void
withdraw
(int
method void withdraw (int amount){
amount){
if
(~(amount
>
balance))
if (~(amount > balance)) {{
let
let balance
balance == balance
balance -- amount;
amount;
}}
return;
return;
}}
//
// More
More BankAccount
BankAccount methods.
methods.
}}

3&

# do b1.deposit(5000)
( $

% void
%

%
do
!

)
b1.deposit(5000)

!

&
deposit(b1,5000)

<

!

#

// do b1.deposit(5000)
push b1
push 5000
call BanAccount.deposit

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 11

Object-oriented programming example (continues)
class
class BankAccount
BankAccount {{
static
static int
int nAccounts;
nAccounts;
field
field int
int id;
id;
field
String
field String owner;
owner;
field
int
balance;
field int balance;
//
// Constructor
Constructor ...
... (omitted)
(omitted)
/**
/** Prints
Prints information
information about
about this
this account.
account. */
*/
method
void
printInfo
()
{
method void printInfo () {
do
do Output.printInt(id);
Output.printInt(id);
do
Output.printString(owner);
do Output.printString(owner);
do
do Output.printInt(balance);
Output.printInt(balance);
return;
return;
}}
/**
/** Disposes
Disposes this
this account.
account. */
*/
method
void
dispose
()
{
method void dispose () {
do
do Memory.deAlloc(this);
Memory.deAlloc(this);
return;
return;
}}

//
// Code
Code in
in any
any other
other class:
class:
...
...
var
var int
int x;
x;
var
BankAccount
var BankAccount b;
b;
let
let bb == BankAccount.new("joe");
BankAccount.new("joe");
//
Manipulates
// Manipulates b...
b...
do
b.printInfo();
do b.printInfo();
do
do b.dispose();
b.dispose();
...
...

3&
$
)
!

do b.dispose()

%

=
&
!

//
// More
More BankAccount
BankAccount methods.
methods.
}}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 12

Abstract data type example
)

Fraction

'(

/**
/** Represents
Represents aa fraction
fraction data
data type.
type.
AA fraction
consists
of
a
numerator
fraction consists of a numerator and
and aa denominator,
denominator, both
both int
int values
values */
*/
class
class Fraction
Fraction {{
/**
/** Constructs
Constructs aa fraction
fraction from
from the
the given
given data
data */
*/
constructor
Fraction
new(int
numerator,
int
denominator)
constructor Fraction new(int numerator, int denominator)
/**
/** Reduces
Reduces this
this fraction,
fraction, e.g.
e.g. changes
changes 20/100
20/100 to
to 1/5.
1/5. */
*/
method
void
reduce()
method void reduce()
/**
/** Accessors
Accessors
method
method int
int getNumerator()
getNumerator()
method
int
getDenominator()
method int getDenominator()
/**
/** Returns
Returns the
the sum
sum of
of this
this fraction
fraction and
and the
the other
other one
one */
*/
method
Fraction
plus(Fraction
other)
method Fraction plus(Fraction other)
/**
/** Returns
Returns the
the product
product of
of this
this fraction
fraction and
and the
the other
other one
one */
*/
method
Fraction
product(Fraction
other)
method Fraction product(Fraction other)
/**
/** Prints
Prints this
this fraction
fraction */
*/
method
void
print()
method void print()

}}

/**
/** Disposes
Disposes this
this fraction
fraction */
*/
method
void
dispose()
method void dispose()

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 13

Abstract data type example (continues)
/**
/** Represents
Represents aa fraction
fraction data
data type.
type.
AA fraction
consists
of
a
numerator
fraction consists of a numerator and
and aa denominator,
denominator, both
both int
int values
values */
*/
class
Fraction
{
class Fraction {
field
field int
int numerator,
numerator, denominator;
denominator;
constructor
constructor Fraction
Fraction new
new (int
(int numerator,
numerator, int
int denominator)
denominator) {{
let
let this.numerator
this.numerator == numerator;
numerator;
let
this.denominator
=
let this.denominator = denominator;
denominator;
do
reduce()
//
Reduces
the
do reduce() // Reduces the new
new fraction
fraction
return
this
return this
}}
/**
/** Reduces
Reduces this
this fraction
fraction */
*/
method
void
reduce
()
{
method void reduce () {
//
// Code
Code omitted
omitted
}}
//
// AA static
static method
method that
that computes
computes the
the greatest
greatest common
common denominator
denominator of
of aa and
and b.
b.
function
int
gcd
(int
a,
int
b)
{
function int gcd (int a, int b) {
//
// Code
Code omitted
omitted
//
// Code
Code in
in any
any other
other class:
class:
}}
...
...
var
method
int
getNumerator
()
{
var Fraction
Fraction a,
a, b;
b;
method int getNumerator () {
return
numerator;
let
a
=
Fraction.new(2,5);
return numerator;
let a = Fraction.new(2,5);
}}
let
let bb == Fraction.new(70,210);
Fraction.new(70,210);
do
b.print()
method
do b.print() //
// prints
prints "1/3"
"1/3"
method int
int getDenominator
getDenominator ()
() {{
...
return
...
return denominator;
denominator;
}}
//
// (print
(print method
method in
in next
next slide)
slide)
//
// More
More Fraction
Fraction methods
methods follow.
follow.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 14

Abstract data type example (continues)
/**
/** Represents
Represents aa fraction
fraction data
data type.
type.
AA fraction
consists
of
a
numerator
fraction consists of a numerator and
and aa denominator,
denominator, both
both int
int values
values */
*/
class
Fraction
{
class Fraction {
field
field int
int numerator,
numerator, denominator;
denominator;
//
// Constructor
Constructor and
and previously
previously defined
defined methods
methods omitted
omitted
/**
/** Returns
Returns the
the sum
sum of
of this
this fraction
fraction the
the other
other one
one */
*/
method
Fraction
plus
(Fraction
other)
{
method Fraction plus (Fraction other) {
var
var int
int sum;
sum;
let
sum
=
let sum = (numerator
(numerator ** other.getDenominator())
other.getDenominator()) ++
(other.getNumerator()
(other.getNumerator() ** denominator());
denominator());
return
Fraction.new(sum
,
denominator
return Fraction.new(sum , denominator ** other.getDenominator());
other.getDenominator());
}}
//
// Similar
Similar fraction
fraction arithmetic
arithmetic methods
methods follow,
follow, code
code omitted.
omitted.
/**
/** Prints
Prints this
this fraction
fraction */
*/
method
void
print
()
{
method void print () {
do
do Output.printInt(numerator);
Output.printInt(numerator);
do
Output.printString("/");
do Output.printString("/");
do
do Output.printInt(denominator);
Output.printInt(denominator);
return
return
}}
}}

//
// Code
Code in
in any
any other
other class:
class:
var
var Fraction
Fraction a,
a, b,
b, c;
c;
let
a
=
Fraction.new(2,3);
let a = Fraction.new(2,3);
let
let bb == Fraction.new(1,5);
Fraction.new(1,5);
//
// computes
computes cc == aa ++ bb
let
let cc == a.plus(b);
a.plus(b);
do
do c.print();
c.print(); //
// prints
prints "13/15"
"13/15"

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 15

Data structure example
/**
/** Represents
Represents aa sequence
sequence of
of int
int values,
values, implemented
implemented as
as aa linked
linked list.
list.
The
list
consists
of
an
atom,
which
is
an
int
value,
The list consists of an atom, which is an int value,
and
and aa tail,
tail, which
which is
is either
either aa list
list or
or aa null
null value.
value. */
*/
class
List
{
class List {
field
field int
int data;
data;
field
List
field List next;
next;
/*
/* Creates
Creates aa new
new list
list */
*/
constructor
List
new
(int
constructor List new (int car,
car, List
List cdr)
cdr) {{
let
let data
data == car;
car;
let
next
=
cdr;
let next = cdr;
return
v
return this;
this;
}}

v

2

3

5

5

/*
/* Disposes
Disposes this
this list
list by
by recursively
recursively disposing
disposing its
its tail.
tail. */
*/
method
void
dispose()
{
method void dispose() {
if
if (~(next
(~(next == null))
null)) {{
//
// Code
Code in
in any
any other
other class:
class:
do
next.dispose();
do next.dispose();
...
...
}}
//
do
// Creates
Creates aa list
list holding
holding the
the numbers
numbers 2,3,
2,3, and
and 5:
5:
do Memory.deAlloc(this);
Memory.deAlloc(this);
return;
var
List
v;
return;
var List v;
}}
let
let vv == List.new(5
List.new(5 ,, null);
null);
...
let
...
let vv == List.new(2
List.new(2 ,, List.new(3,v));
List.new(3,v));
...
}} //
...
// class
class List.
List.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 16

Jack language specification
&
2
5

%

3&

'

!

!

%

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 17

Jack syntax

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 18

Jack syntax (continues)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 19

Jack data types
'

'

int

,>

boolean

0

!

= +

9

.*

#

!

–1

!

char

-32768

true

32767

false

(‘a’, ‘x’, ‘+’, ‘%’, ...)

&

= +

9

/ 0

#

String
Array
...

&

!

?

!

= +

9

#

BankAccount
Fraction
List
Bat / Ball

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 20

Jack variable kinds and scope

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 21

Jack expressions
$

% expression

!

!

#

!

)

%

this
& arrayName[expression]
!
Array

arrayNname

expression

!&

!

@

A#

-expression
~expression
&

!

!

expression op expression

op

!

!

#

+-*0
&|

and

or

=
( expression )

&

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 22

Jack Statements
let
let varName
varName == expression
expression;;
or
or
let
let varName
varName[[expression
expression]] == expression
expression;;
if
if ((expression
expression)) {{

statements
statements

}}
else
else {{
}}

statements
statements

while
while ((expression
expression)) {{
}}

statements
statements

do
do function-or-method-call
function-or-method-call;;

return
return expression
expression;;
or
or
return;
return;
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 23

Jack subroutine calls
B

&#

subroutineName(arg0, arg1, …)
$

!

'
3&

,#

;
)

% &

!

#

!

%

!

function int sqrt(int n)

#

sqrt(17)
sqrt(x)
sqrt((b * b) – (4 * a * c))
sqrt(a * sqrt(c - 17) + 3)

3

(

3&

&
.#

;

#

(! u
)

v
v

method Matrix plus (Matrix other);

!

%

Matrix

!

# u.plus(v)

!

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 24

Noteworthy features of the Jack language
)

let %

let x = 0;

)

d %

do reduce();

C

#

1 + 2 * 3

)

&

9

!!

!

&

/
I !

1 + (2 * 3)

# int boolean char;
,>

!

C

=

=

!
#

!
!

Array x;

x = Array.new();

function

;
(

constructor=

%

& ClassName.new(argsList)

D# 4
#)

!
%
!
%

!

$

$
%

E

F

!
!

%

!
$

!
%

%

&

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 25

Jack program structure
class
class ClassName
ClassName {{
field
variabledeclarations;
declarations;
fieldvariable
static
staticvariable
variabledeclarations;
declarations;
constructor
constructor type
type {{ parameterList
parameterList)) {{

local
localvariable
variabledeclarations;
declarations;
statements
statements

}}
method
type {{ parameterList
parameterList)) {{
method type

local
localvariable
variabledeclarations;
declarations;
statements
statements

}}
function
function type
type {{ parameterList
parameterList)) {{

}}

}}

local
localvariable
variabledeclarations;
declarations;
statements
statements

#
3

G

)

!

)

!

3

$

field 0 static

int boolean char

type

%

#

3
!
$

%

6

!
!
Main

)

Main
main()

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 26

Jack standard library aka language extensions aka Jack OS
class
class Math
Math {{
function
function void
void init()
init()
Class
String
{
Class String
{
function
int
x)
function
int abs(int
abs(int
x)
constructor
String
maxLength)
constructor
String new(int
new(int
maxLength)
function
int
multiply(int
x,
y)
function
int
multiply(int
x, int
int
y)
Class
Array
{
Class
Array
{
method
void
dispose()
method
void
dispose()
function
int
divide(int
x,
function
int
divide(int
x, int
int y)
y)
method
int
length()
function
Array
new(int
size)
method
int
length()
function
int
x, int
function
Array
function
int min(int
min(int
int y)
y) size)
class
Output
{{x,new(int
class
Output
method
char
charAt(int
j)
method
char
charAt(int
j)
function
int
max(int
x,
y)
function
int
max(int
x, int
int
y)
function
void
moveCursor(int
i,
method
void
dispose()
function
void
moveCursor(int
i, int
int j)
j)
method
void
dispose()
method
void
setCharAt(int
j,
char
c)
method
void
setCharAt(int
j,
char
c)
function
int
sqrt(int
x)
Class
Screen
{
function int
sqrt(int
x)
ClassappendChar(char
Screen
{
function
void
c)
function
void printChar(char
printChar(char
c)
method
String
c)
method
String
appendChar(char
c)
}
}}
function
void
clearScreen()
}
function
void
clearScreen()
function
void
printString(String
s)
function
void
printString(String
s)
method
eraseLastChar()
class
Memory
{{
method void
void
eraseLastChar()
class
Memory
function
void
setColor(boolean
b)
function
void
setColor(boolean
b)
function
void
printInt(int
i)
function
void
printInt(int
i)
method
int
intValue()
method int function
intValue()
void
drawPixel(int
x,
peek(int
address)
function
int
function
void
drawPixel(int
x, int
int y)
y)
function
void
println()
function
int
peek(int
address)
function
void
println()
method
void
setInt(int
j)
Class
Keyboard
{
method voidfunction
setInt(int
j)
Class
Keyboard
{
void
drawLine(int
x1,
function
void
drawLine(int
x1, int
int y1,
y1,
function
void
backSpace()
function
void
backSpace()
function
char
backSpace()
int
x2,
int
y2)
function
void
poke(int
address,
int
function char backSpace()
int x2,
int y2)
function
void
poke(int
address,
int value)
value)
keyPressed()
function
char
}
function
char
keyPressed()
}
function
char
doubleQuote()
Class
Sys
drawRectangle(int
x1,
function
void
function char
doubleQuote()
Class
Sys {
{
function
void
drawRectangle(int
x1, int
int y1,
y1,
function
Array
alloc(int
size)
int
x2,
int
y2)
function
Array
alloc(int
size)
function
char
newLine()
int x2, int y2)
function
char
readChar()
function char newLine()
function
charvoid
readChar()
halt():
function
function
void
drawCircle(int
x,
void halt():
}}
function
voidfunction
drawCircle(int
x, int
int y,
y, int
int r)
r)
function
void
deAlloc(Array
o)
function
void
deAlloc(Array
o)
function
String
readLine(String
message)
}}
function
String
readLine(String
message)
function
void
error(int
errorCode)
function
void
error(int
errorCode)
}}
function
int
message)
function
int readInt(String
readInt(String
message)
wait(int
duration)
function
void
function void wait(int duration)
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 27

Perspective
$

%

#

'

/

#
;
/

,G

,,

,.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 9: High-Level Language

slide 28