Академический Документы
Профессиональный Документы
Культура Документы
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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 3
Machine language
Another duality:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 4
Machine language
Binary version
Symbolic version
Memory
Loose definition: state
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 5
Lecture plan
Symbolic version
Binary version
Perspective
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 6
Typical machine language commands (3 types)
ALU operations
Memory access operations
(addressing mode: how to specify operands)
Immediate addressing, LDA R1, 67 // R1=67
Direct addressing, LD R1, 67 // R1=M[67]
Indirect addressing, LDI R1, R2 // R1=M[R2]
Flow control operations
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 7
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 8
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 9
The Hack computer
A 16-bit machine consisting of the following elements:
reset Screen
Computer
Keyboard
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 10
The Hack computer
A 16-bit machine consisting of the following elements:
inM
writeM
Instruction outM Data
instruction
CPU
Memory Memory
addressM
(ROM32K) (Memory)
pc
reset
Both memory chips are 16-bit wide and have 15-bit address space.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 11
The Hack computer
A 16-bit machine consisting of the following elements:
ALU output
C C
C
D
D
C C
decode
outM
ALU
C
A
Mux
A
C
instruction A/M
Mux
M
inM
C writeM
A
addressM
C
reset
A
PC pc
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 12
The A-instruction
@value // A value
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 13
The C-instruction (first approximation)
dest = 1 Set D to 19
dest = -1
Set both A and D to A + D
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 14
The C-instruction (first approximation)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 15
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
{A, D, M}
x =
y = {A, D, M, 1} arr[j] = 17
dest = {A, D, M, MD, 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 16
The C-instruction (first approximation)
Exercise: Implement the following tasks
using Hack commands:
1. @sum 4. @arr 6. @j
sum = 0
M=0 D=A D=M
1.
2. j = j + 1 2. @j @3 @arr
M=M+1 A=D+A D=A+D
3. q = sum + 12 – j
3. @sum M=-1 @ptr
4. arr[3] = -1 D=M 5. @j M=D
5. arr[j] = 0
@12 D=M @17
D=D+A @arr D=A
6. arr[j] = 17
@j A=A+D @ptr
7. etc. D=D-M M=0 A=M
@q M=D
M=D
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 17
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 18
Coding examples (practice) Hack commands:
goto 50 Where:
comp = 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 ,
if D==0 goto 112
D|A , M , !M , ‐M ,M+1, M‐1 , D+M , D‐M ,
M‐D , D&M , D|M
if D<9 goto 507 dest = M, D, A, MD, AM, AD, AMD, or null
jump = JGT , JEQ , JGE , JLT , JNE , JLE , JMP, or null
if RAM[12] > 0 goto 50
In the command dest = comp; jump, the jump materialzes
if (comp jump 0) is true. For example, in D=D+1,JLT,
if sum>0 goto END we jump if D+1 < 0.
Symbol table:
if x[i]<=0 goto NEXT.
sum 2200
Hack convention:
x 4000
(All symbols and
True is represented by -1
i 6151 values in are
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 19
Coding examples (practice)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 20
IF logic – Hack style
if condition { D condition
code block 1 @IF_TRUE
} else { D;JEQ
code block 2
code block 2
}
@END
code block 3
0;JMP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 21
WHILE logic – Hack style
False is represented by 0
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 22
Side note (focus on the yellow chip parts only)
D register D
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 23
Complete program example
C language code: Hack assembly code:
// Adds 1+...+100. // Adds 1+...+100.
int i = 1; @i // i refers to some RAM location
int 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
D=D-A // D = i - 100
Hack assembly convention: @END
D;JGT // If (i-100) > 0 goto END
Variables: lower-case @i
D=M // D = i
Labels: upper-case @sum
M=D+M // sum += i
Commands: upper-case @i
M=M+1 // i++
@LOOP
0;JMP // Got LOOP
Demo
(END)
CPU emulator
@END
0;JMP // Infinite loop
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 24
// Typical symbolic
Symbols in Hack assembly programs // Hack code, meaning
// not important
Symbols created by Hack programmers and code generators: @R0
D=M
Label symbols: Used to label destinations of goto commands. Declared by
@INFINITE_LOOP
the pseudo command (XXX). This directive defines the symbol XXX to
D;JLE
refer to the instruction memory location holding the next command in
@counter
the program (within the program, XXX is called “label”)
M=D
Variable symbols: Any user-defined symbol xxx appearing in an assembly @SCREEN
program that is not defined elsewhere using the (xxx) directive is D=A
treated as a variable, and is “automatically” assigned a unique RAM @addr
address, starting at RAM address 16 M=D
By convention, Hack programmers use lower-case and upper-case letters (LOOP)
for variable names and labels, respectively. @addr
A=M
Predefined symbols: M=-1
@addr
I/O pointers: The symbols SCREEN and KBD are “automatically” D=M
predefined to refer to RAM addresses 16384 and 24576, respectively @32
(base addresses of the Hack platform’s screen and keyboard memory D=D+A
maps) @addr
Virtual registers: covered in future lectures. M=D
@counter
VM control registers: covered in future lectures.
MD=M-1
@LOOP
Q: Who does all the “automatic” assignments of symbols to RAM addresses? D;JGT
A: The assembler, which is the program that translates symbolic Hack (INFINITE_LOOP)
programs into binary Hack program. As part of the translation process, @INFINITE_LOOP
the symbols are resolved to RAM addresses. (more about this in future lectures) 0;JMP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 25
Perspective
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 26