Lecture 6
Lecture 6
Lecture 6
Assembler
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 1
Nand to Tetris Roadmap: Hardware
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 2
Program translation
Assembly program Binary code Computer
// Program: Sum1ToN (R0 represents N) 0101111100111100
// Computes R1 = 1 + 2 + 3 + ... + R0 1010101010101010
// Usage: put a value >= 1 in R0 1100000010101010
// i = 1 1011000010000001
@i
0101111100111100
M=1
// sum = 0 1010101010101010
@sum assembler 1100000010101010 load and
M=0 0101111100111100 execute
(LOOP) 1010101010101010
// if (i > R0) goto STOP 1100000010101010
@i
1011000010000001
D=M
@R0 0101111100111100
D=D-M 1010101010101010
@STOP 1100000010101010
D;JGT
// sum = sum + i
0101111100111100 The assembler is…
1010101010101010
@sum
D=M
1100000010101010 • The “linchpin” that connects the hardware
1011000010000001
@i
D=D+M 0101111100111100
platform and the software hierarchy
@sum 1010101010101010
M=D 1100000010101010 • The lowest rung in the set of translators
// i = i + 1
@i
... developed in Part II of the course (compiler,
M=M+1 VM translator, assembler)
// goto LOOP
@LOOP
0;JMP
• A program that introduces key software
... engineering techniques (parsing, code
generation, symbol tables, …)
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Lecture plan
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Translating A-instructions
Symbolic syntax: Binary syntax:
@ xxx 0vvvvvvvvvvvvvvvv
Where:
Where xxx is a non-negative
decimal value, or a symbol 0 is the A-instruction op-code, and
bound to such a value v v v … v is a binary value
Example:
@17 translate 0000000000010001
Implementation
If xxx is a decimal value: Translate the value into its 16-bit representation;
If xxx is a symbol: Later.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 5
Lecture plan
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 6
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 9
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 11
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 12
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Translating C-instructions
Symbolic syntax: dest = comp ; jump
Binary syntax: 1 1 1 a c c c c c c d d d j j j
Implementation: Get the binary code of each field of the symbolic instruction
(dest, comp, jump), and assemble the codes into a 16-bit instruction.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 14
Chapter 6: Assembler
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 15
Program translation
Symbolic code Binary code
// Computes R1=1 + ... + R0 0000000000010000
// i = 1
@i
Translate 1110111111001000
0000000000010001
M=1 1110101010001000
// sum = 0 0000000000010000
@sum 1111110000010000
M=0 Need to Handle 0000000000000000
(LOOP) 1111010011010000
// if i>R0 goto STOP 0000000000010010
@i • White space 1110001100000001
D=M 0000000000010000
@R0 1111110000010000
D=D-M • Instructions 0000000000010001
@STOP 1111000010001000
D;JGT 0000000000010000
// sum += i
• Symbols 1111110111001000
@i 0000000000000100
D=M 1110101010000111
@sum 0000000000010001
M=D+M 1111110000010000
// i++ ...
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 16
Program translation
Symbolic code Binary code
// Computes R1=1 + ... + R0 0000000000010000
// i = 1
@i
Translate 1110111111001000
0000000000010001
M=1 1110101010001000
// sum = 0 0000000000010000
@sum 1111110000010000
M=0 Need to Handle 0000000000000000
(LOOP) 1111010011010000
// if i>R0 goto STOP 0000000000010010
@i • White space 1110001100000001
D=M 0000000000010000
@R0 1111110000010000
D=D-M • Instructions 0000000000010001
@STOP 1111000010001000
D;JGT 0000000000010000
// sum += i
• Symbols 1111110111001000
@i 0000000000000100
D=M 1110101010000111
@sum 0000000000010001
M=D+M We’ll start with programs 1111110000010000
// i++ that have no symbols, ...
@i
M=M+1 and handle symbols later
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 17
Program translation
Symbolic code Binary code
// Computes R1=1 + ... + R0
// i = 1
@16
Translate
M=1
// sum = 0
@17
M=0 Need to Handle
// if i>R0 goto STOP
@16 • White space
D=M
@0
D=D-M • Instructions
@18
D;JGT
// sum += i
• Symbols (later)
@16
D=M
@17
M=D+M
// i++
@16
M=M+1
@4
0;JMP
@17
D=M
... no symbols
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 18
Program translation
Symbolic code Binary code
// Computes R1=1 + ... + R0
// i = 1
@16
Translate
M=1
// sum = 0
@17
M=0 Need to Handle
// if i>R0 goto STOP
@16 • White space Ignore it
D=M
@0
D=D-M • Instructions
@18
D;JGT
// sum += i
• Symbols (later)
@16
D=M
@17 White space:
M=D+M
// i++ Empty lines,
@16 Comments,
M=M+1
@4 Indentation
0;JMP
@17
D=M
... no symbols
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 19
Program translation
Symbolic code Binary code
@16
M=1
@17
Translate
M=0
@16
D=M
@0 Need to Handle
D=D-M
@18
D;JGT • White space
@16
D=M
@17 • Instructions
M=D+M
@16
M=M+1
• Symbols (later)
@4
0;JMP
@17
D=M
no white space
...no symbols
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 20
Program translation
Symbolic code Binary code
@16
M=1
@17
Translate
M=0
@16
D=M
@0 Need to Handle
D=D-M
@18
D;JGT • White space
@16 Translate,
D=M
@17 • Instructions one by one
M=D+M
@16
• Symbols (later) As shown
M=M+1
@4
earlier in
0;JMP the lecture
@17
D=M
...no symbols
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 21
Program translation
Symbolic code Binary code
@16 0000000000010000
M=1
@17
Translate 1110111111001000
0000000000010001
M=0 1110101010001000
@16 0000000000010000
D=M 1111110000010000
@0 Need to Handle 0000000000000000
D=D-M 1111010011010000
@18 0000000000010010
D;JGT • White space 1110001100000001
@16 Translate, 0000000000010000
D=M 1111110000010000
@17 • Instructions one by one 0000000000010001
M=D+M 1111000010001000
@16
• Symbols (later) As shown 0000000000010000
M=M+1 1111110111001000
@4
earlier in 0000000000000100
0;JMP the lecture 1110101010000111
@17 0000000000010001
D=M 1111110000010000
...no symbols ...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 22
Program translation
Symbolic code Binary code
@16 0000000000010000
M=1
@17
Translate 1110111111001000
0000000000010001
M=0 1110101010001000
@16 0000000000010000
D=M 1111110000010000
@0 Need to Handle 0000000000000000
D=D-M 1111010011010000
@18 0000000000010010
D;JGT • White space 1110001100000001
@16 0000000000010000
D=M 1111110000010000
@17 • Instructions 0000000000010001
M=D+M 1111000010001000
@16 0000000000010000
M=M+1
• Symbols (later) 1111110111001000
@4 0000000000000100
0;JMP 1110101010000111
@17 0000000000010001
D=M 1111110000010000
...no symbols ...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 23
Program translation
Symbolic code Binary code
// Computes R1=1 + ... + R0
// i = 1
@i
Translate
M=1
// sum = 0
@sum
M=0 Need to Handle
(LOOP)
// if i>R0 goto STOP
@i • White space
D=M
@R0
D=D-M • Instructions
@STOP
D;JGT
// sum += i
• Symbols (later)
@i
D=M
@sum
M=D+M
// no
i++
symbols Original program,
@i with symbols
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 24
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0
// i = 1 Symbols
@i
M=1
// sum = 0 • Predefined symbols
@sum
M=0
(LOOP)
• Label symbols
// if i>R0 goto STOP
@i • Variable symbols
D=M
@R0
D=D-M
@STOP
D;JGT
// sum += i
@i
D=M
@sum
M=D+M
// i++ Original program,
@i with symbols
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 25
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0 The Hack language features
// i = 1 Symbols
@i 23 predefined symbols:
M=1
// sum = 0 • Predefined symbols symbol value
@sum R0 0
M=0 R1 1
(LOOP)
• Label symbols R2 2
// if i>R0 goto STOP ... ...
@i • Variable symbols R15 15
D=M SCREEN 16384
@R0 KBD 24576
D=D-M SP 0
@STOP This particular code uses LCL 1
D;JGT ARG 2
// sum += i one predefined symbol: R0 THIS 3
@i THAT 4
D=M
@sum
M=D+M
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 26
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0 The Hack language features
// i = 1 Symbols
@i 23 predefined symbols:
M=1
// sum = 0 • Predefined symbols symbol value
@sum R0 0
M=0 R1 1
(LOOP)
• Label symbols R2 2
// if i>R0 goto STOP ... ...
@i • Variable symbols R15 15
D=M SCREEN 16384
@R0 KBD 24576
D=D-M SP 0
@STOP LCL 1
D;JGT ARG 2
// sum += i
@i Translating @ preDefinedSymbol THIS 3
THAT 4
D=M
@sum
Replace preDefinedSymbol with its value,
M=D+M and complete the translation.
// i++
@i Examples: @R0 0000000000000000
M=M+1
@LOOP @R12 0000000000001100
0;JMP
(STOP) @SCREEN 0100000000000000
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 27
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0
// i = 1 Symbols
@i
M=1
// sum = 0 • Predefined symbols
@sum
M=0
(LOOP)
• Label symbols
// if i>R0 goto STOP
@i • Variable symbols
D=M
@R0
D=D-M
@STOP
D;JGT
// sum += i
@i
D=M
@sum This particular code uses two
M=D+M
label symbols: LOOP, STOP
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 28
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0 Label symbols
// i = 1
@i • Used to label destinations of goto instructions
M=1 • Declared by the pseudo-instruction (label)
// sum = 0
@sum • The (label) directive defines the symbol label to refer
M=0
(LOOP)
to the memory location holding the next instruction in
// if i>R0 goto STOP the program,
@i
D=M
• Which corresponds to the instruction’s line number
@R0
D=D-M
@STOP
D;JGT
// sum += i
@i
D=M
@sum This particular code uses two
M=D+M
label symbols: LOOP, STOP
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 29
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0 Label symbols
// i = 1
0 @i • Used to label destinations of goto instructions
1 M=1 • Declared by the pseudo-instruction (label)
// sum = 0
2 @sum • The (label) directive defines the symbol label to refer
3 M=0
(LOOP)
to the memory location holding the next instruction in
// if i>R0 goto STOP the program,
4 @i
5 D=M
• Which corresponds to the instruction’s line number
6 @R0
7 D=D-M Example: symbol value
8 @STOP LOOP 4
9 D;JGT
STOP 18
// sum += i
10 @i
11 D=M
12 @sum
Translating @ labelSymbol :
13 M=D+M
// i++ Replace labelSymbol with its value
14 @i
15 M=M+1
16 @LOOP
17 0;JMP Example: @LOOP 0000000000000100
(STOP)
18 @sum
19 D=M
... ...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 30
Handling symbols
Symbolic code
// Computes R1=1 + ... + R0
// i = 1
Symbols
@i
M=1 • Predefined symbols
// sum = 0
@sum
M=0 • Label symbols
(LOOP)
// if i>R0 goto STOP
@i • Variable symbols
D=M
@R0
D=D-M
@STOP
D;JGT
// sum += i
@i
D=M
@sum This particular code uses two
M=D+M
variable symbols: i, sum
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 31
Handling symbols
Symbolic code Variable symbols
// Computes R1=1 + ... + R0
// i = 1 • Any symbol xxx which is neither predefined,
@i nor defined elsewhere using an (xxx) label
M=1
// sum = 0
declaration, is treated as a variable
@sum • Hack convention: Each variable is bound to a
M=0
(LOOP) running memory address, starting at 16
// if i>R0 goto STOP
@i
D=M
@R0
D=D-M
@STOP
D;JGT
// sum += i
@i
D=M
@sum This particular code uses two
M=D+M
variable symbols: i, sum
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 32
Handling symbols
Symbolic code Variable symbols
// Computes R1=1 + ... + R0
// i = 1 • Any symbol xxx which is neither predefined,
@i nor defined elsewhere using an (xxx) label
M=1
// sum = 0
declaration, is treated as a variable
@sum • Hack convention: Each variable is bound to a
M=0
(LOOP) running memory address, starting at 16
// if i>R0 goto STOP
@i symbol value
D=M
Example:
@R0 i 16
D=D-M sum 17
@STOP
D;JGT
// sum += i Translating @ variableSymbol :
@i 1. If variableSymbol is seen for the first time,
D=M
@sum bind to it to a value, from 16 onward
M=D+M 2. Else, it has a value
// i++
@i 2. Replace variableSymbol with its value.
M=M+1
@LOOP
0;JMP Example: @sum 0000000000010001
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 33
Handling symbols
Symbolic code Symbol table
// Computes R1=1 + ... + R0
symbol value A data structure that the
// i = 1
@i R0 0 assembler creates and uses
M=1 R1 1 during the program translation
// sum = 0
R2 2
@sum
M=0 ... ... Contains every symbol,
(LOOP) R15 15 and its binding.
// if i>R0 goto STOP SCREEN 16384
@i
KBD 24576
D=M
@R0 SP 0
D=D-M LCL 1
@STOP ARG 2
D;JGT THIS 3
// sum += i
@i THAT 4
D=M LOOP 4
@sum STOP 18
M=D+M i 16
// i++
@i sum 17
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 34
Handling symbols
Symbolic code Symbol table
// Computes R1=1 + ... + R0 symbol value
// i = 1 A data structure that the
@i assembler creates and uses
M=1 during the program translation
// sum = 0
@sum
M=0
(LOOP)
// if i>R0 goto STOP
@i
D=M
@R0
D=D-M
@STOP
D;JGT
// sum += i
@i
D=M
@sum
M=D+M
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 35
Handling symbols
Symbolic code Symbol table
// Computes R1=1 + ... + R0 symbol value
// i = 1 A data structure that the
@i R0 0 assembler creates and uses
M=1 R1 1 during the program translation
// sum = 0
R2 2
@sum
M=0 ... ...
(LOOP) R15 15
// if i>R0 goto STOP SCREEN 16384 Initialization:
@i KBD 24576 Creates the symbol table and adds the
D=M
SP 0
predefined symbols to the table
@R0
D=D-M LCL 1
@STOP ARG 2
D;JGT THIS 3
// sum += i
@i THAT 4
D=M
@sum
M=D+M
// i++
@i
M=M+1
@LOOP
0;JMP
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 36
Handling symbols
Symbolic code Symbol table
// Computes R1=1 + ... + R0 symbol value
// i = 1 A data structure that the
0 @i R0 0 assembler creates and uses
1 M=1 R1 1 during the program translation
// sum = 0
R2 2
2 @sum
3 M=0 ... ...
(LOOP) R15 15
// if i>R0 goto STOP SCREEN 16384 Initialization:
4 @i KBD 24576 Creates the symbol table and adds the
5 D=M
SP 0
predefined symbols to the table
6 @R0
7 D=D-M LCL 1
8 @STOP ARG 2
9 D;JGT THIS 3
// sum += i
@i THAT 4
10
11 D=M LOOP 4 First pass: Counts lines and adds
12 @sum STOP 18 the label symbols to the table
13 M=D+M
// i++
14 @i
15 M=M+1
16 @LOOP
17 0;JMP
(STOP)
18 @sum
19 D=M
... ...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 37
Handling symbols
Symbolic code Symbol table
// Computes R1=1 + ... + R0 symbol value
// i = 1 A data structure that the
@i R0 0 assembler creates and uses
M=1 R1 1 during the program translation
// sum = 0
R2 2
@sum
M=0 ... ...
(LOOP) R15 15
// if i>R0 goto STOP SCREEN 16384 Initialization:
@i KBD 24576 Creates the symbol table and adds the
D=M
SP 0
predefined symbols to the table
@R0
D=D-M LCL 1
@STOP ARG 2
D;JGT THIS 3
// sum += i
@i THAT 4
D=M LOOP 4 First pass: Counts lines and adds
@sum STOP 18 the label symbols to the table
M=D+M i 16
// i++ Second pass:
sum 17 • Generates binary code; in the process:
@i
M=M+1 • Adds the variable symbols to the table
@LOOP
0;JMP (details, soon)
(STOP)
@sum
D=M
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 38
Lecture plan
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 39
Assembler: Usage
Input (Prog.asm): a text file containing a
sequence of lines, each being a string
representing a comment, an A-instruction,
a C-instruction, or a label declaration
Output (Prog.hack): a text file containing
a sequence of lines, each being a string
of sixteen 0 and 1 characters
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 40
Assembler: Algorithm
Initialize
Opens the input file (Prog.asm),
and gets ready to process it
Constructs a symbol table,
and adds to it all the predefined symbols
First pass
Reads the program lines, one by one, Assembler implementation options
focusing only on (label) declarations.
Adds the found labels to the symbol table • Manual
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 41
Assembler: Architecture
drives the
HackAssembler translation process
Proposed architecture
• Four software modules
• Can be realized in any programming language
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 42
HackAssembler
Initialize:
Opens the input file (Prog.asm) and gets ready to process it
Constructs a symbol table, and adds to it all the predefined symbols
First pass:
Reads the program lines, one by one
focusing only on (label) declarations.
Adds the found labels to the symbol table The HackAssembler
implements this
Second pass (main loop): assembly algorithm,
(starts again from the beginning of the file) using the services of:
While there are more lines to process: • Parser
Gets the next instruction, and parses it • Code
If the instruction is @ symbol • SymbolTable
If symbol is not in the symbol table, adds it to the table
Translates the symbol into its binary value
If the instruction is dest = comp ; jump
Translates each of the three fields into its binary value
Assembles the binary values into a string of sixteen 0’s and 1’s
Writes the string to the output file.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 43
Assembler API
drives the
HackAssembler
process
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 44
Parser API
Routines
• Constructor / initializer: Creates a Parser and opens the source text file
current instruction
Examples: @17 instructionType() returns A_INSTRUCTION
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 45
Parser API
Routines
• Constructor / initializer: Creates a Parser and opens the source text file
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 46
Parser API
Routines
• Constructor / initializer: Creates a Parser and opens the source text file
current instruction
Examples: D=D+1;JLE dest() returns "D" comp() returns "D+1" jump() returns "JLE"
M=-1 dest() returns "M" comp() returns "-1" jump() returns null
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 47
Implementation
drives the
process
HackAssembler
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 48
Code API
Deals only with C-instructions: dest = comp ; jump
Routines:
dest(string): Returns the binary representation of the parsed dest field (string)
comp(string): Returns the binary representation of the parsed comp field (string)
jump(string): Returns the binary representation of the parsed jump field (string)
Examples:
dest("DM") returns "011"
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 49
Implementation
drives the
process
HackAssembler
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 50
SymbolTable API
Routines
Constructor / initializer: Creates and initializes a SymbolTable
void addEntry(String symbol, int address): Adds <symbol, address> to the table
Symbol
table:
(example)
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 51
HackAssembler: Drives the translation process
drives the
process
HackAssembler
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 52
Assembler API (detailed)
Parser module:
53
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 53
Assembler API (detailed)
Code module:
SymbolTable module:
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 54
Chapter 6: Assembler
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 55
Developing a Hack Assembler
Contract
Develop a program that translates symbolic Hack programs into binary Hack instructions;
The source assembly program (input) is read from a text file named Prog.asm
The generated binary code (output) is written to a text file named Prog.hack
Assumption: Prog.asm is error-free.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 56
Testing
Prog.asm Prog.hack
// Computes R1 = 1 + ... + R0 0000000000010000
// i = 1 1110111111001000
@i 0000000000010001
M=1 1110101010001000 CPU
// sum = 0 Your 0000000000010000 Load / Emulator
1111110000010000
@sum
M=0
assembler 0000000000000000 Run
(LOOP) 1111010011010000
// if i > R0 goto STOP 0000000000010010
1110001100000001
@i
0000000000010000
D=M
1111110000010000
@R0
0000000000010001
D=D-M
@STOP 1111000010001000
0000000000010000
D;JGT
...
...
Testing strategy
To test your assembler’s correctness, you will use it to translate some given test assembly programs;
If the resulting binary code will execute correctly, we’ll assume that your assembler is correct.
Not a complete test, but that’s the project 6 contract.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 57
Testing
Prog.asm Prog.hack
// Computes R1 = 1 + ... + R0 0000000000010000
// i = 1 1110111111001000
@i 0000000000010001
M=1 1110101010001000 CPU
// sum = 0 Your 0000000000010000 Load / Emulator
1111110000010000
@sum
M=0
assembler 0000000000000000 Run
(LOOP) 1111010011010000
// if i > R0 goto STOP 0000000000010010
1110001100000001
@i
0000000000010000
D=M
1111110000010000
@R0
0000000000010001
D=D-M
@STOP 1111000010001000
0000000000010000
D;JGT
...
...
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 58
Testing: Add
Add.asm Testing on the CPU emulator:
// Computes RAM[0] = 2 + 3
@2
D=A
@3
D=D+A
@0
M=D
Techincal note
When loading a binary
Prog.hack file into the
CPU emulator, the
emulator may present the
code symbolically, for
readability (depending on
the emulator’s version).
To inspect the binary code,
select “binary” from the
1. Translate Add.asm using your assembler
ROM menu. 2. Load into the CPU emulator the translated Add.hack
3. Run the code, inspect R0.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 59
Testing: Max
Max.asm MaxL.asm
// Computes RAM[2] = // Computes RAM[2] =
// max(RAM[0],RAM[1]) // max(RAM[0],RAM[1])
@R0 @0
D=M D=M
@R1 @1
D=D-M D=D-M
@OUTPUT_RAM0 @12
D;JGT D;JGT
// Output RAM[1] // Output RAM[1]
@R1 @1
D=M D=M
@R2 @2
(Same test program,
M=D M=D without symbols, for unit-
@END
0;JMP
@16
0;JMP
testing the basic assembler)
(OUTPUT_RAM0)
@R0 @0
D=M D=M
@R2 @2
M=D M=D
(END)
@END @16
0;JMP 0;JMP
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 61
Testing: Rect
Rect.asm Testing on the CPU emulator:
// Draws a rectangle,
// 16 pixels wide,
// R0 pixels high,
// at the screen’s top-left.
@R0
D=M
@n
M=D
@i
M=0
@SCREEN
D=A
@address
M=D
(LOOP)
@i
D=M
@n
D=D-M 1. Translate Rect.asm
@END
D;JGT 2. Load Rect.hack
...
3. Put a non-negative value in R0, run the code, inspect the screen.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 62
Testing: Pong
Pong.asm
// Pong game
@256
D=A
@SP
Pong game action
M=D
@133
0;JMP
@R15
M=D
@SP
AM=M-1
D=M
A=A-1
D=M-D
M=0
@END_EQ
D;JNE
@SP
A=M-1
M=-1
(END_EQ)
@R15
A=M
Translate Pong.asm, load Pong.hack, and then play the game:
... Select “no animation” from the Animate menu, set the speed slider to “fast”,
and run the code. Move the paddle using the left- and right-arrow keys.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 63
Testing: Pong
Pong.asm
// Pong game
Background
@256 The source Pong program was written in the high-level Jack language;
D=A
@SP The computer’s operating system is also written in Jack;
M=D
@133
The Pong code + the OS code were compiled by the Jack compiler,
0;JMP creating a single file named Pong.asm;
@R15
This file contains many compiler-generated addresses and symbols.
M=D
@SP
AM=M-1
D=M
A=A-1
D=M-D
M=0
@END_EQ
D;JNE
@SP
A=M-1
M=-1
(END_EQ)
@R15
A=M
... 28,374 instructions
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 64
Testing option II: Using the hardware simulator
1. Use your assembler to translate Prog.asm, generating the executable file Prog.hack
2. Put the Prog.hack file in a folder containing the chips that you developed in project 5:
Computer.hdl, CPU.hdl, and Memory.hdl
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 65
Testing option III: Using the supplied assembler
Prog.hack file, Prog.hack file,
translated by the translated by your
supplied assembler assembler
Source
Prog.asm
test file
1. Use your assembler to translate Prog.asm, generating the executable file Prog.hack
2. Load Prog.asm into the supplied assembler, and load Prog.hack as a compare file
3. Translate Prog.hack, and inspect the code comparison feedback messages.
Nand to Tetris / www.nand2tetris.org / Chapter 6 / Copyright © Noam Nisan and Shimon Schocken Slide 66