SlideShare a Scribd company logo
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 1
Lecture 4
Machine Language
From Nand to Tetris
Building a Modern Computer from First Principles
These slides support chapter 4 of the book
The Elements of Computing Systems
By Noam Nisan and Shimon Schocken
MIT Press, 2021
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 2
Nand to Tetris Roadmap: Hardware
abstraction
machine
language
hardware platform
abstraction
computer abstraction
ALU, RAM abstraction
elementary
logic gates
building a
computer building
chips building
gates
p3
p2
p1
In previous projects we’ve built
the computer’s ALU and RAM
developing
an assembler
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 3
Nand to Tetris Roadmap: Hardware
abstraction
machine
language
p4
We will now integrate these
modules into a programmable,
general-purpose computer
hardware platform
abstraction
computer abstraction
ALU, RAM abstraction
elementary
logic gates
building a
computer building
chips building
gates
p3
p2
p1
p5
developing
an assembler
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 4
Nand to Tetris Roadmap: Hardware
hardware platform
abstraction
computer abstraction
ALU, RAM abstraction
elementary
logic gates
building a
computer building
chips building
gates
p3
abstraction
machine
language
p2
p1
p4
To get started, we’ll treat the computer
as an abstraction, and learn how to use
it through its interface: the computer’s
machine language.
developing
an assembler
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 5
Nand to Tetris Roadmap: Hardware
hardware platform
abstraction
computer abstraction
ALU, RAM abstraction
elementary
logic gates
building a
computer building
chips building
gates
p3
abstraction
machine
language
p2
p1
developing
an assembler
We’ll start with a broad overview of:
• Computer architectures
• Machine languages
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 6
Computer systems are flexible and versatile
Same hardware can run many different programs (software)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 7
Computer systems are flexible and versatile
Ada Lovelace
(1843)
Early symbolic program
Landmark “proof of concept” that a fixed computer
can be programmed to perfom different tasks
Same hardware can run many different programs (software)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 8
Computer systems are flexible and versatile
Alan Turing
(1936)
Universal Turing Machine
Landmark paper, describing a theoretical
general-purpose computer
Same hardware can run many different programs (software)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 9
Computer systems are flexible and versatile
Landmark general-purpose computer
ENIAC, University of Pennsylvania
Same hardware can run many different programs (software)
John Von Neumann
(1945)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 10
Computer architecture
ALU
program
Memory CPU
registers
output
input
data
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 11
Computer architecture
ALU
Memory CPU
registers
0
1
2
...
1100101010010101
1100100101100111
0011001010101011
...
instructions
data
output
input
0101110011100110
1100000010010001
1110001011111100
...
instruction
data value
Stored program concept
• The computer memory can store
programs, just like it stores data
• Programs = data.
A fundamental idea in the
history of computer science
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 12
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Lecture plan
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 13
Machine Language
CPU
ALU
10
5
137



Registers
R1
R2
A
Memory
12
615
8828
0
1
2
3
3
-5
...
955
20
136
137
-523
138
...
Computer
(conceptual definition):
A processor (CPU) that
manipulates a set of registers:
• CPU-resident registers
(few, accessed directly, by name)
• Memory-resident registers
(many, accessed by address)
Machine language
A formalism for accessing and manipulating registers.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 14
Registers
Data registers
Hold data values
Address register
Holds an address
Instruction register
Holds an instruction
• All these registers are… registers (containers that hold bits)
• The number and bit-width of the registers vary from one computer to another.
CPU
ALU
10
5
137



Registers
R1
R2
A
Memory
12
615
8828
0
1
2
3
3
-5
...
955
20
136
137
-523
138
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 15
Typical operations (using, for example, a RISC syntax)
// R1  R1 + R2
add R1, R2
// R1  R1 + 73
addi R1, 73
// R1  R2
mov R1, R2
// R1  Memory[137]
load R1, 137
// if R1>0 goto 15
jgt R1, 15
CPU
ALU
10
5
137



Registers
R1
R2
A
Memory
12
615
8828
0
1
2
3
3
-5
...
955
20
136
137
-523
138
...
The syntax of machine languages varies across computers
The semantics is the same: Manipulating registers.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 16
Typical operations
CPU
ALU
000000000010010
0000000000000101
00000010110000



