You are on page 1of 13

Where we are at:

Machine (Assembly) Language Human Abstract design


abstract interface
Software
Thought hierarchy
Chapters 9, 12
H.L. Language Compiler
& abstract interface
Chapters 10 - 11
Operating Sys.
Virtual VM Translator
abstract interface
Machine Chapters 7 - 8

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 Machine language

Abstraction – implementation duality: Abstraction – implementation duality:

 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:

 Binary version: 0001 0001 0010 0011 (machine code)

 Symbolic version ADD R1, R2, R3 (assembly)

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

Abstraction – implementation duality:

 Machine language ( = instruction set) can be viewed as a programmer-


 Machine languages at a glance
oriented abstraction of the hardware platform

 The hardware platform can be viewed as a physical means for realizing  The Hack machine language:
the machine language abstraction

Another duality: ALU  Symbolic version


combinational

 Binary version  Binary version


 Symbolic version
 Perspective
Memory
Loose definition: state
(The assembler will be covered in chapter 6).
 Machine language = an agreed-upon formalism for manipulating
a memory using a processor and a set of registers

 Same spirit but different syntax across different hardware platforms.

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)

 ALU operations // In what follows R1,R2,R3 are registers, PC is program counter,


// and addr is some value.
 Memory access operations
ADD R1,R2,R3 // R1  R2 + R3
(addressing mode: how to specify operands)
ADDI R1,R2,addr // R1  R2 + addr
 Immediate addressing, LDA R1, 67 // R1=67
AND R1,R1,R2 // R1  R1 and R2 (bit-wise)
 Direct addressing, LD R1, 67 // R1=M[67]
JMP addr // PC  addr
 Indirect addressing, LDI R1, R2 // R1=M[R2]
JEQ R1,R2,addr // IF R1 == R2 THEN PC  addr ELSE PC++
 Flow control operations
LOAD R1, addr // R1  RAM[addr]

STORE R1, addr // RAM[addr]  R1

NOP // Do nothing

// Etc. – some 50-300 command variants

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

The Hack computer The Hack computer (CPU)


A 16-bit machine consisting of the following elements: A 16-bit machine consisting of the following elements:

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.

Processing: ALU, capable of computing various functions Example: @21


Program counter: PC, holding an address
Effect:
Control: The ROM is loaded with a sequence of 16-bit instructions, one per memory
location, beginning at address 0. Fetch-execute cycle: later  Sets the A register to 21

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

The A-instruction The C-instruction

@value // A  value dest = comp ; jump

Both dest and jump are optional.

Used for: Coding example: First, we compute something.

 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

dest: null, A, D, M, MD, AM, AD, AMD


 Selecting a ROM location @17 // A = 17
( PC = A ) JMP // fetch the instruction
// stored in ROM[17] Compare to zero. If the
jump: null, JGT, JEQ, JLT, JGE, JNE, JLE, JMP condition holds, jump to
ROM[A]
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 15 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 16
The C-instruction The C-instruction

dest = comp ; jump dest = comp ; jump

 Computes the value of comp 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, D|A
 Stores the result in dest
M, !M, -M, M+1, M-1, D+M, D-M, M-D, D&M, D|M
 If (the condition jump compares to zero is true), goto the instruction at dest: null, A, D, M, MD, AM, AD, AMD
ROM[A].
jump: null, JGT, JEQ, JLT, JGE, JNE, JLE, JMP

Example: set the D register to -1


D = -1

Example: set RAM[300] to the value of the D register minus 1


@300
M = D-1
Example: if ((D-1) == 0) goto ROM[56]
@56
D-1; JEQ
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 17 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 18

Hack programming reference card The Hack machine language

Two ways to express the same semantics:


Hack commands:
Binary code (machine language)
A-command: @value // set A to value
Symbolic language (assembly)
C-command: dest = comp ; jump // dest = and ;jump
// are optional
Where:
symbolic binary
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 , D|A, @17 0000 0000 0001 0001
M , !M , ‐M , M+1, M‐1 , D+M, D‐M, M‐D, D&M, D|M translate
D+1; JLE 1110 0111 1100 0110
dest = M, D, A, MD, AM, AD, AMD, or null
execute
jump = JGT , JEQ , JGE , JLT , JNE , JLE , JMP, or null
In the command dest = comp; jump, the jump materialzes if (comp
jump 0) is true. For example, in D=D+1,JLT, we jump if D+1 < 0. hardware

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

symbolic binary symbolic binary


