CL15: HACK Machine Language
1
Rapid Fire Intro to HACK Computer
HARDWARE
A 16-bit machine consisting of:
• Data memory (RAM): a sequence of 16-bit registers:
RAM[0], RAM[1], RAM[2],…
• Instruction memory (ROM): a sequence of 16-bit registers:
ROM[0], ROM[1], ROM[2],…
• Central Processing Unit (CPU): performs 16-bit instructions
• Instruction bus / data bus / address buses.
SOFTWARE
Hack machine language: 16-bit A-instructions 16-bit C-instructions
Hack program = sequence of instructions written in the Hack machine language
ROM
instructions CPU
data out data in
RAM
instruction memory
instructions CPU
data out data in
data memory
2
Hack computer: registers
A register
D register
The Hack machine language recognizes three 16-bit registers:
• D: used to store data
• A: used to store data / address the memory
• M: represents the currently addressed memory register: M = RAM[A]
Control
1 ROM loaded wth HACKprog
Press Reset
1 starts running Prog
ROM
instructions CPU
data out data in
RAM
M register
2
3
The A-instruction
21 p
Syntax:
Where value is either:
a non-negative decimal constant or a symbol referring to such a constant
nonneg const
@ value
Semantics: ud
21
P
11 A 11 A
21
ofP petIf
Esther
Ary
• Sets the A register to value as doth
I
• Side effects:
– RAM[A] becomes the selected RAM register – ROM[A] becomes the selected ROM register
Example:
// Sets A to 17
D
a.tn
T p is a symbol symbol assignedvalue
A113 21
ag i1iiiEioaf
ommdaddIs
or MGB D
Ig
FAasYp iF
0 JMPHjumptoROMED
4
The C-instruction
Syntax: where:
comp = O
dest = comp ; jump
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
O
dest = null, M, D, MD, A, AM, AD, AMD O
(M refers to RAM[A]) jump = null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
dest
O
Semantics: u Inconditional
• Computes the value of comp
• Stores the result in dest
• If the Boolean expression (comp jump 0) is true, jumps to execute the instruction at ROM[A]
or jump EIAM 11
μ
(dest and jump are optional, but need to have one)
M, !M, -M, M+1,
O
M-1, D+M, D-M, M-D, D&M, D|M
f mandatory comp jump
TITI.infobom
dest comp com
D Jat μjumptoROMA ifD 0
D 1 JLT 11jumptoRom A If D KO 42
this avalidflavorofJyj istuisfuiydcjopt.is
yM
Apra
AT DIM
dest_comp
Dt M Aprea
Im 5
faclauggdaga
//Set the D register to -1
//D=RAM[17]
17 DM
//D=10 //D++
//A=3HAH //D=RAM[4]
DI 1011A _10
//RAM[17]=D
//RAM[17]=10
17 MID 11MED
Yes
Yes d If 0 thenjumpto dest = comp ; jump (both dest and jump are optional) ROM Acuvr
Examples
Syntax: where:
D A11D A 10 D Dtl
17 A to 11A 10
D
311A 3
A A 4
jump = dest comp jump
(M refers to RAM[A]) null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
A
D MYD MLB
or j
DA11310 17 11A17
comp = D
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
0008
O
dest = null, M, D, MD, A, AM, AD, AMD
O
O
M, !M, -M,
M+1, M-1, D+M, D-M, M-D, D&M, D|M
M DHMID
0 10 6
Examples
Syntax: where:
dest = comp ; jump
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
//RAM[5]=RAM[3]
comp =
dest = null, M, D, MD, A, AM, AD, AMD
jump =
(M refers to RAM[A]) null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
//Set RAM[300] to the value of the D register plus 2
2 Dt 300
O
E mn s masDOIFI2aEmEofa EnIE’ii EE.hn 5A
SDMMCsJ D MfsJ
//Set RAM[300] to the value of the D register plus 1
3Oo A 3o0
M Dtl M3003 D11
// If (D-1==0) jumps to execute the instruction stored in
ROM[56]
to ROMEO Jump
ump
7
(both dest and jump are optional) OO
M, !M, -M, M+1, M-1, D+M, D-M, M-D, D&M, D|M
deft op jyamp
obhbouffotboth
jump toROM56 if
DI
Preview – A and C Instruction Formats
A Instruction
@value
o.o
ro En 0
outcomes
Itcomes 8
C Instruction 00
72
C-instruction specification
Symbolic syntax: dest = comp ; jump
Binary syntax: p
destination
8possible 2
8possible jumpflavors
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
opcode
27 128possibleops
dest d1d2d3 effect:thevalueisstoredin:
comp
c1
c2c3c4c5c6
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
101010 111111 111010 001100 110000 001101 110001 001111 110011 011111 110111 001110 110010 000010 010011 000111 000000 010101
a==0
a==1
null M D MD A AM AD AMD
000 001 010 011 100 101 110 111
The value is not stored
RAM[A]
D register
RAM[A] and D register
A register
A register and RAM[A]
A register and D register
A register, RAM[A], and D register
jump
j1
j2
j3
effect:
null JGT JEQ JGE JLT JNE JLE JMP
000 001 010 011 100 101 110 111
no jump
if out > 0 jump if out = 0 jump if out ≥ 0 jump if out < 0 jump if out ≠ 0 jump if out ≤ 0 jump Unconditional jump
Symbolic: Binary:
Examples: MD=D+1 1110011111011000
Hack programs: symbolic and binary
Symbolic code
// Computes RAM[1] = 1+...+RAM[0] // Usage: put a number in RAM[0] @16 // RAM[16] represents i M=1 // i = 1
commentlinesBinary code
@17 // RAM[17] M=0 //sum=0
represents sum
discarded
Emptylines discarded
onamisimInabed
translate
Assembler
0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010001 1110001100000001 0000000000010000 1111110000010000 0000000000010001 1111000010001000 0000000000010000 1111110111001000 0000000000000100 1110101010000111 0000000000010001 1111110000010000 0000000000000001 1110001100001000 0000000000010101 1110101010000111
G
@16 D=M @0 D=D-M @17 D;JGT
// if i>RAM[0] goto 17
@16
D=M
@17
M=D+M // sum += i @16
M=M+1 // i++
@4 // goto 4 (loop)
0;JMP
@17
D=M
@1
M=D // RAM[1] = sum @21 // program’s end 0;JMP // infinite loop
y
translates KALLk and
d Vall A
C
instructions
execute
10
2 or
Input / output
ROM
instruc- tions
data CPU
Screen: used to display outputs
ME
RAM
e
Memory Mapped
910 MM
Keyboard: used to enter inputs
Input: Display
@24576
ROM
RAM
location
instruc-
tions CPU
data
Keyboard
KBRD 24576 mappedwytoboard
RAM 24576
is mappedto
egg
eg.ge
keyboard
0O0000000l0
when no keyis pressed resultingcodeis maycorrespondto
To check which key is pressed a key
probecontents ofMemlocationassignedto keyboard RAM 24576
Output: Display
@16384 @24575 256×512 = 8192 16-bit words
ROM
CPU
RscAreMen memory map
instruc- tions
data
Display
grow
Ifspvsi dfd.IE
24575
TamD.spiayog
RAM
DisplayOrg