The Elements of Computing Systems Nisan & Schocken

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 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


Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

Electrical
Engineering
Physics

slide 2

Why care about assemblers?

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 3

Assembly example
!

For now,
ignore all

details!

"

//
// Computes
Computes 1+...+RAM[0]
1+...+RAM[0]
//
// And
And stored
stored the
the sum
sum in
in RAM[1]
RAM[1]
@i
@i
M=1
//

M=1
// ii == 11
@sum
@sum
M=0
//
M=0
// sum
sum == 00
(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M

D=M
@R0
@R0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
...
//
...
// Etc.
Etc.

#!

assemble

$


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
...
...

execute

%

!
&

!

$


%

!

'

i% sum% LOOP%

"

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 4

Revisiting Hack low-level programming: an example
sum.asm"
//
// Computes
Computes 1+...+RAM[0]

1+...+RAM[0]
//
// And
And stores
stores the
the sum
sum in
in RAM[1].
RAM[1].
@i
@i
M=1
M=1 //
// ii == 11
@sum
@sum
M=0
M=0 //
// sum
sum == 00

(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M
D=M
@0
@0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
@i
//
@i
// sum
sum +=
+= ii
D=M
D=M
@sum
@sum
M=D+M
M=D+M
@i
//
@i
// i++
i++
M=M+1
M=M+1
@LOOP
@LOOP //
// goto
goto LOOP
LOOP
0;JMP
0;JMP
(WRITE)
(WRITE)
@sum
@sum
D=M
D=M
@1
@1
M=D
M=D //
// RAM[1]
RAM[1] == the
the sum
sum
(END)
(END)
@END
@END
0;JMP
0;JMP

()*

()*

!
'

+

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

,
%

!
slide 5

The assembler’s view of an assembly program
//
// Computes
Computes 1+...+RAM[0]
1+...+RAM[0]
//
// And
And stores
stores the
the sum
sum in
in RAM[1].
RAM[1].
@i
@i
M=1
M=1 //
// ii == 11
@sum
@sum
M=0
M=0 //
// sum
sum == 00
(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M
D=M
@0
@0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
@i
//
@i
// sum
sum +=
+= ii
D=M
D=M
@sum
@sum
M=D+M
M=D+M
@i
//
@i
// i++
i++
M=M+1
M=M+1
@LOOP
@LOOP //
// goto
goto LOOP
LOOP
0;JMP
0;JMP
(WRITE)
(WRITE)
@sum
@sum
D=M
D=M
@1
@1
M=D
M=D //
// RAM[1]
RAM[1] == the
the sum
sum
(END)
(END)
@END
@END
0;JMP
0;JMP

!

%
-

AC- (SYMBOL)
(

// comment
.

/0
!

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 6

Translating / assembling A-instructions
Symbolic:

// Where value is either a non-negative decimal number
// or a symbol referring to such number.

@value

value (v = 0 or 1)
Binary:

0

v

v

v

v

v

v

v

v

v

v

v

v

v

v

v

+ ,
+ ,

,

%

%

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 7

Translating / assembling C-instructions
Symbolic:

dest=comp;jump

// Either the dest or jump fields may be empty.
// If dest is empty, the "=" is ommitted;
// If jump is empty, the ";" is omitted.
comp

Binary:

1

1

1

a

c1 c2 c3 c4

-

dest
c5 c6

d1 d2

jump
d3 j1 j2 j3

1

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 8

The overall assembly logic
//
// Computes
Computes 1+...+RAM[0]
1+...+RAM[0]
//
// And
And stores
stores the
the sum
sum in
in RAM[1].
RAM[1].
@i
@i
M=1
M=1 //
// ii == 11
@sum
@sum
M=0
M=0 //
// sum
sum == 00
(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M
D=M
@0
@0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
@i
//
@i
// sum
sum +=
+= ii
D=M
D=M
@sum
@sum
M=D+M
M=D+M
@i
//
@i
// i++
i++
M=M+1
M=M+1
@LOOP
@LOOP //
// goto
goto LOOP
LOOP
0;JMP
0;JMP
(WRITE)
(WRITE)
@sum
@sum
D=M
D=M
@1
@1
M=D
M=D //
// RAM[1]
RAM[1] == the
the sum
sum
(END)
(END)
@END
@END
0;JMP
0;JMP

2

"
)

%
-

A

"
%
%

"

-

C
%

/0
/0

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 9

Handling symbols (aka symbol resolution)
,

-

3
3
4
*
)

5

"
'

"

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

'
@R0
@R0
D=M
D=M
@END
@END
D;JLE
D;JLE
@counter
@counter
M=D
M=D
@SCREEN
@SCREEN
D=A
D=A
@x
@x
M=D
M=D
(LOOP)
(LOOP)
@x
@x
A=M
A=M
M=-1
M=-1
@x
@x
D=M
D=M
@32
@32
D=D+A
D=D+A
@x
@x
M=D
M=D
@counter
@counter
MD=M-1
MD=M-1
@LOOP
@LOOP
D;JGT
D;JGT
(END)
(END)
@END
@END
0;JMP
0;JMP
slide 10

'

Handling symbols: user-defined symbols
3

-

- *
6

,

(XXX)
XXX
!

4

-

xxx

,
.

(xxx)

,
& 7

/08 3
,

%

%

& 7

"

%'
,

:

9& 7

/0

%

,

;
8

-

%
,

& 7

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

@R0
@R0
D=M
D=M
@END
@END
D;JLE
D;JLE
@counter
@counter
M=D
M=D
@SCREEN
@SCREEN
D=A
D=A
@x
@x
M=D
M=D
(LOOP)
(LOOP)
@x
@x
A=M
A=M
M=-1
M=-1
@x
@x
D=M
D=M
@32
@32
D=D+A
D=D+A
@x
@x
M=D
M=D
@counter
@counter
MD=M-1
MD=M-1
@LOOP
@LOOP
D;JGT
D;JGT
(END)
(END)
@END
@END
0;JMP
0;JMP
slide 11

'

Handling symbols: pre-defined symbols
4

R0% % R15
& 7

+?@

A@BC0%

,
"
47

-

SP% LCL% ARG% THIS%

$

!

"

& 7
@%

THAT
D

,

47

%

,

,
!

R0% % R4
% ,

"

9-

:
& 7

;
8

-

%
,

& 7

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

@R0
@R0
D=M
D=M
@END
@END
D;JLE
D;JLE
@counter
@counter
M=D
M=D
@SCREEN
@SCREEN
D=A
D=A
@x
@x
M=D
M=D
(LOOP)
(LOOP)
@x
@x
A=M
A=M
M=-1
M=-1
@x
@x
D=M
D=M
@32
@32
D=D+A
D=D+A
@x
@x
M=D
M=D
@counter
@counter
MD=M-1
MD=M-1
@LOOP
@LOOP
D;JGT
D;JGT
(END)
(END)
@END
@END
0;JMP
0;JMP
slide 12

Handling symbols: symbol table
!

"

//
// Computes
Computes 1+...+RAM[0]
1+...+RAM[0]
//
// And
And stored
stored the
the sum
sum in
in RAM[1]
RAM[1]
@i
@i
M=1
M=1 //
// ii == 11
@sum
@sum
M=0
M=0 //
// sum
sum == 00
(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M
D=M
@R0
@R0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
@i
//
@i
// sum
sum +=
+= ii
D=M
D=M
@sum
@sum
M=D+M
M=D+M
@i
//
@i
// i++
i++
M=M+1
M=M+1
@LOOP
@LOOP //
// goto
goto LOOP
LOOP
0;JMP
0;JMP
(WRITE)
(WRITE)
@sum
@sum
D=M
D=M
@R1
@R1
M=D
M=D //
// RAM[1]
RAM[1] == the
the sum
sum
(END)
(END)
@END
@END
0;JMP
0;JMP

R0
R0
R1
R1
R2
R2
...
...
R15
R15
SCREEN
SCREEN
KBD
KBD
SP
SP
LCL
LCL
ARG
ARG
THIS
THIS
THAT
THAT
WRITE
WRITE
END
END
ii
sum
sum

00
11
22
...
...
15
15
16384
16384
24576
24576
00
11
22
33
44
18
18
22
22
16
16
17
17

%

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 13

Handling symbols: constructing the symbol table
!

"

//
// Computes
Computes 1+...+RAM[0]
1+...+RAM[0]
//
// And
And stored
stored the
the sum
sum in
in RAM[1]
RAM[1]
@i
@i
M=1
M=1 //
// ii == 11
@sum
@sum
M=0
M=0 //
// sum
sum == 00
(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M
D=M
@R0
@R0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
@i
//
@i
// sum
sum +=
+= ii
D=M
D=M
@sum
@sum
M=D+M
M=D+M
@i
//
@i
// i++
i++
M=M+1
M=M+1
@LOOP
@LOOP //
// goto
goto LOOP
LOOP
0;JMP
0;JMP
(WRITE)
(WRITE)
@sum
@sum
D=M
D=M
@R1
@R1
M=D
M=D //
// RAM[1]
RAM[1] == the
the sum
sum
(END)
(END)
@END
@END
0;JMP
0;JMP

R0
R0
R1
R1
R2
R2
...
...
R15
R15
SCREEN
SCREEN
KBD
KBD
SP
SP
LCL
LCL
ARG
ARG
THIS
THIS
THAT
THAT
WRITE
WRITE
END
END
ii
sum
sum

+

E

00
11
22
15
15
16384
16384
24576
24576
00
11
22
33
44
18
18
22
22
16
16
17
17

-

2

-

%
"
-

,

+

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

%
%
slide 14

The assembly process (detailed)
+

E

-

2
2

E

%

(LABEL)
FLABEL % G
-

%

+

C

+

@xxx

+

@xxx

-

%
%

xxx
%

xxx

+

%

,

$
+

%

,

Fxxx % G

%

%

!

,

& 7

$

)

-

& 7

%

/0"
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 15

The result ...
!

"

//
// Computes
Computes 1+...+RAM[0]
1+...+RAM[0]
//
// And
And stored
stored the
the sum
sum in
in RAM[1]
RAM[1]
@i
@i
M=1
M=1 //
// ii == 11
@sum
@sum
M=0
M=0 //
// sum
sum == 00
(LOOP)
(LOOP)
@i
//
@i
// if
if i>RAM[0]
i>RAM[0] goto
goto WRITE
WRITE
D=M
D=M
@R0
@R0
D=D-M
D=D-M
@WRITE
@WRITE
D;JGT
D;JGT
@i
//
@i
// sum
sum +=
+= ii
D=M
D=M
@sum
@sum
M=D+M
M=D+M
@i
//
@i
// i++
i++
M=M+1
M=M+1
@LOOP
@LOOP //
// goto
goto LOOP
LOOP
0;JMP
0;JMP
(WRITE)
(WRITE)
@sum
@sum
D=M
D=M
@R1
@R1
M=D
M=D //
// RAM[1]
RAM[1] == the
the sum
sum
(END)
(END)
@END
@END
0;JMP
0;JMP

assemble

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
1111000010001000
1111000010001000
0000000000010000
0000000000010000
1111110111001000
1111110111001000
0000000000000100
0000000000000100
1110101010000111
1110101010000111
0000000000010001
0000000000010001
1111110000010000
1111110000010000
0000000000000001
0000000000000001
1110001100001000
1110001100001000
0000000000010110
0000000000010110
1110101010000111
1110101010000111

H
"

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler

slide 16

Proposed assembler implementation
,
%
Parser: *
,

Code:

%

,
SymbolTable: 7
Main: +

E

+