Registers
R1
R2
A
Memory
1110111100001010
1100000011111110
0110111100001010
0
1
2
1100000011111110
3
0101100011100110
...
0001100011011100
1100000011111110
136
137
0101110010001110
138
...
current
instruction
Which instruction should be
executed next?
• By default, the CPU executes
the next instruction
• Sometimes we want to “jump”
to execute another instruction
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 17
Typical operations
Branching
• Execute an instruction other than the next one
• Example: Embarking on a new iteration in a loop
...
// Adds 1 to R1, repetitively
(LOOP)
addi R1,1
...
goto LOOP
...
Symbolic version
...
// Adds 1 to R1, repetitively
addi R1,1
...
goto 13
...
Basic version
13
...
27
...
Programs with symbolic references are …
• Easier to develop
• Readable
• Relocatable.
• Line numbers
• Physical addresses
• No line numbers
• Symbolic addresses
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 18
Typical operations
Symbolic program
Conditional branching
Sometimes we want to “jump” to execute an instruction,
but only if a certain condition is met
// Sets R2 to abs(R1)
// R2 ← R1
mov R2,R1
// if (R2 > 0) goto cont
jgt R2,CONT
// R2 ← –R1
movi R2,0
subR2,R1
CONT:
// Here R2 = abs(R1)
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 19
// Sets R2 to abs(R1)
// R2 ← R1
mov R2,R1
// if (R2 > 0) goto cont
jgt R2,CONT
// R2 ← –R1
movi R2,0
subR2,R1
CONT:
// Here R2 = abs(R1)
...
Program translation
Symbolic program
0101111100111100
1010101010101010
1100000010101010
1011000010000001
...
Binary code
translate
load and
execute
Assembler
(tool)
Assembly
(language)
Translation
Before it can be executed, a symbolic program must be translated into binary
instructions that the computer can decode and execute.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 20
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 21
The Hack computer
Hack: a 16-bit computer, featuring two memory units
address
ROM
Instruction memory
out
RAM
address
Data memory
out
Address register Data register
A
0
1
2
...
D
0010101010110110
1111100100101011
0011101001011011
1110011001011001
0000110011100111
1000110000110000
0000010011111100
0
1
2
...
0000011100110010
1001000011110101
0001101001101111
(Conceptual, partial view of the
Hack computer architecture)
...
32766
32767
...
32766
32767
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 22
Memory
address
ROM
Instruction memory
out
RAM
address
Data memory
out
Address register Data register
A
...
32766
32767
...
32766
32767
0
1
2
...
D
0010101010110110
1111100100101011
0011101001011011
1110011001011001
0000110011100111
1000110000110000
0000010011111100
0
1
2
...
0000111100110010
0000011100110010
1001000011110101
0001101001101111
M
RAM
• Read-write data memory
• Addressed by the A register
• The selected memory location, RAM[A],
is referred to as M
(Conceptual, partial view of the
Hack computer architecture)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 23
Memory
address
ROM
Instruction memory
out
RAM
address
Data memory
out
Address register Data register
A
...
32766
32767
...
32766
32767
0
1
2
...
1001111100011001
D
0010101010110110
1111100100101011
0011101001011011
1110011001011001
0000110011100111
1000110000110000
0000010011111100
0
1
2
...
0000111100110010
0000011100110010
1001000011110101
0001101001101111
M
Loaded with a sequence of
16-bit Hack instructions
Should we focus on RAM[A], or on ROM[A]?
Depends on the current instruction (later)
ROM
• Read-only instruction memory
• Addressed by the (same) A register
• The selected memory location, ROM[A],
contains the current instruction
RAM
• Read-write data memory
• Addressed by the A register
• The selected memory location, RAM[A],
is referred to as M
(Conceptual, partial view of the
Hack computer architecture)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 24
Registers
D: data register
A: address register
M: selected RAM register
address
ROM
Instruction memory
out
RAM
address
Data memory
out
Address register Data register
A
...
32766
32767
...
32766
32767
0
1
2
...
0
1
2
...
M instruction
D
(Conceptual, partial view of the
Hack computer architecture)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 25
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 26
Hack instructions
Instruction set
• A - instruction (address)
• C - instruction (compute)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 27
• A - instruction (address)
• C - instruction (compute)
Hack instructions
Instruction set
Side effects:
• RAM[A] (denoted M) becomes selected
• ROM[A] becomes selected
Example
@ 19
Semantics
A ← 19
Syntax:
where xxx is a
non-negative integer
@ xxx
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 28
Hack instructions
D=0
A=–1
M=1
...
Examples:
Syntax:
reg = {0|1|–1}
where reg = {A |D|M}
reg1 = reg2
where reg1 = {A |D |M}
reg2 = [–]{A |D|M}
D=A
D=M
M=–M
...
where reg, reg1 = {A |D|M}
reg2 = {A |D|M |1}
op = {+ |– | & | I}
reg = reg1 op reg2
D=D+M
A=A–1
M=D+1
...
(Complete / formal
syntax, later).
• A - instruction (address)
• C - instruction (compute)
Instruction set
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 29
Hack instructions
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
// D ← 2
Examples:
The game: We show a subset of Hack instructions (top left),
and practice writing code examples that use these instructions.
?
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 30
Hack instructions
Examples:
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
// D ← 2
D=1
D=D+1
// D ← 1954
Use only the instructions
shown above
?
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 31
Hack instructions
// D ← 1954
@1954
D=A
// D ← 2
D=1
D=D+1
Examples:
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
// D ← D + 23
Use only the instructions
shown above
?
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 32
Hack instructions
// D ← 1954
@1954
D=A
Observation
• In all these examples, we used both D and A as a data registers:
• The addressing side-effects of A are ignored.
// D ← 2
D=1
D=D+1
// D ← D + 23
@23
D=D+A
Examples:
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 33
Hack instructions
// RAM[100] ← 0
@100
M=0
// RAM[100] ← 17
@17
D=A
@100
M=D
• First pair of instructions:
A is used as a data register
• Second pair of instructions:
A is used as an address register
More examples:
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 34
Hack instructions
// RAM[100] ← 0
@100
M=0
// RAM[100] ← 17
@17
D=A
@100
M=D
More examples:
// RAM[100] ← RAM[200]
?
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 35
Hack instructions
// RAM[100] ← 0
@100
M=0
// RAM[100] ← 17
@17
D=A
@100
M=D
// RAM[100] ← RAM[200]
@200
D=M
@100
M=D
When we want to operate on a memory location, we use a pair of instructions:
• A-instruction: Selects a memory location
• C-instruction: Operates on the selected location.
More examples:
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 36
Hack instructions
// RAM[3] ← RAM[3] – 15
?
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 37
Hack instructions
// RAM[3] ← RAM[3] – 15
@15
D=A
@3
M=M–D
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Use only the above instructions
// RAM[3] ← RAM[4] + 1
?
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 38
Hack instructions
// RAM[3] ← RAM[3] – 15
@15
D=A
@3
M=M–D
// RAM[3] ← RAM[4] + 1
@4
D=M+1
@3
M=D
Typical instructions:
@ constant (A←constant)
D=1
D=A
D=D+1
...
D=D+A
D=M
M=0
...
M=D
D=D+A
M=M–D
...
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 39
Hack instructions
Typical instructions:
A=1
D=–1
M=0
...
A=M
D=M
M=D
...
A=D-A
D=D+A
D=D+M
...
@ constant (A←constant)
// Computes: RAM[2] = RAM[0] + RAM[1] + 17
Add.asm
? Use only the
above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 40
Hack instructions
// Computes: RAM[2] = RAM[0] + RAM[1] + 17
// D = RAM[0]
@0
D=M
// D = D + RAM[1]
@1
D=D+M
// D = D + 17
@17
D=D+A
// RAM[2] = D
@2
M=D
Add.asm
Typical instructions:
A=1
D=–1
M=0
...
A=M
D=M
M=D
...
A=D-A
D=D+A
D=D+M
...
@ constant (A←constant)
Use only the
above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 41
Hack instructions
// Computes: RAM[2] = RAM[0] + RAM[1] + 17
// D = RAM[0]
@0
D=M
// D = D + RAM[1]
@1
D=D+M
// D = D + 17
@17
D=D+A
// RAM[2] = D
@2
M=D
Add.asm
How can we tell that a given program
actually works?
• Testing / simulating
• Formal verification
@ constant (A←constant)
Typical instructions:
A=1
D=–1
M=0
...
A=M
D=M
M=D
...
A=D-A
D=D+A
D=D+M
...
@ constant (A←constant)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 42
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 43
The CPU emulator
• Software that emulates the Hack CPU
• Part of the Nand to Tetris IDE
code
screen
load/exec controls
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 44
The CPU emulator
// Computes: RAM[2] = RAM[0] + RAM[1] + 17
// D = RAM[0]
@0
D=M
// D = D + RAM[1]
@1
D=D+M
// D = D + 17
@17
D=D+A
// RAM[2] = D
@2
M=D
Add.asm (example)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 45
The CPU emulator
// Computes: RAM[2] = RAM[0] + RAM[1] + 17
// D = RAM[0]
@0
D=M
// D = D + RAM[1]
@1
D=D+M
// D = D + 17
@17
D=D+A
// RAM[2] = D
@2
M=D
Add.asm (example)
When loading a symbolic program
into our CPU emulator, the emulator
translates it into binary code (using a
built-in assembler).
Load into the
CPU emulator
0000000000000000
1000010010001101
0000000000000001
1010011001100001
0000000000010001
1001111100110011
0000000000000010
1110010010010011
Binary
Execute in the
CPU emulator
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 46
The CPU emulator
CPU emulator
demo
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 47
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 48
Loading a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
M
ROM
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
Hack program
load
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 49
Loading a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
M
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
Hack program
load
Convention:
The first instruction is
loaded into address 0,
the next instruction into
address 1, and so on.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 50
Executing a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
M
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 51
Executing a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
M
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 52
Executing a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
M
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 53
Executing a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
M
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 54
Executing a program
Instruction memory
RAM
Data memory
Address register Data register
A
0
1
2
...
0
1
2
...
D
instruction
instruction
instruction
instruction
instruction
instruction
instruction
instruction
M
• The default: Execute the next instruction
• Suppose we wish to execute another instruction;
• How to specify branching?
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 55
Branching
Flow of control:
0,1,2,3,4,
7,8,9,
2,3,4,
7,8,9,
2,3,4,
...
instruction
instruction
instruction
instruction
goto 7
instruction
instruction
instruction
instruction
goto 2
instruction
...
0
1
2
3
4
5
6
7
8
9
10
11
Unconditional branching
example (pseudocode)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 56
Branching
instruction
instruction
instruction
instruction
goto 7
instruction
instruction
instruction
instruction
goto 2
instruction
...
0
1
2
3
4
5
6
7
8
9
10
11
Unconditional branching
example (pseudocode)
…
// goto 7
@7
0;JMP
…
In Hack:
Syntax:
• Use an A-instruction to select an address
• Use a C-instruction to jump to that address
Semantics of 0;JMP
Jump to execute the instruction stored in ROM[A]
(the 0; prefix is a syntax convention)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 57
Branching
D;JGT // if D > 0 jump
Typical branching instructions:
to the
instruction
stored in
ROM[A]
Pseudocode
0
1
2
3
4
5
6
7
...
instruction
instruction
if (D>0) goto 6
instruction
instruction
instruction
instruction
instruction
...
In Hack
…
// if (D>0) goto 6
@6
D;JGT
…
Conditional branching
example
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 58
Branching
D;JGT // if D > 0 jump
D;JGE // if D ≥ 0 jump
D;JLT // if D < 0 jump
D;JLE // if D ≤ 0 jump
D;JEQ // if D = 0 jump
D;JNE // if D ≠ 0 jump
0;JMP // jump
Typical branching instructions:
0
1
2
3
4
5
6
7
...
instruction
instruction
if (D>0) goto 6
instruction
instruction
instruction
instruction
instruction
...
In Hack
to the
instruction
stored in
ROM[A]
…
// if (D>0) goto 6
@6
D;JGT
…
D can be replaced with any ALU computation: D+1, D-1, etc.
Pseudocode
Conditional branching
example
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 59
Branching
D;JGT // if D > 0 jump
D;JGE // if D ≥ 0 jump
D;JLT // if D < 0 jump
D;JLE // if D ≤ 0 jump
D;JEQ // if D = 0 jump
D;JNE // if D ≠ 0 jump
0;JMP // jump
Typical branching instructions:
to the
instruction
stored in
ROM[A]
Typical instructions:
A=1
D=-1
M=0
...
A=M
D=A
M=D
...
D=D-A
A=A-1
M=D+1
...
@ constant (A←constant)
// if )D = 0( goto 300
?
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 60
// if )D = 0( goto 300
@300
D;JEQ
Branching
D;JGT // if D > 0 jump
D;JGE // if D ≥ 0 jump
D;JLT // if D < 0 jump
D;JLE // if D ≤ 0 jump
D;JEQ // if D = 0 jump
D;JNE // if D ≠ 0 jump
0;JMP // jump
Typical branching instructions:
to the
instruction
stored in
ROM[A]
Typical instructions:
A=1
D=-1
M=0
...
A=M
D=A
M=D
...
D=D-A
A=A-1
M=D+1
...
@ constant (A←constant)
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 61
Branching
// if (RAM[3]<100) goto 12 D;JGT // if D > 0 jump
D;JGE // if D ≥ 0 jump
D;JLT // if D < 0 jump
D;JLE // if D ≤ 0 jump
D;JEQ // if D = 0 jump
D;JNE // if D ≠ 0 jump
0;JMP // jump
Typical branching instructions:
to the
instruction
stored in
ROM[A]
Typical instructions:
A=1
D=-1
M=0
...
A=M
D=A
M=D
...
D=D-A
A=A-1
M=D+1
...
@ constant (A←constant)
?
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 62
Branching
// if (RAM[3]<100) goto 12
// D = RAM[3] – 100
@3
D=M
@100
D=D–A
// if (D<0) goto 12
@12
D;JLT
D;JGT // if D > 0 jump
D;JGE // if D ≥ 0 jump
D;JLT // if D < 0 jump
D;JLE // if D ≤ 0 jump
D;JEQ // if D = 0 jump
D;JNE // if D ≠ 0 jump
0;JMP // jump
Typical branching instructions:
to the
instruction
stored in
ROM[A]
Typical instructions:
A=1
D=-1
M=0
...
A=M
D=A
M=D
...
D=D-A
A=A-1
M=D+1
...
@ constant (A←constant)
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 63
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 64
The A-instruction
Syntax:
where xxx is either a constant, or
a symbol bound to a constant
@ xxx
Examples:
@ 19
@ sym
Semantics:
A ← 19
A ← the number that sym is bound to
This idiom can be used for realizing:
• Variables
• Labels
• A - instruction
• C - instruction
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 65
Variables
...
i = 1
sum = 0
...
sum = sum + i
i = i + 1
...
Pseudocode (example)
...
// i = 1
Hack assembly
write
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 66
Variables
...
i = 1
sum = 0
...
sum = sum + i
i = i + 1
...
Pseudocode (example)
...
// i = 1
@i
M=1
// sum = 0
@sum
M=0
...
// sum = sum + i
@i
D=M
@sum
M=D+M
// i = i + 1
@i
M=M+1
...
Hack assembly
Symbolic programming
• The code writer is allowed to create and
use symbolic variables, as needed
• We assume that there is an agent who
knows how to bind these symbols to
sensible RAM addresses
• This agent is the assembler
For example
• If the assembler will bind i and sum
to, say, 16 and 17, every instruction
@i and @sum will end up selecting
RAM[16] and RAM[17]
• Invisible to the code writer
• The result: a powerful, low-level,
variables abstraction.
write
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 67
Variables
// sum = 0
?
// x = 512
?
// n = n – 1
?
// sum = sum + x
?
A← the constant which is bound to symbol
Typical instructions:
@ symbol
@ constant A←constant
D=0
M=1
D=–1
M=0
...
D=M
A=M
M=D
D=A
...
D=D+A
D=A+1
D=D+M
M=M–1
...
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 68
Variables
// sum = sum + x
@sum
D=M
@x
D=D+M
@sum
M=D
// sum = 0
@sum
M=0
// n = n – 1
@n
M=M–1
// x = 512
@512
D=A
@x
M=D
A← the constant which is bound to symbol
Typical instructions:
@ symbol
@ constant A←constant
D=0
M=1
D=–1
M=0
...
D=M
A=M
M=D
D=A
...
D=D+A
D=A+1
D=D+M
M=M–1
...
Use only the above instructions
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 69
Variables
Example:
// Sets R1 to 2 * R0
// Usage: Enter a value in R0
@R0
D=M
@R1
M=D
M=D+M
symbol value
R0 0
R1 1
R2 2
... ...
R15 15
0 R0
1 R1
2 R2
... ...
15 R15
16
17
...
32767
RAM
The use of R0, R1, … (instead of physical addresses 0, 1, …)
makes Hack code more readable.
Pre-defined symbols in the Hack language
16 “built-in variables” named R0…R15
Sometimes referred to as “virtual registers”
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 70
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 71
Example (pseudocode)
i = 1000
LOOP:
if (i = 0) goto CONT
i = i - 1
goto LOOP
CONT:
...
Labels
Hack assembly
// i = 1000
@1000
D=A
@i
M=D
(LOOP)
// if (i = 0) goto CONT
@i
D=M
@CONT
D;JEQ
// i = i - 1
@i
M=M-1
// gotoLOOP
@LOOP
0;JMP
(CONT)
...
write
Label declaration in the Hack
assembly language:
(sym)
Results in binding sym to the
address of the next instruction
In this example:
LOOP is bound to 4
CONT is bound to 12
(done by the assembler;
The code writer doesn’t care
about these numbers)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 72
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 73
Program example 1: Add
// Sets R2 to R0 + R1 + 17
// D = R0
@R0
D=M
// D = D + R1
@R1
D=D+M
// D = D + 17
@17
D=D+A
// R2 = D
@R2
M=D
Add.asm
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 74
Program example 2: Signum
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Pseudocode
Best practice
When writing a (non-trivial)
assembly program, start by
writing pseudocode;
Then translate the pseudo
instructions into assembly.
Signum.asm
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
write
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 75
Program translation
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
@0
0
Memory
D=M
1
@8
2
D;JGE
3
@1
4
M=–1
5
@10
6
0;JMP
7
@1
8
M=1
9
10
11
12
13
14
...
Assembler /
loader
The assembler
replaces all the
symbols with
physical addresses
(the assembler
generates binary
instructions;
Here we show their
symbolic versions,
for readability)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 76
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
Watch out for loose ends
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
@0
0
Memory
D=M
1
@8
2
D;JGE
3
@1
4
M=–1
5
@10
6
0;JMP
7
@1
8
M=1
9
10
11
12
13
14
...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 77
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
Watch out for loose ends
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
0111111000111110
1010101001011110
0101011100110111
@0
0
Memory
D=M
1
@8
2
D;JGE
3
@1
4
M=–1
5
@10
6
0;JMP
7
@1
8
M=1
9
10
11
12
13
14
...
0100100110011011
1110010011111111
The memory is
never empty
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 78
Watch out for loose ends
Program
execution:
0111111000111110
1010101001011110
0101011100110111
@0
0
Memory
D=M
1
@8
2
D;JGE
3
@1
4
M=–1
5
@10
6
0;JMP
7
@1
8
M=1
9
10
11
12
13
14
...
Malicious
Code
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 79
Watch out for loose ends
Program
execution:
0111111000111110
1010101001011110
0101011100110111
@0
0
Memory
D=M
1
@8
2
D;JGE
3
@1
4
M=–1
5
@10
6
0;JMP
7
@1
8
M=1
9
10
11
12
13
14
...
Malicious
Code
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 80
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
Terminating programs properly
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 81
Terminating programs properly
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
Infinite loop
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 82
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
Terminating programs properly
@10
0;JMP
0101011100110111
0
Memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
0100100110011011
1110010011111111
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
Assembler /
loader
Infinite loop
@0
D=M
@8
D;JGE
@1
M=–1
@10
0;JMP
@1
M=1
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 83
@10
0;JMP
0101011100110111
0
Memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
0100100110011011
1110010011111111
Terminating programs properly
Program
execution:
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
@0
D=M
@8
D;JGE
@1
M=–1
@10
0;JMP
@1
M=1
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 84
@10
0;JMP
0101011100110111
0
Memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
0100100110011011
1110010011111111
Terminating programs properly
Program
execution:
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
@0
D=M
@8
D;JGE
@1
M=–1
@10
0;JMP
@1
M=1
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 85
@10
0;JMP
0101011100110111
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
0100100110011011
1110010011111111
Terminating programs properly
Program
execution:
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
@0
D=M
@8
D;JGE
@1
M=–1
@10
0;JMP
@1
M=1
Memory
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 86
@10
0;JMP
0101011100110111
0
Memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
0100100110011011
1110010011111111
Terminating programs properly
Program
execution:
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
@0
D=M
@8
D;JGE
@1
M=–1
@10
0;JMP
@1
M=1
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 87
Terminating programs properly
Best practice
Terminate every
assembly program with
an infinite loop.
@10
0;JMP
0101011100110111
0
Memory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
0100100110011011
1110010011111111
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Signum.asm
Pseudocode
// if R0>=0 then R1=1
// else R1=–1
// if R0>=0 goto POS
@R0
D=M
@POS
D;JGE
// R1 = –1
@R1
M=–1
// gotoEND
@END
0;JMP
(POS)
// R1 = 1
@R1
M=1
(END)
@END
0;JMP
@0
D=M
@8
D;JGE
@1
M=–1
@10
0;JMP
@1
M=1
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 88
By the way…
// if R0 >= 0 then R1 = 1
// else R1 =–1
if (R0 ≥ 0) goto POS
R1 = –1
goto END
POS:
R1 = 1
END:
Pseudocode
// if R0 >= 0 then R1 = 1
// else R1 =–1
R1 = -1
if (R0 < 0) goto END
R1 = 1
END:
Better:
Best practice
Optimize your pseudocode before
writing it in machine language.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 89
Program example 3: Max
// R2 = max(R0,R1)
// if (R0 > R1) then R2 = R0
// else R2 = R1
...
Pseudocode
• Start by writing the pseudocode
• Write the assembly code in a text file named Max2.asm
• Load Max2.asm into the CPU emulator
• Put some values in R0 and R1
• Run the program, one instruction at a time
• Inspect the result, R2.
//// You do it
Max2.asm
write
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 90
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 91
Iterative processing
Pseudocode
// Program: Sum1ToN (R0 represents N)
// Computes R1 = 1 + 2 + 3 + ... + R0
// Usage: put a value >= 1 in R0
i = 1
sum = 0
LOOP:
if (i > R0) goto STOP
sum = sum + i
i = i + 1
goto LOOP
STOP:
R1 = sum
Example: Compute 1 + 2 + 3 + ... + N
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 92
Iterative processing
Pseudocode Hack assembly (code continues here)
// Program: Sum1ToN (R0 represents N)
// Computes R1 = 1 + 2 + 3 + ... + R0
// Usage: put a value >= 1 in R0
// i = 1
@i
M=1
// sum = 0
@sum
M=0
(LOOP)
// if (i > R0) goto STOP
@i
D=M
@R0
D=D-M
@STOP
D;JGT
// sum = sum + i
@sum
D=M
@i
D=D+M
@sum
M=D
// i = i + 1
@i
M=M+1
// goto LOOP
@LOOP
0;JMP
(STOP)
// R1 = sum
@sum
D=M
@R1
M=D
// infinite loop
(END)
@END
0;JMP
Example: Compute 1 + 2 + 3 + ... + N
// Program: Sum1ToN (R0 represents N)
// Computes R1 = 1 + 2 + 3 + ... + R0
// Usage: put a value >= 1 in R0
i = 1
sum = 0
LOOP:
if (i > R0) goto STOP
sum = sum + i
i = i + 1
goto LOOP
STOP:
R1 = sum
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 93
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 94
Pointers
// Sets RAM[R0] to –1
// Usage: Put some non-negative value in R0
@R0
A=M
M=-1
In Hack, pointer-based access is realized by setting
the address register to the address that we want to
access, using the instruction:
A = …
1015
0 R0
1 R1
2 R2
... ...
15
16
17
...
255
256
...
1012
1013
1014
-1
1015
RAM
desired
result
1016
...
R15
example:
addr = 1015
Example 1: Set the register at address addr to –1
Input: R0 holds addr
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 95
Pointers
Example 2: Get the value of the register at address addr
Input: R0 holds addr
// Gets R1 = RAM[R0]
// Usage: Put some non-negative value in R0
1013
0 R0
75
1 R1
2 R2
... ...
15
16
17
...
255
256
...
512
1012
75
1013
19
1014
-17
256
1015
RAM
desired
result
1016
...
example:
addr = 1013
R15
?
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 96
Pointers
1013
0 R0
75
1 R1
2 R2
... ...
15
16
17
...
255
256
...
512
1012
75
1013
19
1014
-17
256
1015
RAM
desired
result
1016
...
// Gets R1 = RAM[R0]
// Usage: Put some non-negative value in R0
@R0
A=M
D=M
@R1
M=D
Example 2: Get the value of the register at address addr
Input: R0 holds addr
example:
addr = 1013
R15
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 97
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
16
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
–1
304
305
...
RAM
desired
output
base
n
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
example:
base = 300
n = 5
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 98
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
16
17
...
255
256
...
300
301
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 99
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
0
16 i
17
...
255
256
...
300
301
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 100
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
0
16 i
17
...
255
256
...
–1
300
301
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 101
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
1
16 i
17
...
255
256
...
–1
300
301
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 102
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
1
16 i
17
...
255
256
...
–1
300
–1
301
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 103
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
2
16 i
17
...
255
256
...
–1
300
–1
301
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 104
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
2
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 105
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
3
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 106
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
3
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 107
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
4
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 108
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
4
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
–1
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 109
Pointers
300
0 R0
5
1 R1
2 R2
... ...
15 R15
5
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
–1
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 110
Pointers
Pseudocode
// Program: PointerDemo.asm
// Starting at the address stored in R0,
// sets the first R1 words to –1
i = 0
LOOP:
if (i == R1) goto END
RAM[R0 + i] = -1
i = i + 1
goto LOOP
END:
300
0 R0
5
1 R1
2 R2
... ...
15 R15
5
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
–1
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 111
Pointers
Assembly code
Pseudocode
// Program: PointerDemo.asm
// Starting at the address stored in R0,
// sets the first R1 words to –1
i = 0
LOOP:
if (i == R1) goto END
RAM[R0 + i] = -1
i = i + 1
goto LOOP
END:
// Program: PointerDemo.asm
// Starting at the address stored in R0,
// sets the first R1 words to –1
// i = 0
@i
M=0
(LOOP)
// if (i == R1) goto END
@i
D=M
@R1
D=D-M
@END
D;JEQ
// RAM[R0 + i] = -1
@R0
D=M
@i
A=D+M
M=-1
// i = i + 1
@i
M=M+1
// goto LOOP
@LOOP
0;JMP
(END)
@END
0;JMP
300
0 R0
5
1 R1
2 R2
... ...
15 R15
5
16 i
17
...
255
256
...
–1
300
–1
301
–1
302
–1
303
–1
304
305
...
RAM
base
n
example:
base = 300
n = 5
Example 3: Set the first n words
of the memory block beginning in
address base to –1
Inputs: R0 (base) and R1 (n)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 112
Sneak preview to compilation: Handling arrays
High-level code (Java example)
...
// Variable declarations
int[] arr = new int[5];
int sum = 0;
...
// Enters some values into the array
// (code omitted)
...
// Sums up the array elements
for (int j = 0; j < 5; j++) {
sum = sum + arr[j];
}
...
0 R0
1 R1
2 R2
... ...
15 R15
5034
16 arr
0
17 sum
...
75
76
...
RAM
255
256
...
100
5034
50
5035
200
5036
2
5037
7
5038
5036
...
Memory state just
before executing the
for loop:
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 113
0 R0
1 R1
2 R2
... ...
15 R15
5034
16 arr
359
17 sum
5
... j
75
76
...
RAM
255
256
...
100
5034
50
5035
200
5036
2
5037
7
5038
5036
...
High-level code (Java example)
...
// Variable declarations
int[] arr = new int[5];
int sum = 0;
...
// Enters some values into the array
// (code omitted)
...
// Sums up the array elements
for (int j = 0; j < 5; j++) {
sum = sum + arr[j];
}
...
Memory state just
after executing the
for loop:
Sneak preview to compilation: Handling arrays
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 114
...
// Variable declarations
int[] arr = new int[5];
int sum = 0;
...
// Enters some values into the array
// (code omitted)
...
// Sums up the array elements
for (int j = 0; j < 5; j++) {
sum = sum + arr[j];
}
...
Hack assembly
Compiler
...
// sum = sum + arr[j]
@arr
D=M
@j
A=D+M
D=M
@sum
M=M+D
...
High-level code (Java example)
0 R0
1 R1
2 R2
... ...
15 R15
5034
16 arr
359
17 sum
5
... j
75
76
...
RAM
255
256
...
100
5034
50
5035
200
5036
2
5037
7
5038
5036
...
Sneak preview to compilation: Handling arrays
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 115
...
// Variable declarations
int[] arr = new int[5];
int sum = 0;
...
// Enters some values into the array
// (code omitted)
...
// Sums up the array elements
for (int j = 0; j < 5; j++) {
sum = sum + arr[j];
}
...
// Increments each array element
for (int j = 0; j < 5; j++) {
arr[j] = arr[j] + 1
}
...
Hack assembly
Compiler
...
// sum = sum + arr[j]
@arr
D=M
@j
A=D+M
D=M
@sum
M=M+D
...
High-level code (Java example)
0 R0
1 R1
2 R2
... ...
15 R15
5034
16 arr
359
17 sum
5
... j
75
76
...
RAM
255
256
...
100
5034
50
5035
200
5036
2
5037
7
5038
5036
...
Sneak preview to compilation: Handling arrays
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 116
...
// sum = sum + arr[j]
@arr
D=M
@j
A=D+M
D=M
@sum
M=M+D
...
// arr[j] = arr[j] + 1
Hack assembly
?
...
// Variable declarations
int[] arr = new int[5];
int sum = 0;
...
// Enters some values into the array
// (code omitted)
...
// Sums up the array elements
for (int j = 0; j < 5; j++) {
sum = sum + arr[j];
}
...
// Increments each array element
for (int j = 0; j < 5; j++) {
arr[j] = arr[j] + 1
}
...
Compiler
High-level code (Java example)
0 R0
1 R1
2 R2
... ...
15 R15
5034
16 arr
359
17 sum
5
... j
75
76
...
RAM
255
256
...
100
5034
50
5035
200
5036
2
5037
7
5038
5036
...
Sneak preview to compilation: Handling arrays
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 117
...
// Variable declarations
int[] arr = new int[5];
int sum = 0;
...
// Enters some values into the array
// (code omitted)
...
// Sums up the array elements
for (int j = 0; j < 5; j++) {
sum = sum + arr[j];
}
...
// Increments each array element
for (int j = 0; j < 5; j++) {
arr[j] = arr[j] + 1
}
...
...
// sum = sum + arr[j]
@arr
D=M
@j
A=D+M
D=M
@sum
M=M+D
...
// arr[j] = arr[j] + 1
@arr
D=M
@j
A=D+M
M=M+1
...
Hack assembly
Every high-level array access arr[expression] in any programming language can
be compiled into Hack code that realizes the access using the low-level idiom
A = arr + expression
Compiler
High-level code (Java example)
0 R0
1 R1
2 R2
... ...
15 R15
5034
16 arr
359
17 sum
5
... j
75
76
...
RAM
255
256
...
100
5034
50
5035
200
5036
2
5037
7
5038
5036
...
Sneak preview to compilation: Handling arrays
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 118
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 119
• A - instruction
• C - instruction
The A-instruction
Instruction set
Syntax:
where xxx is either a constant, or
a symbol bound to a constant
@ xxx
Semantics:
• Sets the A register to the xxx
• Side effects:
RAM[A] becomes the selected RAM location
ROM[A] becomes the selected ROM location
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 120
The C-instruction
Instruction set
• A - instruction
• C - instruction
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 121
The C-instruction
where:
0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A
M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
dest = comp ; jump
Syntax:
null, M, D, DM, A, AM, AD, ADM
dest = M stands for RAM[A]
Semantics
Computes the value of comp and stores the result in dest;
If (comp jump 0), branches to execute ROM[A]
“dest =” and “;jump” are optional
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 122
The C-instruction
Examples:
// Sets the D register to -1
D=-1
where:
0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A
M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, DM, A, AM, AD, ADM
dest =
dest = comp ; jump
Syntax:
M stands for RAM[A]
“dest =” and “;jump” are optional
Semantics
Computes the value of comp and stores the result in dest;
If (comp jump 0), branches to execute ROM[A]
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 123
where:
0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A
M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, DM, A, AM, AD, ADM
dest =
dest = comp ; jump
Syntax:
M stands for RAM[A]
The C-instruction
Examples:
// Sets D and M to the value of the D register, plus 1
DM=D+1
“dest =” and “;jump” are optional
Semantics
Computes the value of comp and stores the result in dest;
If (comp jump 0), branches to execute ROM[A]
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 124
where:
0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A
M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, DM, A, AM, AD, ADM
dest =
dest = comp ; jump
Syntax:
M stands for RAM[A]
The C-instruction
Examples:
// If (D-1 = 0) jumps to execute the instruction stored in ROM[56]
@56
D-1;JEQ
“dest =” and “;jump” are optional
Semantics
Computes the value of comp and stores the result in dest;
If (comp jump 0), branches to execute ROM[A]
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 125
where:
0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A
M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, DM, A, AM, AD, ADM
dest =
dest = comp ; jump
Syntax:
M stands for RAM[A]
The C-instruction
Examples:
// goto LOOP
@LOOP
0;JMP // The 0; prefix is a syntax convention
“dest =” and “;jump” are optional
Semantics
Computes the value of comp and stores the result in dest;
If (comp jump 0), branches to execute ROM[A]
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 126
Recap: A-instructions and C-instructions
// if D=0 goto 100
@100
D;JEQ
To set up for a C-instruction that specifies a jump,
Use an A-instruction to select the target address
// RAM[5] = RAM[5] - 1
@5
M=M-1
They normally come in pairs:
To set up for a C-instruction that operates on M,
Use an A-instruction to select the target address
Observation: It makes no sense that a C-instruction will use the same address to
access the data memory and the instruction memory simultaneously;
Best practice rule
A C-instruction should specify either M, or a jump directive, but not both
Syntax convention: A C-instruction that mentions M should not have
a jump directive, and vice versa
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 127
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 128
Hack machine language specification
Two versions
• Symbolic
• Binary
The binary specification is not intended for writing low-level programs;
It is intended for writing assemblers (chapter 6).
We describe it here, for completeness.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 129
The Hack language specification
A instruction
(vv ... v = 15-bit value of xxx)
(xxx is a decimal value ranging from 0 to 32767,
or a symbol bound to such a decimal value)
Symbolic: @xxx
Binary: 0 vvvvvvvvvvvvvvv
dest = comp; jump
111accccccdddjjj
(comp is mandatory.
If dest is empty, the = is omitted;
If jump is empty, the ; is omitted)
C instruction
Symbolic:
Binary:
0 1 0 1 0 1 0
1 1 1 1 1 1 1
–1 1 1 1 0 1 0
D 0 0 1 1 0 0
A M 1 1 0 0 0 0
!D 0 0 1 1 0 1
!A !M 1 1 0 0 0 1
–D 0 0 1 1 1 1
–A -M 1 1 0 0 1 1
D+1 0 1 1 1 1 1
A+1 M+1 1 1 0 1 1 1
D–1 0 0 1 1 1 0
A–1 M–1 1 1 0 0 1 0
D+A D+M 0 0 0 0 1 0
D–A D–M 0 1 0 0 1 1
A–D M–D 0 0 0 1 1 1
D&A D&M 0 0 0 0 0 0
D|A D|M 0 1 0 1 0 1
null 0 0 0 the value is not stored
M 0 0 1 RAM[A]
D 0 1 0 D register (reg)
DM 0 1 1 RAM[A] and D reg
A 1 0 0 A reg
AM 1 0 1 A reg and RAM[A]
AD 1 1 0 A reg and D reg
ADM 1 1 1 A reg, D reg, and RAM[A]
null 0 0 0 no jump
JGT 0 0 1 if comp > 0 jump
JEQ 0 1 0 if comp = 0 jump
JGE 0 1 1 if comp ≥ 0 jump
JLT 1 0 0 if comp < 0 jump
JNE 1 0 1 if comp ≠ 0 jump
JLE 1 1 0 if comp ≤ 0 jump
JMP 1 1 1 unconditional jump
comp c c c c c c
a == 0 a == 1
dest d d d Effect: store comp in:
jump j j j Effect:
symbol value
R0 0
R1 1
R2 2
... ...
R15 15
SP 0
LCL 1
ARG 2
THIS 3
THAT 4
SCREEN 16384
KBD 24576
Predefined symbols:
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 130
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 131
Input / output
data
RAM
ROM
instruc-
tions
CPU
Keyboard: used
to enter inputs
Screen: used to
display outputs
Low-level I/O handling:
Manipulating bits directly, using memory resident bitmaps.
High-level I/O handling (later in the course):
I/O libraries for handling text, graphics, audio, video, …
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 132
Bitmaps
0
...
15
16
...
255
256
...
RAM
2047
2048
...
16383
screen
memory
map
16384
...
24575
24576
...
32767
Screen memory map:
An 8K memory block, dedicated to representing a black-and-white display unit
Base address: SCREEN = 16384 (predefined symbol)
Output is rendered by writing bits in the screen memory map.
SCREEN =
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 133
Bitmaps
...
...
...
...
...
Screen shots of computer games
developed on the Hack computer
Physical screen
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 134
Bitmaps
...
...
...
...
...
Screen shots of computer games
developed on the Hack computer
Physical screen
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 135
Bitmaps
...
...
...
...
...
Screen shots of computer games
developed on the Hack computer
Physical screen
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 136
Bitmaps
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
Mapping
The (row, col) pixel in the physical screen is represented by
the (col % 16)th bit in RAM address SCREEN + 32*row + col/16
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 137
Bitmaps
To set the (row , col) pixel to black or white:
addr ← SCREEN + 32*row + col/16
word ← RAM[addr]
Set the (col % 16)th bit of word to 0 or 1
RAM[addr] ← word
Not to worry...
Cool Bitmap Editor coming up
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 138
Bitmaps
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
Examples of simple patterns that can be easily drawn:
// Sets the first (left) 16 pixels
// of the top row to black
@SCREEN
M=-1 // –1 = 1111111111111111
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 139
Bitmaps
Examples of simple patterns that can be easily drawn:
// Sets the first (left) 16 pixels
// of the top row to black
@SCREEN
M=-1 // –1 = 1111111111111111
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
// Sets the first 16 pixels
// of row 2 to black
?
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 140
Bitmaps
Examples of simple patterns that can be easily drawn:
// Sets the first (left) 16 pixels
// of the top row to black
@SCREEN
M=-1 // –1 = 1111111111111111
// Sets the first 16 pixels
// of row 2 to black
@64
D=A
@SCREEN
A=A+D
M=-1
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 141
Bitmaps
Examples of simple patterns that can be easily drawn:
// Sets the first (left) 16 pixels
// of the top row to black
@SCREEN
M=-1 // –1 = 1111111111111111
// Sets the first 16 pixels
// of row 2 to black
@64
D=A
@SCREEN
A=A+D
M=-1
// Sets the entire screen
// to black / white
(Project 4)
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 142
Bitmaps
Simple
graphics
program:
Rectangle
Drawing
Rectangle
Drawing
demo
demo
refresh ...
0 1 2 3 4 5 6 7 8 ... 511
0
1
...
...
...
...
255
Physical screen
0000000010101111
0000000000000000
1000000000000000
0
1
. . .
31
0000000001110000
0000000000000000
0000000000000000
32
33
. . .
63
. . .
0000000010101101
0000000000000000
0000000000000000
8159
8160
. . .
8191
Screen Memory Map
row 0
. . .
row 255
row 1
16384
base address
of the screen
memory map
SCREEN
=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 143
Bitmap Editor
0000111111100000 = 4064
0001100000110000 = 6192
0001001010010000 = 4752
...
...
0111111011111100 = 32508
Bitmap editor (web-based tool)
The developer draws a pixeled image on a 2D grid;
The tool generates assembly code that draws the
image in the RAM;
The generated code can be copy-pasted into the
developer’s code.
Note: The editor generates either Jack code or Hack assembly code –
see the radio buttons at the very bottom of the editor’s GUI.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 144
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 145
Input
0
...
15
16
...
255
256
...
RAM
2047
2048
...
16383
screen
16384
...
24575
keyboard
24576
...
32767
Low-level input handling
Read bits.
High-level input handling (later in the course)
readInt, readString, ...
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 146
Input
0
...
15
16
...
255
256
...
RAM
2047
2048
...
16383
16384
...
24575
keyboard
24576
...
32767
Keyboard memory map
A single 16-bit memory location, dedicated to representing the keyboard.
Base address: KBD = 24576 (predefined symbol)
Reading inputs is affected by probing this register.
kbd=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 147
The Hack character set
key code
(space) 32
! 33
“ 34
# 35
$ 36
% 37
& 38
‘ 39
( 40
) 41
* 42
+ 43
, 44
- 45
. 46
/ 47
key code
0 48
1 49
… …
9 57
: 58
; 59
< 60
= 61
> 62
? 63
@ 64
key code
A 65
B 66
C …
… …
Z 90
[ 91
/ 92
] 93
^ 94
_ 95
` 96
key code
a 97
b 98
c 99
… …
z 122
key code
newline 128
backspace 129
left arrow 130
up arrow 131
right arrow 132
down arrow 133
home 134
end 135
Page up 136
Page down 137
insert 138
delete 139
esc 140
f1 141
. . . . . .
f12 152
{ 123
| 124
} 125
~ 126
(Subset of Unicode)
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 148
Memory mapped input
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 149
Memory mapped input
When a key is pressed on the keyboard,
the key’s character code appears in the keyboard memory map.
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
=
k
code('k') = 75
0000000001001011
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 150
Memory mapped input
When a key is pressed on the keyboard,
the key’s character code appears in the keyboard memory map.
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
=
4
code('4') = 52
0000000000110100
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 151
Memory mapped input
When a key is pressed on the keyboard,
the key’s character code appears in the keyboard memory map.
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
= 
code('') = 131
0000000010000011
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 152
Memory mapped input
When a key is pressed on the keyboard,
the key’s character code appears in the keyboard memory map.
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
=
space
code(' ') = 32
0000000000100000
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 153
0000000000000000
Memory mapped input
RAM
When no key is pressed, the resulting code is 0.
24576
base address
of the keyboard
memory map
KBD
=
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 154
Reading inputs
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
=
k
code('k') = 75
0000000001001011
// Set D to the character code of
// the currently pressed key
Examples:
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 155
Reading inputs
0000000000000000
RAM
24576
base address
of the keyboard
memory map
KBD
=
k
code('k') = 75
0000000001001011
// Set D to the character code of
// the currently pressed key
@KBD
D=M
Examples:
// If the currently pressed key is 'q', goto END
@KBD
D=M
@113 // 'q'
D=D-A
@END
D;JEQ
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 156
Symbolic programming
• Control
• Variables
• Labels
Overview
• Machine language
• The Hack computer
• The Hack instruction set
• The Hack CPU Emulator
Machine Language
The Hack Language
• Symbolic
• Binary
• Output
• Input
• Project 4
Programming examples
• Basic
• Iteration
• Pointers
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 157
Project 4
Objectives
Gain a hands-on taste of:
• Low-level programming
• Assembly language
• The Hack computer
Tasks
• Write a simple algebraic program: Mult
• Write a simple interactive program: Fill
• Get creative: Define and write some program of your own (optional).
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 158
code:
Project 4
Mult: a program that computes R2 = R0 * R1
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 159
Mult: a program that computes R2 = R0 * R1
The supplied test script sets
up and executes several
tests of the Mult program.
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 160
Mult: a program that computes R2 = R0 * R1
Multiplication algorithm
• Repetitive addition (simple, inefficient)
• Bitwise multiplication (sophisticated, efficient)
Either approach is fine for this project.
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 161
Fill: a simple interactive program
When the user presses a
keyboard key (any key), the
entire screen becomes black
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 162
Fill: a simple interactive program
The screen remains black as
long as the key is pressed.
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 163
Fill: a simple interactive program
When the user releases the
key, the screen is cleared
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 164
Fill: a simple interactive program
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 165
Fill: a simple interactive program
Etc...
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 166
Fill: a simple interactive program
Etc...
code:
Project 4
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 167
Algorithm
• Execute an infinite loop that listens to the keyboard input
• When a key is pressed (any key),
execute code that writes "black" in every pixel
• When no key is pressed, execute code that writes "white" in every pixel
Tip: This program requires working with pointers.
Fill: a simple interactive program
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 168
Any ideas?
It’s your call!
Task 3: Define and write a program of your own
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 169
Implementation notes
Well-written low-level code is
• Compact
• Efficient
• Elegant
• Self-describing
Tips
• Use symbolic variables and labels
• Use sensible documentation
• Use sensible variable and label names
• Variables: lower-case
• Labels: upper-case
• Use indentation
• Start by writing pseudocode.
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 170
Nand to Tetris Roadmap: Hardware
hardware platform
abstraction
computer abstraction
ALU, RAM abstraction
elementary
logic gates
building a
computer building
chips building
gates
p3
abstraction
machine
language
p2
p1
p4
Writing low-level
programs
This lecture introduced:
• The Hack computer / instruction set
• Low-level programming
• Assembly / assembler
developing
an assembler
Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 171
Nand to Tetris Roadmap: Hardware
hardware platform
abstraction
computer abstraction
ALU, RAM abstraction
elementary
logic gates
building a
computer building
chips building
gates
p3
abstraction
machine
language
p2
p1
p4
Writing low-level
programs
developing
an assembler
Next lecture:
Build the computer
p5

More Related Content

PDF
Machine language/nandtotetris/lecture_3
PDF
Introduction
PDF
Advanced View of Atmega Microcontroller Projects List - Projects Tutorials Co...
PPTX
Microkernels and Beyond
PPTX
ComputerProgrammingCPattPatelChapter.pptx
PDF
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
PDF
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
PPTX
SequenceL intro slideshare
Machine language/nandtotetris/lecture_3
Introduction
Advanced View of Atmega Microcontroller Projects List - Projects Tutorials Co...
Microkernels and Beyond
ComputerProgrammingCPattPatelChapter.pptx
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
SequenceL intro slideshare

Similar to Machine language/nandtotetris/lecture_4 (20)

PPTX
SequenceL Auto-Parallelizing Toolset Intro slideshare
PDF
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
PDF
unit-1 jntuk r23computer programming.pdf
PDF
Computer architecture
PPTX
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
PPT
Open Kode, Airplay And The New Reality Of Write Once Run Anywhere
PPTX
pcDuino Presentation at SparkFun
PPTX
Making a Process (Virtualizing Memory)
PPTX
Pipeline processing and generation of computer.
PPT
GNAT GPL For Mindstorms
PDF
Arduino: Arduino starter kit
PDF
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
DOC
Linux Kernel , BSP, Boot Loader, ARM Engineer - Satish profile
PDF
Indroduction the arduino
PDF
Indroduction arduino
PDF
Research Poster
PDF
Free Python Notes PDF - Python Crash Course
PDF
Machine language
DOCX
Emergency Service Provide by Mobile
PDF
Introduction to Computers
SequenceL Auto-Parallelizing Toolset Intro slideshare
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
unit-1 jntuk r23computer programming.pdf
Computer architecture
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
Open Kode, Airplay And The New Reality Of Write Once Run Anywhere
pcDuino Presentation at SparkFun
Making a Process (Virtualizing Memory)
Pipeline processing and generation of computer.
GNAT GPL For Mindstorms
Arduino: Arduino starter kit
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Linux Kernel , BSP, Boot Loader, ARM Engineer - Satish profile
Indroduction the arduino
Indroduction arduino
Research Poster
Free Python Notes PDF - Python Crash Course
Machine language
Emergency Service Provide by Mobile
Introduction to Computers
Ad

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PPT
Teaching material agriculture food technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development
Big Data Technologies - Introduction.pptx
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
Teaching material agriculture food technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
sap open course for s4hana steps from ECC to s4
Ad

Machine language/nandtotetris/lecture_4

  • 1. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 1 Lecture 4 Machine Language From Nand to Tetris Building a Modern Computer from First Principles These slides support chapter 4 of the book The Elements of Computing Systems By Noam Nisan and Shimon Schocken MIT Press, 2021
  • 2. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 2 Nand to Tetris Roadmap: Hardware abstraction machine language hardware platform abstraction computer abstraction ALU, RAM abstraction elementary logic gates building a computer building chips building gates p3 p2 p1 In previous projects we’ve built the computer’s ALU and RAM developing an assembler
  • 3. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 3 Nand to Tetris Roadmap: Hardware abstraction machine language p4 We will now integrate these modules into a programmable, general-purpose computer hardware platform abstraction computer abstraction ALU, RAM abstraction elementary logic gates building a computer building chips building gates p3 p2 p1 p5 developing an assembler
  • 4. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 4 Nand to Tetris Roadmap: Hardware hardware platform abstraction computer abstraction ALU, RAM abstraction elementary logic gates building a computer building chips building gates p3 abstraction machine language p2 p1 p4 To get started, we’ll treat the computer as an abstraction, and learn how to use it through its interface: the computer’s machine language. developing an assembler
  • 5. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 5 Nand to Tetris Roadmap: Hardware hardware platform abstraction computer abstraction ALU, RAM abstraction elementary logic gates building a computer building chips building gates p3 abstraction machine language p2 p1 developing an assembler We’ll start with a broad overview of: • Computer architectures • Machine languages
  • 6. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 6 Computer systems are flexible and versatile Same hardware can run many different programs (software)
  • 7. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 7 Computer systems are flexible and versatile Ada Lovelace (1843) Early symbolic program Landmark “proof of concept” that a fixed computer can be programmed to perfom different tasks Same hardware can run many different programs (software)
  • 8. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 8 Computer systems are flexible and versatile Alan Turing (1936) Universal Turing Machine Landmark paper, describing a theoretical general-purpose computer Same hardware can run many different programs (software)
  • 9. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 9 Computer systems are flexible and versatile Landmark general-purpose computer ENIAC, University of Pennsylvania Same hardware can run many different programs (software) John Von Neumann (1945)
  • 10. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 10 Computer architecture ALU program Memory CPU registers output input data
  • 11. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 11 Computer architecture ALU Memory CPU registers 0 1 2 ... 1100101010010101 1100100101100111 0011001010101011 ... instructions data output input 0101110011100110 1100000010010001 1110001011111100 ... instruction data value Stored program concept • The computer memory can store programs, just like it stores data • Programs = data. A fundamental idea in the history of computer science
  • 12. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 12 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Lecture plan The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 13. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 13 Machine Language CPU ALU 10 5 137    Registers R1 R2 A Memory 12 615 8828 0 1 2 3 3 -5 ... 955 20 136 137 -523 138 ... Computer (conceptual definition): A processor (CPU) that manipulates a set of registers: • CPU-resident registers (few, accessed directly, by name) • Memory-resident registers (many, accessed by address) Machine language A formalism for accessing and manipulating registers.
  • 14. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 14 Registers Data registers Hold data values Address register Holds an address Instruction register Holds an instruction • All these registers are… registers (containers that hold bits) • The number and bit-width of the registers vary from one computer to another. CPU ALU 10 5 137    Registers R1 R2 A Memory 12 615 8828 0 1 2 3 3 -5 ... 955 20 136 137 -523 138 ...
  • 15. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 15 Typical operations (using, for example, a RISC syntax) // R1  R1 + R2 add R1, R2 // R1  R1 + 73 addi R1, 73 // R1  R2 mov R1, R2 // R1  Memory[137] load R1, 137 // if R1>0 goto 15 jgt R1, 15 CPU ALU 10 5 137    Registers R1 R2 A Memory 12 615 8828 0 1 2 3 3 -5 ... 955 20 136 137 -523 138 ... The syntax of machine languages varies across computers The semantics is the same: Manipulating registers.
  • 16. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 16 Typical operations CPU ALU 000000000010010 0000000000000101 00000010110000    Registers R1 R2 A Memory 1110111100001010 1100000011111110 0110111100001010 0 1 2 1100000011111110 3 0101100011100110 ... 0001100011011100 1100000011111110 136 137 0101110010001110 138 ... current instruction Which instruction should be executed next? • By default, the CPU executes the next instruction • Sometimes we want to “jump” to execute another instruction
  • 17. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 17 Typical operations Branching • Execute an instruction other than the next one • Example: Embarking on a new iteration in a loop ... // Adds 1 to R1, repetitively (LOOP) addi R1,1 ... goto LOOP ... Symbolic version ... // Adds 1 to R1, repetitively addi R1,1 ... goto 13 ... Basic version 13 ... 27 ... Programs with symbolic references are … • Easier to develop • Readable • Relocatable. • Line numbers • Physical addresses • No line numbers • Symbolic addresses
  • 18. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 18 Typical operations Symbolic program Conditional branching Sometimes we want to “jump” to execute an instruction, but only if a certain condition is met // Sets R2 to abs(R1) // R2 ← R1 mov R2,R1 // if (R2 > 0) goto cont jgt R2,CONT // R2 ← –R1 movi R2,0 subR2,R1 CONT: // Here R2 = abs(R1) ...
  • 19. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 19 // Sets R2 to abs(R1) // R2 ← R1 mov R2,R1 // if (R2 > 0) goto cont jgt R2,CONT // R2 ← –R1 movi R2,0 subR2,R1 CONT: // Here R2 = abs(R1) ... Program translation Symbolic program 0101111100111100 1010101010101010 1100000010101010 1011000010000001 ... Binary code translate load and execute Assembler (tool) Assembly (language) Translation Before it can be executed, a symbolic program must be translated into binary instructions that the computer can decode and execute.
  • 20. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 20 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 21. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 21 The Hack computer Hack: a 16-bit computer, featuring two memory units address ROM Instruction memory out RAM address Data memory out Address register Data register A 0 1 2 ... D 0010101010110110 1111100100101011 0011101001011011 1110011001011001 0000110011100111 1000110000110000 0000010011111100 0 1 2 ... 0000011100110010 1001000011110101 0001101001101111 (Conceptual, partial view of the Hack computer architecture) ... 32766 32767 ... 32766 32767
  • 22. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 22 Memory address ROM Instruction memory out RAM address Data memory out Address register Data register A ... 32766 32767 ... 32766 32767 0 1 2 ... D 0010101010110110 1111100100101011 0011101001011011 1110011001011001 0000110011100111 1000110000110000 0000010011111100 0 1 2 ... 0000111100110010 0000011100110010 1001000011110101 0001101001101111 M RAM • Read-write data memory • Addressed by the A register • The selected memory location, RAM[A], is referred to as M (Conceptual, partial view of the Hack computer architecture)
  • 23. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 23 Memory address ROM Instruction memory out RAM address Data memory out Address register Data register A ... 32766 32767 ... 32766 32767 0 1 2 ... 1001111100011001 D 0010101010110110 1111100100101011 0011101001011011 1110011001011001 0000110011100111 1000110000110000 0000010011111100 0 1 2 ... 0000111100110010 0000011100110010 1001000011110101 0001101001101111 M Loaded with a sequence of 16-bit Hack instructions Should we focus on RAM[A], or on ROM[A]? Depends on the current instruction (later) ROM • Read-only instruction memory • Addressed by the (same) A register • The selected memory location, ROM[A], contains the current instruction RAM • Read-write data memory • Addressed by the A register • The selected memory location, RAM[A], is referred to as M (Conceptual, partial view of the Hack computer architecture)
  • 24. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 24 Registers D: data register A: address register M: selected RAM register address ROM Instruction memory out RAM address Data memory out Address register Data register A ... 32766 32767 ... 32766 32767 0 1 2 ... 0 1 2 ... M instruction D (Conceptual, partial view of the Hack computer architecture)
  • 25. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 25 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 26. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 26 Hack instructions Instruction set • A - instruction (address) • C - instruction (compute)
  • 27. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 27 • A - instruction (address) • C - instruction (compute) Hack instructions Instruction set Side effects: • RAM[A] (denoted M) becomes selected • ROM[A] becomes selected Example @ 19 Semantics A ← 19 Syntax: where xxx is a non-negative integer @ xxx
  • 28. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 28 Hack instructions D=0 A=–1 M=1 ... Examples: Syntax: reg = {0|1|–1} where reg = {A |D|M} reg1 = reg2 where reg1 = {A |D |M} reg2 = [–]{A |D|M} D=A D=M M=–M ... where reg, reg1 = {A |D|M} reg2 = {A |D|M |1} op = {+ |– | & | I} reg = reg1 op reg2 D=D+M A=A–1 M=D+1 ... (Complete / formal syntax, later). • A - instruction (address) • C - instruction (compute) Instruction set
  • 29. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 29 Hack instructions Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ... // D ← 2 Examples: The game: We show a subset of Hack instructions (top left), and practice writing code examples that use these instructions. ?
  • 30. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 30 Hack instructions Examples: Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ... // D ← 2 D=1 D=D+1 // D ← 1954 Use only the instructions shown above ? Use only the above instructions
  • 31. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 31 Hack instructions // D ← 1954 @1954 D=A // D ← 2 D=1 D=D+1 Examples: Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ... // D ← D + 23 Use only the instructions shown above ?
  • 32. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 32 Hack instructions // D ← 1954 @1954 D=A Observation • In all these examples, we used both D and A as a data registers: • The addressing side-effects of A are ignored. // D ← 2 D=1 D=D+1 // D ← D + 23 @23 D=D+A Examples: Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ...
  • 33. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 33 Hack instructions // RAM[100] ← 0 @100 M=0 // RAM[100] ← 17 @17 D=A @100 M=D • First pair of instructions: A is used as a data register • Second pair of instructions: A is used as an address register More examples: Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ...
  • 34. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 34 Hack instructions // RAM[100] ← 0 @100 M=0 // RAM[100] ← 17 @17 D=A @100 M=D More examples: // RAM[100] ← RAM[200] ? Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ...
  • 35. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 35 Hack instructions // RAM[100] ← 0 @100 M=0 // RAM[100] ← 17 @17 D=A @100 M=D // RAM[100] ← RAM[200] @200 D=M @100 M=D When we want to operate on a memory location, we use a pair of instructions: • A-instruction: Selects a memory location • C-instruction: Operates on the selected location. More examples: Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ...
  • 36. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 36 Hack instructions // RAM[3] ← RAM[3] – 15 ? Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ... Use only the above instructions
  • 37. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 37 Hack instructions // RAM[3] ← RAM[3] – 15 @15 D=A @3 M=M–D Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ... Use only the above instructions // RAM[3] ← RAM[4] + 1 ?
  • 38. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 38 Hack instructions // RAM[3] ← RAM[3] – 15 @15 D=A @3 M=M–D // RAM[3] ← RAM[4] + 1 @4 D=M+1 @3 M=D Typical instructions: @ constant (A←constant) D=1 D=A D=D+1 ... D=D+A D=M M=0 ... M=D D=D+A M=M–D ... Use only the above instructions
  • 39. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 39 Hack instructions Typical instructions: A=1 D=–1 M=0 ... A=M D=M M=D ... A=D-A D=D+A D=D+M ... @ constant (A←constant) // Computes: RAM[2] = RAM[0] + RAM[1] + 17 Add.asm ? Use only the above instructions
  • 40. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 40 Hack instructions // Computes: RAM[2] = RAM[0] + RAM[1] + 17 // D = RAM[0] @0 D=M // D = D + RAM[1] @1 D=D+M // D = D + 17 @17 D=D+A // RAM[2] = D @2 M=D Add.asm Typical instructions: A=1 D=–1 M=0 ... A=M D=M M=D ... A=D-A D=D+A D=D+M ... @ constant (A←constant) Use only the above instructions
  • 41. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 41 Hack instructions // Computes: RAM[2] = RAM[0] + RAM[1] + 17 // D = RAM[0] @0 D=M // D = D + RAM[1] @1 D=D+M // D = D + 17 @17 D=D+A // RAM[2] = D @2 M=D Add.asm How can we tell that a given program actually works? • Testing / simulating • Formal verification @ constant (A←constant) Typical instructions: A=1 D=–1 M=0 ... A=M D=M M=D ... A=D-A D=D+A D=D+M ... @ constant (A←constant)
  • 42. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 42 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 43. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 43 The CPU emulator • Software that emulates the Hack CPU • Part of the Nand to Tetris IDE code screen load/exec controls
  • 44. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 44 The CPU emulator // Computes: RAM[2] = RAM[0] + RAM[1] + 17 // D = RAM[0] @0 D=M // D = D + RAM[1] @1 D=D+M // D = D + 17 @17 D=D+A // RAM[2] = D @2 M=D Add.asm (example)
  • 45. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 45 The CPU emulator // Computes: RAM[2] = RAM[0] + RAM[1] + 17 // D = RAM[0] @0 D=M // D = D + RAM[1] @1 D=D+M // D = D + 17 @17 D=D+A // RAM[2] = D @2 M=D Add.asm (example) When loading a symbolic program into our CPU emulator, the emulator translates it into binary code (using a built-in assembler). Load into the CPU emulator 0000000000000000 1000010010001101 0000000000000001 1010011001100001 0000000000010001 1001111100110011 0000000000000010 1110010010010011 Binary Execute in the CPU emulator
  • 46. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 46 The CPU emulator CPU emulator demo
  • 47. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 47 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 48. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 48 Loading a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D M ROM instruction instruction instruction instruction instruction instruction instruction instruction instruction Hack program load
  • 49. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 49 Loading a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D instruction instruction instruction instruction instruction instruction instruction instruction M instruction instruction instruction instruction instruction instruction instruction instruction instruction Hack program load Convention: The first instruction is loaded into address 0, the next instruction into address 1, and so on.
  • 50. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 50 Executing a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D instruction instruction instruction instruction instruction instruction instruction instruction M
  • 51. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 51 Executing a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D instruction instruction instruction instruction instruction instruction instruction instruction M
  • 52. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 52 Executing a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D instruction instruction instruction instruction instruction instruction instruction instruction M
  • 53. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 53 Executing a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D instruction instruction instruction instruction instruction instruction instruction instruction M
  • 54. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 54 Executing a program Instruction memory RAM Data memory Address register Data register A 0 1 2 ... 0 1 2 ... D instruction instruction instruction instruction instruction instruction instruction instruction M • The default: Execute the next instruction • Suppose we wish to execute another instruction; • How to specify branching?
  • 55. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 55 Branching Flow of control: 0,1,2,3,4, 7,8,9, 2,3,4, 7,8,9, 2,3,4, ... instruction instruction instruction instruction goto 7 instruction instruction instruction instruction goto 2 instruction ... 0 1 2 3 4 5 6 7 8 9 10 11 Unconditional branching example (pseudocode)
  • 56. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 56 Branching instruction instruction instruction instruction goto 7 instruction instruction instruction instruction goto 2 instruction ... 0 1 2 3 4 5 6 7 8 9 10 11 Unconditional branching example (pseudocode) … // goto 7 @7 0;JMP … In Hack: Syntax: • Use an A-instruction to select an address • Use a C-instruction to jump to that address Semantics of 0;JMP Jump to execute the instruction stored in ROM[A] (the 0; prefix is a syntax convention)
  • 57. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 57 Branching D;JGT // if D > 0 jump Typical branching instructions: to the instruction stored in ROM[A] Pseudocode 0 1 2 3 4 5 6 7 ... instruction instruction if (D>0) goto 6 instruction instruction instruction instruction instruction ... In Hack … // if (D>0) goto 6 @6 D;JGT … Conditional branching example
  • 58. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 58 Branching D;JGT // if D > 0 jump D;JGE // if D ≥ 0 jump D;JLT // if D < 0 jump D;JLE // if D ≤ 0 jump D;JEQ // if D = 0 jump D;JNE // if D ≠ 0 jump 0;JMP // jump Typical branching instructions: 0 1 2 3 4 5 6 7 ... instruction instruction if (D>0) goto 6 instruction instruction instruction instruction instruction ... In Hack to the instruction stored in ROM[A] … // if (D>0) goto 6 @6 D;JGT … D can be replaced with any ALU computation: D+1, D-1, etc. Pseudocode Conditional branching example
  • 59. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 59 Branching D;JGT // if D > 0 jump D;JGE // if D ≥ 0 jump D;JLT // if D < 0 jump D;JLE // if D ≤ 0 jump D;JEQ // if D = 0 jump D;JNE // if D ≠ 0 jump 0;JMP // jump Typical branching instructions: to the instruction stored in ROM[A] Typical instructions: A=1 D=-1 M=0 ... A=M D=A M=D ... D=D-A A=A-1 M=D+1 ... @ constant (A←constant) // if )D = 0( goto 300 ? Use only the above instructions
  • 60. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 60 // if )D = 0( goto 300 @300 D;JEQ Branching D;JGT // if D > 0 jump D;JGE // if D ≥ 0 jump D;JLT // if D < 0 jump D;JLE // if D ≤ 0 jump D;JEQ // if D = 0 jump D;JNE // if D ≠ 0 jump 0;JMP // jump Typical branching instructions: to the instruction stored in ROM[A] Typical instructions: A=1 D=-1 M=0 ... A=M D=A M=D ... D=D-A A=A-1 M=D+1 ... @ constant (A←constant) Use only the above instructions
  • 61. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 61 Branching // if (RAM[3]<100) goto 12 D;JGT // if D > 0 jump D;JGE // if D ≥ 0 jump D;JLT // if D < 0 jump D;JLE // if D ≤ 0 jump D;JEQ // if D = 0 jump D;JNE // if D ≠ 0 jump 0;JMP // jump Typical branching instructions: to the instruction stored in ROM[A] Typical instructions: A=1 D=-1 M=0 ... A=M D=A M=D ... D=D-A A=A-1 M=D+1 ... @ constant (A←constant) ? Use only the above instructions
  • 62. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 62 Branching // if (RAM[3]<100) goto 12 // D = RAM[3] – 100 @3 D=M @100 D=D–A // if (D<0) goto 12 @12 D;JLT D;JGT // if D > 0 jump D;JGE // if D ≥ 0 jump D;JLT // if D < 0 jump D;JLE // if D ≤ 0 jump D;JEQ // if D = 0 jump D;JNE // if D ≠ 0 jump 0;JMP // jump Typical branching instructions: to the instruction stored in ROM[A] Typical instructions: A=1 D=-1 M=0 ... A=M D=A M=D ... D=D-A A=A-1 M=D+1 ... @ constant (A←constant) Use only the above instructions
  • 63. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 63 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 64. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 64 The A-instruction Syntax: where xxx is either a constant, or a symbol bound to a constant @ xxx Examples: @ 19 @ sym Semantics: A ← 19 A ← the number that sym is bound to This idiom can be used for realizing: • Variables • Labels • A - instruction • C - instruction
  • 65. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 65 Variables ... i = 1 sum = 0 ... sum = sum + i i = i + 1 ... Pseudocode (example) ... // i = 1 Hack assembly write
  • 66. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 66 Variables ... i = 1 sum = 0 ... sum = sum + i i = i + 1 ... Pseudocode (example) ... // i = 1 @i M=1 // sum = 0 @sum M=0 ... // sum = sum + i @i D=M @sum M=D+M // i = i + 1 @i M=M+1 ... Hack assembly Symbolic programming • The code writer is allowed to create and use symbolic variables, as needed • We assume that there is an agent who knows how to bind these symbols to sensible RAM addresses • This agent is the assembler For example • If the assembler will bind i and sum to, say, 16 and 17, every instruction @i and @sum will end up selecting RAM[16] and RAM[17] • Invisible to the code writer • The result: a powerful, low-level, variables abstraction. write
  • 67. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 67 Variables // sum = 0 ? // x = 512 ? // n = n – 1 ? // sum = sum + x ? A← the constant which is bound to symbol Typical instructions: @ symbol @ constant A←constant D=0 M=1 D=–1 M=0 ... D=M A=M M=D D=A ... D=D+A D=A+1 D=D+M M=M–1 ... Use only the above instructions
  • 68. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 68 Variables // sum = sum + x @sum D=M @x D=D+M @sum M=D // sum = 0 @sum M=0 // n = n – 1 @n M=M–1 // x = 512 @512 D=A @x M=D A← the constant which is bound to symbol Typical instructions: @ symbol @ constant A←constant D=0 M=1 D=–1 M=0 ... D=M A=M M=D D=A ... D=D+A D=A+1 D=D+M M=M–1 ... Use only the above instructions
  • 69. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 69 Variables Example: // Sets R1 to 2 * R0 // Usage: Enter a value in R0 @R0 D=M @R1 M=D M=D+M symbol value R0 0 R1 1 R2 2 ... ... R15 15 0 R0 1 R1 2 R2 ... ... 15 R15 16 17 ... 32767 RAM The use of R0, R1, … (instead of physical addresses 0, 1, …) makes Hack code more readable. Pre-defined symbols in the Hack language 16 “built-in variables” named R0…R15 Sometimes referred to as “virtual registers”
  • 70. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 70 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 71. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 71 Example (pseudocode) i = 1000 LOOP: if (i = 0) goto CONT i = i - 1 goto LOOP CONT: ... Labels Hack assembly // i = 1000 @1000 D=A @i M=D (LOOP) // if (i = 0) goto CONT @i D=M @CONT D;JEQ // i = i - 1 @i M=M-1 // gotoLOOP @LOOP 0;JMP (CONT) ... write Label declaration in the Hack assembly language: (sym) Results in binding sym to the address of the next instruction In this example: LOOP is bound to 4 CONT is bound to 12 (done by the assembler; The code writer doesn’t care about these numbers)
  • 72. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 72 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 73. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 73 Program example 1: Add // Sets R2 to R0 + R1 + 17 // D = R0 @R0 D=M // D = D + R1 @R1 D=D+M // D = D + 17 @17 D=D+A // R2 = D @R2 M=D Add.asm
  • 74. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 74 Program example 2: Signum // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Pseudocode Best practice When writing a (non-trivial) assembly program, start by writing pseudocode; Then translate the pseudo instructions into assembly. Signum.asm // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) write
  • 75. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 75 Program translation // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode @0 0 Memory D=M 1 @8 2 D;JGE 3 @1 4 M=–1 5 @10 6 0;JMP 7 @1 8 M=1 9 10 11 12 13 14 ... Assembler / loader The assembler replaces all the symbols with physical addresses (the assembler generates binary instructions; Here we show their symbolic versions, for readability)
  • 76. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 76 // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) Watch out for loose ends // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode @0 0 Memory D=M 1 @8 2 D;JGE 3 @1 4 M=–1 5 @10 6 0;JMP 7 @1 8 M=1 9 10 11 12 13 14 ...
  • 77. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 77 // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) Watch out for loose ends // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode 0111111000111110 1010101001011110 0101011100110111 @0 0 Memory D=M 1 @8 2 D;JGE 3 @1 4 M=–1 5 @10 6 0;JMP 7 @1 8 M=1 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 The memory is never empty
  • 78. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 78 Watch out for loose ends Program execution: 0111111000111110 1010101001011110 0101011100110111 @0 0 Memory D=M 1 @8 2 D;JGE 3 @1 4 M=–1 5 @10 6 0;JMP 7 @1 8 M=1 9 10 11 12 13 14 ... Malicious Code // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END)
  • 79. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 79 Watch out for loose ends Program execution: 0111111000111110 1010101001011110 0101011100110111 @0 0 Memory D=M 1 @8 2 D;JGE 3 @1 4 M=–1 5 @10 6 0;JMP 7 @1 8 M=1 9 10 11 12 13 14 ... Malicious Code // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END)
  • 80. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 80 // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) Terminating programs properly // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode
  • 81. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 81 Terminating programs properly // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP Infinite loop
  • 82. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 82 // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP Terminating programs properly @10 0;JMP 0101011100110111 0 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode Assembler / loader Infinite loop @0 D=M @8 D;JGE @1 M=–1 @10 0;JMP @1 M=1
  • 83. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 83 @10 0;JMP 0101011100110111 0 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 Terminating programs properly Program execution: // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP @0 D=M @8 D;JGE @1 M=–1 @10 0;JMP @1 M=1
  • 84. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 84 @10 0;JMP 0101011100110111 0 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 Terminating programs properly Program execution: // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP @0 D=M @8 D;JGE @1 M=–1 @10 0;JMP @1 M=1
  • 85. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 85 @10 0;JMP 0101011100110111 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 Terminating programs properly Program execution: // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP @0 D=M @8 D;JGE @1 M=–1 @10 0;JMP @1 M=1 Memory
  • 86. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 86 @10 0;JMP 0101011100110111 0 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 Terminating programs properly Program execution: // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP @0 D=M @8 D;JGE @1 M=–1 @10 0;JMP @1 M=1
  • 87. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 87 Terminating programs properly Best practice Terminate every assembly program with an infinite loop. @10 0;JMP 0101011100110111 0 Memory 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... 0100100110011011 1110010011111111 // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Signum.asm Pseudocode // if R0>=0 then R1=1 // else R1=–1 // if R0>=0 goto POS @R0 D=M @POS D;JGE // R1 = –1 @R1 M=–1 // gotoEND @END 0;JMP (POS) // R1 = 1 @R1 M=1 (END) @END 0;JMP @0 D=M @8 D;JGE @1 M=–1 @10 0;JMP @1 M=1
  • 88. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 88 By the way… // if R0 >= 0 then R1 = 1 // else R1 =–1 if (R0 ≥ 0) goto POS R1 = –1 goto END POS: R1 = 1 END: Pseudocode // if R0 >= 0 then R1 = 1 // else R1 =–1 R1 = -1 if (R0 < 0) goto END R1 = 1 END: Better: Best practice Optimize your pseudocode before writing it in machine language.
  • 89. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 89 Program example 3: Max // R2 = max(R0,R1) // if (R0 > R1) then R2 = R0 // else R2 = R1 ... Pseudocode • Start by writing the pseudocode • Write the assembly code in a text file named Max2.asm • Load Max2.asm into the CPU emulator • Put some values in R0 and R1 • Run the program, one instruction at a time • Inspect the result, R2. //// You do it Max2.asm write
  • 90. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 90 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 91. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 91 Iterative processing Pseudocode // Program: Sum1ToN (R0 represents N) // Computes R1 = 1 + 2 + 3 + ... + R0 // Usage: put a value >= 1 in R0 i = 1 sum = 0 LOOP: if (i > R0) goto STOP sum = sum + i i = i + 1 goto LOOP STOP: R1 = sum Example: Compute 1 + 2 + 3 + ... + N
  • 92. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 92 Iterative processing Pseudocode Hack assembly (code continues here) // Program: Sum1ToN (R0 represents N) // Computes R1 = 1 + 2 + 3 + ... + R0 // Usage: put a value >= 1 in R0 // i = 1 @i M=1 // sum = 0 @sum M=0 (LOOP) // if (i > R0) goto STOP @i D=M @R0 D=D-M @STOP D;JGT // sum = sum + i @sum D=M @i D=D+M @sum M=D // i = i + 1 @i M=M+1 // goto LOOP @LOOP 0;JMP (STOP) // R1 = sum @sum D=M @R1 M=D // infinite loop (END) @END 0;JMP Example: Compute 1 + 2 + 3 + ... + N // Program: Sum1ToN (R0 represents N) // Computes R1 = 1 + 2 + 3 + ... + R0 // Usage: put a value >= 1 in R0 i = 1 sum = 0 LOOP: if (i > R0) goto STOP sum = sum + i i = i + 1 goto LOOP STOP: R1 = sum
  • 93. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 93 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 94. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 94 Pointers // Sets RAM[R0] to –1 // Usage: Put some non-negative value in R0 @R0 A=M M=-1 In Hack, pointer-based access is realized by setting the address register to the address that we want to access, using the instruction: A = … 1015 0 R0 1 R1 2 R2 ... ... 15 16 17 ... 255 256 ... 1012 1013 1014 -1 1015 RAM desired result 1016 ... R15 example: addr = 1015 Example 1: Set the register at address addr to –1 Input: R0 holds addr
  • 95. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 95 Pointers Example 2: Get the value of the register at address addr Input: R0 holds addr // Gets R1 = RAM[R0] // Usage: Put some non-negative value in R0 1013 0 R0 75 1 R1 2 R2 ... ... 15 16 17 ... 255 256 ... 512 1012 75 1013 19 1014 -17 256 1015 RAM desired result 1016 ... example: addr = 1013 R15 ?
  • 96. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 96 Pointers 1013 0 R0 75 1 R1 2 R2 ... ... 15 16 17 ... 255 256 ... 512 1012 75 1013 19 1014 -17 256 1015 RAM desired result 1016 ... // Gets R1 = RAM[R0] // Usage: Put some non-negative value in R0 @R0 A=M D=M @R1 M=D Example 2: Get the value of the register at address addr Input: R0 holds addr example: addr = 1013 R15
  • 97. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 97 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 16 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 –1 304 305 ... RAM desired output base n Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n) example: base = 300 n = 5
  • 98. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 98 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 16 17 ... 255 256 ... 300 301 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 99. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 99 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 0 16 i 17 ... 255 256 ... 300 301 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 100. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 100 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 0 16 i 17 ... 255 256 ... –1 300 301 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 101. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 101 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 1 16 i 17 ... 255 256 ... –1 300 301 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 102. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 102 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 1 16 i 17 ... 255 256 ... –1 300 –1 301 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 103. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 103 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 2 16 i 17 ... 255 256 ... –1 300 –1 301 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 104. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 104 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 2 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 105. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 105 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 3 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 106. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 106 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 3 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 107. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 107 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 4 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 108. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 108 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 4 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 –1 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 109. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 109 Pointers 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 5 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 –1 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 110. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 110 Pointers Pseudocode // Program: PointerDemo.asm // Starting at the address stored in R0, // sets the first R1 words to –1 i = 0 LOOP: if (i == R1) goto END RAM[R0 + i] = -1 i = i + 1 goto LOOP END: 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 5 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 –1 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 111. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 111 Pointers Assembly code Pseudocode // Program: PointerDemo.asm // Starting at the address stored in R0, // sets the first R1 words to –1 i = 0 LOOP: if (i == R1) goto END RAM[R0 + i] = -1 i = i + 1 goto LOOP END: // Program: PointerDemo.asm // Starting at the address stored in R0, // sets the first R1 words to –1 // i = 0 @i M=0 (LOOP) // if (i == R1) goto END @i D=M @R1 D=D-M @END D;JEQ // RAM[R0 + i] = -1 @R0 D=M @i A=D+M M=-1 // i = i + 1 @i M=M+1 // goto LOOP @LOOP 0;JMP (END) @END 0;JMP 300 0 R0 5 1 R1 2 R2 ... ... 15 R15 5 16 i 17 ... 255 256 ... –1 300 –1 301 –1 302 –1 303 –1 304 305 ... RAM base n example: base = 300 n = 5 Example 3: Set the first n words of the memory block beginning in address base to –1 Inputs: R0 (base) and R1 (n)
  • 112. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 112 Sneak preview to compilation: Handling arrays High-level code (Java example) ... // Variable declarations int[] arr = new int[5]; int sum = 0; ... // Enters some values into the array // (code omitted) ... // Sums up the array elements for (int j = 0; j < 5; j++) { sum = sum + arr[j]; } ... 0 R0 1 R1 2 R2 ... ... 15 R15 5034 16 arr 0 17 sum ... 75 76 ... RAM 255 256 ... 100 5034 50 5035 200 5036 2 5037 7 5038 5036 ... Memory state just before executing the for loop:
  • 113. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 113 0 R0 1 R1 2 R2 ... ... 15 R15 5034 16 arr 359 17 sum 5 ... j 75 76 ... RAM 255 256 ... 100 5034 50 5035 200 5036 2 5037 7 5038 5036 ... High-level code (Java example) ... // Variable declarations int[] arr = new int[5]; int sum = 0; ... // Enters some values into the array // (code omitted) ... // Sums up the array elements for (int j = 0; j < 5; j++) { sum = sum + arr[j]; } ... Memory state just after executing the for loop: Sneak preview to compilation: Handling arrays
  • 114. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 114 ... // Variable declarations int[] arr = new int[5]; int sum = 0; ... // Enters some values into the array // (code omitted) ... // Sums up the array elements for (int j = 0; j < 5; j++) { sum = sum + arr[j]; } ... Hack assembly Compiler ... // sum = sum + arr[j] @arr D=M @j A=D+M D=M @sum M=M+D ... High-level code (Java example) 0 R0 1 R1 2 R2 ... ... 15 R15 5034 16 arr 359 17 sum 5 ... j 75 76 ... RAM 255 256 ... 100 5034 50 5035 200 5036 2 5037 7 5038 5036 ... Sneak preview to compilation: Handling arrays
  • 115. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 115 ... // Variable declarations int[] arr = new int[5]; int sum = 0; ... // Enters some values into the array // (code omitted) ... // Sums up the array elements for (int j = 0; j < 5; j++) { sum = sum + arr[j]; } ... // Increments each array element for (int j = 0; j < 5; j++) { arr[j] = arr[j] + 1 } ... Hack assembly Compiler ... // sum = sum + arr[j] @arr D=M @j A=D+M D=M @sum M=M+D ... High-level code (Java example) 0 R0 1 R1 2 R2 ... ... 15 R15 5034 16 arr 359 17 sum 5 ... j 75 76 ... RAM 255 256 ... 100 5034 50 5035 200 5036 2 5037 7 5038 5036 ... Sneak preview to compilation: Handling arrays
  • 116. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 116 ... // sum = sum + arr[j] @arr D=M @j A=D+M D=M @sum M=M+D ... // arr[j] = arr[j] + 1 Hack assembly ? ... // Variable declarations int[] arr = new int[5]; int sum = 0; ... // Enters some values into the array // (code omitted) ... // Sums up the array elements for (int j = 0; j < 5; j++) { sum = sum + arr[j]; } ... // Increments each array element for (int j = 0; j < 5; j++) { arr[j] = arr[j] + 1 } ... Compiler High-level code (Java example) 0 R0 1 R1 2 R2 ... ... 15 R15 5034 16 arr 359 17 sum 5 ... j 75 76 ... RAM 255 256 ... 100 5034 50 5035 200 5036 2 5037 7 5038 5036 ... Sneak preview to compilation: Handling arrays
  • 117. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 117 ... // Variable declarations int[] arr = new int[5]; int sum = 0; ... // Enters some values into the array // (code omitted) ... // Sums up the array elements for (int j = 0; j < 5; j++) { sum = sum + arr[j]; } ... // Increments each array element for (int j = 0; j < 5; j++) { arr[j] = arr[j] + 1 } ... ... // sum = sum + arr[j] @arr D=M @j A=D+M D=M @sum M=M+D ... // arr[j] = arr[j] + 1 @arr D=M @j A=D+M M=M+1 ... Hack assembly Every high-level array access arr[expression] in any programming language can be compiled into Hack code that realizes the access using the low-level idiom A = arr + expression Compiler High-level code (Java example) 0 R0 1 R1 2 R2 ... ... 15 R15 5034 16 arr 359 17 sum 5 ... j 75 76 ... RAM 255 256 ... 100 5034 50 5035 200 5036 2 5037 7 5038 5036 ... Sneak preview to compilation: Handling arrays
  • 118. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 118 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 119. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 119 • A - instruction • C - instruction The A-instruction Instruction set Syntax: where xxx is either a constant, or a symbol bound to a constant @ xxx Semantics: • Sets the A register to the xxx • Side effects: RAM[A] becomes the selected RAM location ROM[A] becomes the selected ROM location
  • 120. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 120 The C-instruction Instruction set • A - instruction • C - instruction
  • 121. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 121 The C-instruction where: 0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M comp = null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP jump = dest = comp ; jump Syntax: null, M, D, DM, A, AM, AD, ADM dest = M stands for RAM[A] Semantics Computes the value of comp and stores the result in dest; If (comp jump 0), branches to execute ROM[A] “dest =” and “;jump” are optional
  • 122. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 122 The C-instruction Examples: // Sets the D register to -1 D=-1 where: 0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M comp = null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP jump = null, M, D, DM, A, AM, AD, ADM dest = dest = comp ; jump Syntax: M stands for RAM[A] “dest =” and “;jump” are optional Semantics Computes the value of comp and stores the result in dest; If (comp jump 0), branches to execute ROM[A]
  • 123. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 123 where: 0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M comp = null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP jump = null, M, D, DM, A, AM, AD, ADM dest = dest = comp ; jump Syntax: M stands for RAM[A] The C-instruction Examples: // Sets D and M to the value of the D register, plus 1 DM=D+1 “dest =” and “;jump” are optional Semantics Computes the value of comp and stores the result in dest; If (comp jump 0), branches to execute ROM[A]
  • 124. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 124 where: 0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M comp = null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP jump = null, M, D, DM, A, AM, AD, ADM dest = dest = comp ; jump Syntax: M stands for RAM[A] The C-instruction Examples: // If (D-1 = 0) jumps to execute the instruction stored in ROM[56] @56 D-1;JEQ “dest =” and “;jump” are optional Semantics Computes the value of comp and stores the result in dest; If (comp jump 0), branches to execute ROM[A]
  • 125. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 125 where: 0, 1, –1, D, A, !D, !A, –D, –A, D+1, A+1, D–1, A–1, D+A, D–A, A–D, D&A, D|A M, !M, –M, M+1, M–1, D+M, D–M, M–D, D&M, D|M comp = null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP jump = null, M, D, DM, A, AM, AD, ADM dest = dest = comp ; jump Syntax: M stands for RAM[A] The C-instruction Examples: // goto LOOP @LOOP 0;JMP // The 0; prefix is a syntax convention “dest =” and “;jump” are optional Semantics Computes the value of comp and stores the result in dest; If (comp jump 0), branches to execute ROM[A]
  • 126. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 126 Recap: A-instructions and C-instructions // if D=0 goto 100 @100 D;JEQ To set up for a C-instruction that specifies a jump, Use an A-instruction to select the target address // RAM[5] = RAM[5] - 1 @5 M=M-1 They normally come in pairs: To set up for a C-instruction that operates on M, Use an A-instruction to select the target address Observation: It makes no sense that a C-instruction will use the same address to access the data memory and the instruction memory simultaneously; Best practice rule A C-instruction should specify either M, or a jump directive, but not both Syntax convention: A C-instruction that mentions M should not have a jump directive, and vice versa
  • 127. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 127 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 128. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 128 Hack machine language specification Two versions • Symbolic • Binary The binary specification is not intended for writing low-level programs; It is intended for writing assemblers (chapter 6). We describe it here, for completeness.
  • 129. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 129 The Hack language specification A instruction (vv ... v = 15-bit value of xxx) (xxx is a decimal value ranging from 0 to 32767, or a symbol bound to such a decimal value) Symbolic: @xxx Binary: 0 vvvvvvvvvvvvvvv dest = comp; jump 111accccccdddjjj (comp is mandatory. If dest is empty, the = is omitted; If jump is empty, the ; is omitted) C instruction Symbolic: Binary: 0 1 0 1 0 1 0 1 1 1 1 1 1 1 –1 1 1 1 0 1 0 D 0 0 1 1 0 0 A M 1 1 0 0 0 0 !D 0 0 1 1 0 1 !A !M 1 1 0 0 0 1 –D 0 0 1 1 1 1 –A -M 1 1 0 0 1 1 D+1 0 1 1 1 1 1 A+1 M+1 1 1 0 1 1 1 D–1 0 0 1 1 1 0 A–1 M–1 1 1 0 0 1 0 D+A D+M 0 0 0 0 1 0 D–A D–M 0 1 0 0 1 1 A–D M–D 0 0 0 1 1 1 D&A D&M 0 0 0 0 0 0 D|A D|M 0 1 0 1 0 1 null 0 0 0 the value is not stored M 0 0 1 RAM[A] D 0 1 0 D register (reg) DM 0 1 1 RAM[A] and D reg A 1 0 0 A reg AM 1 0 1 A reg and RAM[A] AD 1 1 0 A reg and D reg ADM 1 1 1 A reg, D reg, and RAM[A] null 0 0 0 no jump JGT 0 0 1 if comp > 0 jump JEQ 0 1 0 if comp = 0 jump JGE 0 1 1 if comp ≥ 0 jump JLT 1 0 0 if comp < 0 jump JNE 1 0 1 if comp ≠ 0 jump JLE 1 1 0 if comp ≤ 0 jump JMP 1 1 1 unconditional jump comp c c c c c c a == 0 a == 1 dest d d d Effect: store comp in: jump j j j Effect: symbol value R0 0 R1 1 R2 2 ... ... R15 15 SP 0 LCL 1 ARG 2 THIS 3 THAT 4 SCREEN 16384 KBD 24576 Predefined symbols:
  • 130. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 130 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 131. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 131 Input / output data RAM ROM instruc- tions CPU Keyboard: used to enter inputs Screen: used to display outputs Low-level I/O handling: Manipulating bits directly, using memory resident bitmaps. High-level I/O handling (later in the course): I/O libraries for handling text, graphics, audio, video, …
  • 132. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 132 Bitmaps 0 ... 15 16 ... 255 256 ... RAM 2047 2048 ... 16383 screen memory map 16384 ... 24575 24576 ... 32767 Screen memory map: An 8K memory block, dedicated to representing a black-and-white display unit Base address: SCREEN = 16384 (predefined symbol) Output is rendered by writing bits in the screen memory map. SCREEN =
  • 133. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 133 Bitmaps ... ... ... ... ... Screen shots of computer games developed on the Hack computer Physical screen
  • 134. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 134 Bitmaps ... ... ... ... ... Screen shots of computer games developed on the Hack computer Physical screen
  • 135. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 135 Bitmaps ... ... ... ... ... Screen shots of computer games developed on the Hack computer Physical screen
  • 136. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 136 Bitmaps refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN = Mapping The (row, col) pixel in the physical screen is represented by the (col % 16)th bit in RAM address SCREEN + 32*row + col/16
  • 137. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 137 Bitmaps To set the (row , col) pixel to black or white: addr ← SCREEN + 32*row + col/16 word ← RAM[addr] Set the (col % 16)th bit of word to 0 or 1 RAM[addr] ← word Not to worry... Cool Bitmap Editor coming up refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN =
  • 138. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 138 Bitmaps refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN = Examples of simple patterns that can be easily drawn: // Sets the first (left) 16 pixels // of the top row to black @SCREEN M=-1 // –1 = 1111111111111111
  • 139. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 139 Bitmaps Examples of simple patterns that can be easily drawn: // Sets the first (left) 16 pixels // of the top row to black @SCREEN M=-1 // –1 = 1111111111111111 refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN = // Sets the first 16 pixels // of row 2 to black ?
  • 140. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 140 Bitmaps Examples of simple patterns that can be easily drawn: // Sets the first (left) 16 pixels // of the top row to black @SCREEN M=-1 // –1 = 1111111111111111 // Sets the first 16 pixels // of row 2 to black @64 D=A @SCREEN A=A+D M=-1 refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN =
  • 141. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 141 Bitmaps Examples of simple patterns that can be easily drawn: // Sets the first (left) 16 pixels // of the top row to black @SCREEN M=-1 // –1 = 1111111111111111 // Sets the first 16 pixels // of row 2 to black @64 D=A @SCREEN A=A+D M=-1 // Sets the entire screen // to black / white (Project 4) refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN =
  • 142. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 142 Bitmaps Simple graphics program: Rectangle Drawing Rectangle Drawing demo demo refresh ... 0 1 2 3 4 5 6 7 8 ... 511 0 1 ... ... ... ... 255 Physical screen 0000000010101111 0000000000000000 1000000000000000 0 1 . . . 31 0000000001110000 0000000000000000 0000000000000000 32 33 . . . 63 . . . 0000000010101101 0000000000000000 0000000000000000 8159 8160 . . . 8191 Screen Memory Map row 0 . . . row 255 row 1 16384 base address of the screen memory map SCREEN =
  • 143. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 143 Bitmap Editor 0000111111100000 = 4064 0001100000110000 = 6192 0001001010010000 = 4752 ... ... 0111111011111100 = 32508 Bitmap editor (web-based tool) The developer draws a pixeled image on a 2D grid; The tool generates assembly code that draws the image in the RAM; The generated code can be copy-pasted into the developer’s code. Note: The editor generates either Jack code or Hack assembly code – see the radio buttons at the very bottom of the editor’s GUI.
  • 144. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 144 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 145. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 145 Input 0 ... 15 16 ... 255 256 ... RAM 2047 2048 ... 16383 screen 16384 ... 24575 keyboard 24576 ... 32767 Low-level input handling Read bits. High-level input handling (later in the course) readInt, readString, ...
  • 146. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 146 Input 0 ... 15 16 ... 255 256 ... RAM 2047 2048 ... 16383 16384 ... 24575 keyboard 24576 ... 32767 Keyboard memory map A single 16-bit memory location, dedicated to representing the keyboard. Base address: KBD = 24576 (predefined symbol) Reading inputs is affected by probing this register. kbd=
  • 147. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 147 The Hack character set key code (space) 32 ! 33 “ 34 # 35 $ 36 % 37 & 38 ‘ 39 ( 40 ) 41 * 42 + 43 , 44 - 45 . 46 / 47 key code 0 48 1 49 … … 9 57 : 58 ; 59 < 60 = 61 > 62 ? 63 @ 64 key code A 65 B 66 C … … … Z 90 [ 91 / 92 ] 93 ^ 94 _ 95 ` 96 key code a 97 b 98 c 99 … … z 122 key code newline 128 backspace 129 left arrow 130 up arrow 131 right arrow 132 down arrow 133 home 134 end 135 Page up 136 Page down 137 insert 138 delete 139 esc 140 f1 141 . . . . . . f12 152 { 123 | 124 } 125 ~ 126 (Subset of Unicode)
  • 148. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 148 Memory mapped input 0000000000000000 RAM 24576 base address of the keyboard memory map KBD =
  • 149. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 149 Memory mapped input When a key is pressed on the keyboard, the key’s character code appears in the keyboard memory map. 0000000000000000 RAM 24576 base address of the keyboard memory map KBD = k code('k') = 75 0000000001001011
  • 150. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 150 Memory mapped input When a key is pressed on the keyboard, the key’s character code appears in the keyboard memory map. 0000000000000000 RAM 24576 base address of the keyboard memory map KBD = 4 code('4') = 52 0000000000110100
  • 151. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 151 Memory mapped input When a key is pressed on the keyboard, the key’s character code appears in the keyboard memory map. 0000000000000000 RAM 24576 base address of the keyboard memory map KBD =  code('') = 131 0000000010000011
  • 152. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 152 Memory mapped input When a key is pressed on the keyboard, the key’s character code appears in the keyboard memory map. 0000000000000000 RAM 24576 base address of the keyboard memory map KBD = space code(' ') = 32 0000000000100000
  • 153. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 153 0000000000000000 Memory mapped input RAM When no key is pressed, the resulting code is 0. 24576 base address of the keyboard memory map KBD =
  • 154. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 154 Reading inputs 0000000000000000 RAM 24576 base address of the keyboard memory map KBD = k code('k') = 75 0000000001001011 // Set D to the character code of // the currently pressed key Examples:
  • 155. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 155 Reading inputs 0000000000000000 RAM 24576 base address of the keyboard memory map KBD = k code('k') = 75 0000000001001011 // Set D to the character code of // the currently pressed key @KBD D=M Examples: // If the currently pressed key is 'q', goto END @KBD D=M @113 // 'q' D=D-A @END D;JEQ
  • 156. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 156 Symbolic programming • Control • Variables • Labels Overview • Machine language • The Hack computer • The Hack instruction set • The Hack CPU Emulator Machine Language The Hack Language • Symbolic • Binary • Output • Input • Project 4 Programming examples • Basic • Iteration • Pointers
  • 157. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 157 Project 4 Objectives Gain a hands-on taste of: • Low-level programming • Assembly language • The Hack computer Tasks • Write a simple algebraic program: Mult • Write a simple interactive program: Fill • Get creative: Define and write some program of your own (optional).
  • 158. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 158 code: Project 4 Mult: a program that computes R2 = R0 * R1
  • 159. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 159 Mult: a program that computes R2 = R0 * R1 The supplied test script sets up and executes several tests of the Mult program. code: Project 4
  • 160. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 160 Mult: a program that computes R2 = R0 * R1 Multiplication algorithm • Repetitive addition (simple, inefficient) • Bitwise multiplication (sophisticated, efficient) Either approach is fine for this project. code: Project 4
  • 161. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 161 Fill: a simple interactive program When the user presses a keyboard key (any key), the entire screen becomes black code: Project 4
  • 162. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 162 Fill: a simple interactive program The screen remains black as long as the key is pressed. code: Project 4
  • 163. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 163 Fill: a simple interactive program When the user releases the key, the screen is cleared code: Project 4
  • 164. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 164 Fill: a simple interactive program code: Project 4
  • 165. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 165 Fill: a simple interactive program Etc... code: Project 4
  • 166. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 166 Fill: a simple interactive program Etc... code: Project 4
  • 167. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 167 Algorithm • Execute an infinite loop that listens to the keyboard input • When a key is pressed (any key), execute code that writes "black" in every pixel • When no key is pressed, execute code that writes "white" in every pixel Tip: This program requires working with pointers. Fill: a simple interactive program
  • 168. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 168 Any ideas? It’s your call! Task 3: Define and write a program of your own
  • 169. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 169 Implementation notes Well-written low-level code is • Compact • Efficient • Elegant • Self-describing Tips • Use symbolic variables and labels • Use sensible documentation • Use sensible variable and label names • Variables: lower-case • Labels: upper-case • Use indentation • Start by writing pseudocode.
  • 170. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 170 Nand to Tetris Roadmap: Hardware hardware platform abstraction computer abstraction ALU, RAM abstraction elementary logic gates building a computer building chips building gates p3 abstraction machine language p2 p1 p4 Writing low-level programs This lecture introduced: • The Hack computer / instruction set • Low-level programming • Assembly / assembler developing an assembler
  • 171. Nand to Tetris / www.nand2tetris.org / Chapter 4 / Copyright © Noam Nisan and Shimon Schocken Slide 171 Nand to Tetris Roadmap: Hardware hardware platform abstraction computer abstraction ALU, RAM abstraction elementary logic gates building a computer building chips building gates p3 abstraction machine language p2 p1 p4 Writing low-level programs developing an assembler Next lecture: Build the computer p5