Professional Documents
Culture Documents
Assembly
Language
Assembler
Chapter 6
abstract interface
Computer
Building a Modern Computer From First Principles Machine Architecture
abstract interface
Language
Chapters 4 - 5
www.nand2tetris.org 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 1 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 2
Machine language ( = instruction set) can be viewed as a programmer- Machine language ( = instruction set) can be viewed as a programmer-
oriented abstraction of the hardware platform oriented abstraction of the hardware platform
The hardware platform can be viewed as a physical means for realizing The hardware platform can be viewed as a physical means for realizing
the machine language abstraction the machine language abstraction
Another duality:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 3 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 4
Machine language Lecture plan
The hardware platform can be viewed as a physical means for realizing The Hack machine language:
the machine language abstraction
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 5 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 6
Typical machine language commands (3 types) Typical machine language commands (a small sample)
NOP // Do nothing
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 7 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 8
The Hack computer The Hack computer
A 16-bit machine consisting of the following elements: The ROM is loaded with a Hack program
The reset button is pushed
The program starts running
reset Screen
Computer
Keyboard
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 9 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 10
ALU output
inM
C C
C
writeM
D
D
Instruction outM Data
Memory instruction Memory decode
C C
CPU
outM
addressM
ALU
C
(ROM32K) (Memory) A
Mux
pc A
C
instruction A/M
Mux
M
inM
C writeM
A
addressM
C
reset
A
PC 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 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 12
The Hack computer The A-instruction
A 16-bit machine consisting of the following elements: @value // A value
Data memory: RAM – an addressable sequence of registers Where value is either a number or a symbol referring to some number.
Why A-instruction?
Instruction memory: ROM – an addressable sequence of registers In TOY, we store address in the instruction (fmt #2). But, it is impossible
to pack a 15-bit address into a 16-bit instruction. So, we have the A-
Registers: D, A, M, where M stands for RAM[A] instruction for setting addresses if needed.
Instruction set: Two instructions: A-instruction, C-instruction. RAM[21] becomes the selected RAM register M
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 13 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 14
Entering a constant value Next, optionally, we can store the result, or use it to jump to somewhere to
( A = value) @17 // A = 17 continue the program execution.
D = A // D = 17
comp:
Selecting a RAM location @17 // A = 17 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
( register = RAM[A]) D = M // D = RAM[17]
M = -1 // RAM[17]=-1 M, !M, -M, M+1, M-1, D+M, D-M, M-D, D&M, D|M
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 19 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 20
The A-instruction The C-instruction
]
value is a non-negative decimal value is a 15-bit binary number comp dest jump
number <= 215-1 or not used
opcode
A symbol referring to such a
constant
Example
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 21 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 22
A D M
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 23 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 24
The C-instruction Hack assembly/machine language
111A C1C2C3C4 C5C6 D1D2 D3J1J2J3 Source code (example) Target code
// Computes 1+...+RAM[0] 0000000000010000
// And stored the sum in RAM[1]
comp dest jump @i
1110111111001000
0000000000010001
M=1 // i = 1 1110101010001000
@sum 0000000000010000
M=0 // sum = 0 1111110000010000
(LOOP) 0000000000000000
@i // if i>RAM[0] goto WRITE 1111010011010000
D=M 0000000000010010
@R0 1110001100000001
D=D‐M 0000000000010000
@WRITE
D;JGT assemble 1111110000010000
0000000000010001
@i // sum += i 1111000010001000
D=M 0000000000010000
@sum Hack assembler 1111110111001000
M=D+M
@i // i++ or CPU emulator 0000000000000100
1110101010000111
M=M+1 0000000000010001
@LOOP // goto LOOP 1111110000010000
0;JMP 0000000000000001
(WRITE) 1110001100001000
@sum 0000000000010110
D=M 1110101010000111
@R1
M=D // RAM[1] = the sum
(END)
@END We will focus on writing the assembly code.
0;JMP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 25 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 26
3. Set D to 19
4. D++
5. D=RAM[17]
6. Set RAM[5034] to D - 1
8. 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 27 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 28
Hack programming exercises A simple program: add two numbers (demo)
To avoid malicious code, you could terminate your program with an symbol value symbol value
infinite loop, such as R0 0 SP 0
R1 1 LCL 1
R2 2 ARG 2
@6
… … THIS 3
0; JMP R15 15 THAT 4
SCREEN 16384
KBD 24576
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 31 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 32
Branching Branching
// Program: branch.asm // Program: branch.asm
// if R0>0 // if R0>0
// R1=1 // R1=1
// else // else
// R1=0 // R1=0
@R0
D=M // D=RAM[0]
@8
D; JGT // If R0>0 goto 8
@R1
M=0 // R1=0
@10
0; JMP // go to end
@R1
M=1 // R1=1
@10
0; JMP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 33 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 34
@R1
(POSTIVE)
@R1
declare a label 13
M=1 // R1=1 M=1 // R1=1
14
(END) 15
@10 @10 16
0; JMP 0; JMP
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 35 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 36
IF logic – Hack style Coding examples (practice)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 37 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 38
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 39 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 40
variables Hack program (exercise)
// Program: swap.asm Exercise: Implement the following tasks
// temp = R1 When a symbol is encountered, using Hack commands:
// R1 = R0 the assembler looks up a symbol
// R0 = temp
table 1. sum = 0
@R1
If it is a new label, assign a j = j + 1
D=M
number (address of the next
2.
@temp
M=D // temp = R1 available memory cell) to it. 3. q = sum + 12 – j
D=M
@R0
M=D // R0 = temp
D=M 5. @j M=D
4. arr[3] = -1 @LOOP
0;JMP
5. arr[j] = 0
@12 D=M @17 Hack convention:
(END)
D=D+A @arr D=A True is represented by -1 code block 2
6. arr[j] = 17
@j A=D+M @ptr
False is represented by 0
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 43 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 44
Complete program example Complete program example
C language code: Pseudo code: Hack assembly code:
// Adds 1+...+100. i = 1; // Adds 1+...+100.
int i = 1; sum = 0; @i // i refers to some RAM location
int sum = 0; M=1 // i=1
LOOP:
while (i <= 100){ @sum // sum refers to some RAM location
if (i>100) goto END
M=0 // sum=0
sum += i; sum += i; (LOOP)
i++; i++; @i
} goto LOOP D=M // D = i
END: @100
D=D-A // D = i - 100
Hack assembly convention: Hack assembly convention: @END
D;JGT // If (i-100) > 0 goto END
Variables: lower-case Variables: lower-case @i
D=M // D = i
Labels: upper-case Labels: upper-case @sum
M=D+M // sum += i
Commands: upper-case 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 45 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 46
Example Example
// for (i=0; i<n; i++) Pseudo code: // for (i=0; i<n; i++) Pseudo code:
// arr[i] = ‐1; // arr[i] = ‐1;
i = 0
(LOOP)
if (i‐n)>=0 goto END
arr[i] = ‐1
i++
goto LOOP
(END)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 47 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 48
Example Perspective
// for (i=0; i<n; i++) Pseudo code:
Hack is a simple machine language
// arr[i] = ‐1;
@i i = 0
M=0 User friendly syntax: D=D+A instead of ADD D,D,A
(LOOP) (LOOP)
@i if (i‐n)>=0 goto END
D=M arr[i] = ‐1 Hack is a “½-address machine”: any operation that needs to operate on the
@n i++ RAM must be specified using two commands: an A-command to address the
D=D‐M goto LOOP RAM, and a subsequent C-command to operate on it
@END (END)
D; JGE
A Macro-language can be easily developed
@arr
D=M D=D+M[XXX] => @XXX followed by D=D+M
@i GOTO YYY => @YYY followed by 0; JMP
A=D+M
M=‐1
A Hack assembler is needed and will be discusses and developed later in
@i the course.
M=M+1
@LOOP
0; JMP
(END)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 49 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 50