@value 0value dest = comp ; jump 111A C1C2C3C4 C5C6 D1D2 D3J1J2J3

]
 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

@21 0000 0000 0001 0101

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

The C-instruction The C-instruction


111A C1C2C3C4 C5C6 D1D2 D3J1J2J3 111A C1C2C3C4 C5C6 D1D2 D3J1J2J3

comp dest jump comp dest jump

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

Working with registers and memory Hack programming exercises

 D: data register Exercise: Implement the following tasks


using Hack commands:
 A: address/data register
1. Set D to A-1
 M: the currently selected memory cell, M=RAM[A]
2. Set both A and D to A + 1

3. Set D to 19

4. D++

5. D=RAM[17]

6. Set RAM[5034] to D - 1

7. Set RAM[53] to 171

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)

Exercise: Implement the following tasks 1. D = A-1


using Hack commands:
2. AD=A+1
1. Set D to A-1 3. @19
D=A
2. Set both A and D to A + 1
4. D=D+1
3. Set D to 19 5. @17
D=M
4. D++
6. @5034
5. D=RAM[17] M=D-1
7. @171
6. Set RAM[5034] to D - 1
D=A
7. Set RAM[53] to 171 @53
M=D
8. Add 1 to RAM[7],
and store the result in D. 8. @7
D=M+1
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 29 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 30

Terminate properly Built-in symbols

 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

 R0, R1, …, R15 : virtual registers


 SCREEN and KBD : base address of I/O memory maps
 Others: used in the implementation of the Hack Virtual Machine
 Note that Hack assembler is case-sensitive, R5 != r5

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

Branching Branching with labels


// Program: branch.asm // Program: branch.asm 0 @0
// if R0>0 // if R0>0
// R1=1 // R1=1
1 D=M
// else // else 2 @8
// R1=0 // R1=0 3 D;JGT
4 @1
@R0 @R0 5 M=0
D=M // D=RAM[0] D=M // D=RAM[0] 6 @10
@8 @POSTIVE refer a label 7
0;JMP
D; JGT // If R0>0 goto 8 D; JGT // If R0>0 goto 8 8
@1
9
@R1 @R1 10 M=1
M=0 // R1=0 M=0 // R1=0 @10
@10 @END 11
0; JMP // go to end 0; JMP // go to end 12 0; JMP

@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)

High level: Exercise: Implement the following


Hack:
tasks using Hack commands:
if condition { D  condition
code block 1 @IF_TRUE 1. goto 50
} else { D;JEQ
code block 2 2. if D==0 goto 112
code block 2
}
@END
code block 3 3. if D<9 goto 507
0;JMP

Hack convention: (IF_TRUE) 4. if RAM[12] > 0 goto 50


code block 1
 True is represented by -1 5. if sum>0 goto END
(END)
 False is represented by 0 code block 3
6. if x[i]<=0 goto NEXT.

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

Coding examples (practice) variables


// Program: swap.asm
Exercise: Implement the following // temp = R1
tasks using Hack commands: // R1 = R0
// R0 = temp
1. goto 50 1. @50 5. @sum
0; JMP D=M
2. if D==0 goto 112
2. @112 @END
3. if D<9 goto 507 D; JEQ D: JGT
3. @9 6. @i
4. if RAM[12] > 0 goto 50 D=D-A D=M
@507 @x
if sum>0 goto END
A=D+M
5.
D; JLT
6. if x[i]<=0 goto NEXT. 4. @12 D=M
D=M @NEXT
@50 D; JLE
D; JGT

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

@R0  For this example, temp is arr[3] = -1


assigned with 16.
4.
D=M
@R1
 If the symbol exists, replace it
M=D // R1 = temp 5. arr[j] = 0

with the number recorded in arr[j] = 17


@temp
the table.
6.

D=M
@R0
M=D // R0 = temp

(END)  With symbols and labels, the


@END program is easier to read and
0;JMP debug. Also, it can be relocated.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 41 Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 42

Hack program (exercise) WHILE logic – Hack style


Exercise: Implement the following tasks
using Hack commands: High level: Hack:
1. @sum 4. @arr 6. @j while condition { (LOOP)
sum = 0
M=0 D=M D=M
1.
code block 1 D  condition
2. j = j + 1 2. @j @3 @arr } @END
Code block 2
M=M+1 A=D+A D=D+M D;JNE
3. q = sum + 12 – j
3. @sum M=-1 @ptr code block 1

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

You might also like