Lecture 04 Machine Language
Lecture 04 Machine Language
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 1
Where we are at:
Assembly
Language
Assembler
Chapter 6
abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 2
Machine language
Another duality:
Binary version
Symbolic version
Loose definition:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 3
Binary and symbolic notation
Evolution:
Physical coding
Symbolic documentation
Symbolic coding
Requires a translator.
Jacquard loom Augusta Ada King,
(1801) Countess of Lovelace
(1815-1852)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 4
Lecture plan
Symbolic version
Binary version
Perspective
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 5
Typical machine language commands (a small sample)
ADD R1,R2,R3 // R1 R2 + R3
NOP // Do nothing
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 6
The Hack computer
A 16-bit machine consisting of the following elements:
Control: The ROM is loaded with a sequence of 16-bit instructions, one per memory
location, beginning at address 0. Fetch-execute cycle: later
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 7
The A-instruction
@value // A value
Later
Selecting a ROM location @17 // A = 17
( PC = A ) JMP // fetch the instruction
// stored in ROM[17]
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 8
The C-instruction (first approximation)
dest = 1 Set D to 19
dest = -1
Set both A and D to A + D
Add 1 to RAM[7],
and store the result in D.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 9
The C-instruction (first approximation)
Exercise: Implement the following tasks
dest = x + y using Hack commands:
dest = x - y sum = 0
dest = x
j = j + 1
dest = 0
q = sum + 12 – j
dest = 1
dest = -1 arr[3] = -1
arr[j] = 0
x = {A, D, M}
y = {A, D, M , 1} arr[j] = 17
dest = {A, D, M, MD, A, AM, AD, AMD, null}
etc.
Symbol table:
j 3012
(All symbols and values
sum 4500 are arbitrary examples)
q 3812
arr 20561
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 10
Control (focus on the yellow chips only)
D register D
a-bit
ALU
A
A register
A/M
Mux
address RAM M
input
(selected
register)
In the Hack architecture:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 11
Coding examples (practice) Hack commands:
Symbol table:
if x[i]<=0 goto NEXT.
sum 2200
Hack convention:
x 4000 (All symbols and
i 6151 values in are
True is represented by -1
END 50 arbitrary examples)
False is represented by 0 NEXT 120
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 12
IF logic – Hack style
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 13
WHILE logic – Hack style
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 14
Side note (focus on the yellow chip parts only)
D
D register
a-bit
ALU
A
A register
A/M
Mux
address RAM M
input
(selected
register)
In the Hack architecture, the A register
addresses both the RAM and the ROM,
simultaneously. Therefore:
Command pairs like @addr followed by
address ROM D=M;someJumpDirective make no sense
input Instruction
PC Best practice: in well-written Hack
(selected
register) programs, a C-instruction should contain
either a reference to M, or
a jump directive,
but not both.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 15
Complete program example
C language code: Hack assembly code:
// Adds 1+...+100. // Adds 1+...+100.
into i = 1; @i // i refers to some RAM location
into sum = 0; M=1 // i=1
while (i <= 100){ @sum // sum refers to some RAM location
M=0 // sum=0
sum += i;
(LOOP)
i++;
@i
} D=M // D = i
@100
Hack assembly convention: D=D-A // D = i - 100
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 18