CSCE-312 Day 1 Introduction
L15 – HACK Computer Machine Language
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.
instructions
data out
data in
CPU
instruction
memory
data
memory
Hack computer: software
Hack machine language:
16-bit A-instructions
16-bit C-instructions
Hack program = sequence of instructions written in the
Hack machine language
RAM
ROM
instructions
data out
data in
CPU
RAM
ROM
instructions
data out
data in
CPU
Hack computer: control
Control:
The ROM is loaded with a Hack program
The reset button is pushed
The program starts running
reset
RAM
ROM
instructions
data out
data in
CPU
Hack computer: registers
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]
M register
A register
D register
The A-instruction
@value
Syntax:
Where value is either:
a non-negative decimal constant or
a symbol referring to such a constant (later)
Semantics:
Sets the A register to value
Side effects:
RAM[A] becomes the selected RAM register
ROM[A] becomes the selected ROM register
Example:
// Sets A to 17
6
The A-instruction
@value
Syntax:
Where value is either:
a non-negative decimal constant or
a symbol referring to such a constant (later)
Semantics:
Sets the A register to value
Side effects:
RAM[A] becomes the selected RAM register
ROM[A] becomes the selected ROM register
Example:
// Sets A to 17
@17
7
The C-instruction
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
Semantics:
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]
dest = comp ; jump
(both dest and jump are optional)
Syntax:
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
The C-instruction
Example:
Semantics:
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]
// Sets the D register to -1
dest = comp ; jump
(both dest and jump are optional)
Syntax:
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
The C-instruction
Example:
Semantics:
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]
// Sets the D register to -1
D=-1
dest = comp ; jump
(both dest and jump are optional)
Syntax:
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
The C-instruction
Example:
Semantics:
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]
// Sets RAM[300] to the value of the D register plus 1
dest = comp ; jump
(both dest and jump are optional)
Syntax:
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
The C-instruction
Example:
Semantics:
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]
dest = comp ; jump
(both dest and jump are optional)
Syntax:
// Sets RAM[300] to the value of the D register plus 1
@300 // A = 300
M=D+1 // RAM[300] = D + 1
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
The C-instruction
Semantics:
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]
Example:
// If (D-1 == 0) jumps to execute the instruction stored in ROM[56]
dest = comp ; jump
(both dest and jump are optional)
Syntax:
where:
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
comp =
null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
jump =
null, M, D, MD, A, AM, AD, AMD
dest =
(M refers to RAM[A])
The C-instruction
Example:
Semantics:
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]
dest = comp ; jump
(both dest and jump are optional)
Syntax:
// If (D-1 == 0) jumps to execute the instruction stored in ROM[56]
@56 // A = 56
D-1;JEQ // if (D-1 == 0) goto to instruction ROM[A]
RAM
ROM
instructions
data out
data in
CPU
Hack machine language
Hack program
@17
D+1;JLE
Symbolic:
load & execute
translate
0000000000010001
1110011111000110
Binary:
Two ways to express the same semantics:
A-instruction specification
Where value is either:
a non-negative decimal constant ≤ 65535 (=215-1) or
a symbol referring to a constant (later)
@21
Example:
Example:
0000000000010101
Binary syntax:
0value
Where value is a 15-bit binary constant
Semantics: Sets the A register to value
@value
Symbolic syntax:
sets A to 21
sets A to 21
opcode signifying an
A-instruction
16
C-instruction specification
dest = comp ; jump
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
Binary syntax:
C-instruction specification
dest = comp ; jump
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
dest bits
jump bits
comp bits
not used
opcode
Binary syntax:
C-instruction specification
comp c1 c2 c3 c4 c5 c6
0 1 0 1 0 1 0
1 1 1 1 1 1 1
-1 1 1 1 0 1 0
D 0 0 1 1 0 0
A M 1 1 0 0 0 0
!D 0 0 1 1 0 1
!A !M 1 1 0 0 0 1
-D 0 0 1 1 1 1
-A -M 1 1 0 0 1 1
D+1 0 1 1 1 1 1
A+1 M+1 1 1 0 1 1 1
D-1 0 0 1 1 1 0
A-1 M-1 1 1 0 0 1 0
D+A D+M 0 0 0 0 1 0
D-A D-M 0 1 0 0 1 1
A-D M-D 0 0 0 1 1 1
D&A D&M 0 0 0 0 0 0
D|A D|M 0 1 0 1 0 1
a==0 a==1
dest = comp ; jump
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
Binary syntax:
C-instruction specification
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
dest d1 d2 d3 effect: the value is stored in:
null 0 0 0 The value is not stored
M 0 0 1 RAM[A]
D 0 1 0 D register
MD 0 1 1 RAM[A] and D register
A 1 0 0 A register
AM 1 0 1 A register and RAM[A]
AD 1 1 0 A register and D register
AMD 1 1 1 A register, RAM[A], and D register
dest = comp ; jump
Binary syntax:
C-instruction specification
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
jump j1 j2 J3 effect
null 0 0 0 no jump
JGT 0 0 1 if out>0 jump
JEQ 0 1 0 if out=0 jump
JGE 0 1 1 if out≥0 jump
JLT 1 0 0 if out<0 jump
JNE 1 0 1 if out≠0 jump
JLE 1 1 0 if out≤0 jump
JMP 1 1 1 unconditional jump
dest = comp ; jump
Binary syntax:
C-instruction specification
comp c1 c2 c3 c4 c5 c6
0 1 0 1 0 1 0
1 1 1 1 1 1 1
-1 1 1 1 0 1 0
D 0 0 1 1 0 0
A M 1 1 0 0 0 0
!D 0 0 1 1 0 1
!A !M 1 1 0 0 0 1
-D 0 0 1 1 1 1
-A -M 1 1 0 0 1 1
D+1 0 1 1 1 1 1
A+1 M+1 1 1 0 1 1 1
D-1 0 0 1 1 1 0
A-1 M-1 1 1 0 0 1 0
D+A D+M 0 0 0 0 1 0
D-A D-M 0 1 0 0 1 1
A-D M-D 0 0 0 1 1 1
D&A D&M 0 0 0 0 0 0
D|A D|M 0 1 0 1 0 1
a==0 a==1
dest d1 d2 d3 effect: the value is stored in:
null 0 0 0 The value is not stored
M 0 0 1 RAM[A]
D 0 1 0 D register
MD 0 1 1 RAM[A] and D register
A 1 0 0 A register
AM 1 0 1 A register and RAM[A]
AD 1 1 0 A register and D register
AMD 1 1 1 A register, RAM[A], and D register
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
Binary syntax:
jump j1 j2 j3 effect:
null 0 0 0 no jump
JGT 0 0 1 if out > 0 jump
JEQ 0 1 0 if out = 0 jump
JGE 0 1 1 if out ≥ 0 jump
JLT 1 0 0 if out < 0 jump
JNE 1 0 1 if out ≠ 0 jump
JLE 1 1 0 if out ≤ 0 jump
JMP 1 1 1 Unconditional jump
Examples:
MD=D+1
Symbolic:
1110011111011000
Binary:
dest = comp ; jump
C-instruction specification
Examples:
M=1
Symbolic:
1110111111001000
Binary:
comp c1 c2 c3 c4 c5 c6
0 1 0 1 0 1 0
1 1 1 1 1 1 1
-1 1 1 1 0 1 0
D 0 0 1 1 0 0
A M 1 1 0 0 0 0
!D 0 0 1 1 0 1
!A !M 1 1 0 0 0 1
-D 0 0 1 1 1 1
-A -M 1 1 0 0 1 1
D+1 0 1 1 1 1 1
A+1 M+1 1 1 0 1 1 1
D-1 0 0 1 1 1 0
A-1 M-1 1 1 0 0 1 0
D+A D+M 0 0 0 0 1 0
D-A D-M 0 1 0 0 1 1
A-D M-D 0 0 0 1 1 1
D&A D&M 0 0 0 0 0 0
D|A D|M 0 1 0 1 0 1
a==0 a==1
dest d1 d2 d3 effect: the value is stored in:
null 0 0 0 The value is not stored
M 0 0 1 RAM[A]
D 0 1 0 D register
MD 0 1 1 RAM[A] and D register
A 1 0 0 A register
AM 1 0 1 A register and RAM[A]
AD 1 1 0 A register and D register
AMD 1 1 1 A register, RAM[A], and D register
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
Binary syntax:
jump j1 j2 j3 effect:
null 0 0 0 no jump
JGT 0 0 1 if out > 0 jump
JEQ 0 1 0 if out = 0 jump
JGE 0 1 1 if out ≥ 0 jump
JLT 1 0 0 if out < 0 jump
JNE 1 0 1 if out ≠ 0 jump
JLE 1 1 0 if out ≤ 0 jump
JMP 1 1 1 Unconditional jump
dest = comp ; jump
C-instruction specification
Examples:
D+1;JLE
Symbolic:
1110011111000110
Binary:
comp c1 c2 c3 c4 c5 c6
0 1 0 1 0 1 0
1 1 1 1 1 1 1
-1 1 1 1 0 1 0
D 0 0 1 1 0 0
A M 1 1 0 0 0 0
!D 0 0 1 1 0 1
!A !M 1 1 0 0 0 1
-D 0 0 1 1 1 1
-A -M 1 1 0 0 1 1
D+1 0 1 1 1 1 1
A+1 M+1 1 1 0 1 1 1
D-1 0 0 1 1 1 0
A-1 M-1 1 1 0 0 1 0
D+A D+M 0 0 0 0 1 0
D-A D-M 0 1 0 0 1 1
A-D M-D 0 0 0 1 1 1
D&A D&M 0 0 0 0 0 0
D|A D|M 0 1 0 1 0 1
a==0 a==1
dest d1 d2 d3 effect: the value is stored in:
null 0 0 0 The value is not stored
M 0 0 1 RAM[A]
D 0 1 0 D register
MD 0 1 1 RAM[A] and D register
A 1 0 0 A register
AM 1 0 1 A register and RAM[A]
AD 1 1 0 A register and D register
AMD 1 1 1 A register, RAM[A], and D register
jump j1 j2 j3 effect:
null 0 0 0 no jump
JGT 0 0 1 if out > 0 jump
JEQ 0 1 0 if out = 0 jump
JGE 0 1 1 if out ≥ 0 jump
JLT 1 0 0 if out < 0 jump
JNE 1 0 1 if out ≠ 0 jump
JLE 1 1 0 if out ≤ 0 jump
JMP 1 1 1 Unconditional jump
Symbolic syntax:
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
Binary syntax:
dest = comp ; jump
Hack program
// Computes RAM[1] = 1+...+RAM[0]
// Usage: put a number in RAM[0]
@16 // RAM[16] represents i
M=1 // i = 1
@17 // RAM[17] represents sum
M=0 // sum = 0
@16
D=M
@0
D=D-M
@17 // if i>RAM[0] goto 17
D;JGT
@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
Symbolic code
Observations:
Hack program:
a sequence of Hack instructions
White space is permitted
Comments are welcome
There are better ways to write symbolic Hack programs; stay tuned.
No need to understand …
we’ll review the code later in the lecture.
Hack programs: symbolic and binary
0000000000010000
1110111111001000
0000000000010001
1110101010001000
0000000000010000
1111110000010000
0000000000000000
1111010011010000
0000000000010001
1110001100000001
0000000000010000
1111110000010000
0000000000010001
1111000010001000
0000000000010000
1111110111001000
0000000000000100
1110101010000111
0000000000010001
1111110000010000
0000000000000001
1110001100001000
0000000000010101
1110101010000111
Binary code
translate
// Computes RAM[1] = 1+…+RAM[0]
// Usage: put a number in RAM[0]
@16 // RAM[16] represents i
M=1 // i = 1
@17 // RAM[17] represents sum
M=0 // sum = 0
@16
D=M
@0
D=D-M
@17 // if i>RAM[0] goto 17
D;JGT
@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
Symbolic code
execute
Input / output
data
RAM
ROM
instruc-
tions
CPU
Keyboard: used to enter inputs
Screen: used to display outputs
Hello, world
I/O handling (low-level):
Bits manipulation.
I/O handling (high-level):
Software libraries enabling text, graphics, audio, video, etc.
Input: loading selected RAM registers with values
Output: inspecting the state of selected RAM registers
27
data
RAM
ROM
instruc-
tions
CPU
Memory mapped output
screen
memory map
Memory mapped output
A designated memory area, dedicated to manage a display unit
The physical display is continuously refreshed from the memory map,
many times per second
Output is effected by writing code that manipulates the screen memory map.
Hello, world
Memory mapped output
row 0
row 1
row 255
0000000000000000
63
0000000000000000
0000000000000000
8159
8160
0011000000000001
0000101000000000
0000000000000000
31
32
33
1111010100000000
0000000000000000
(16384) 0
1
1011010100000000
8191
Screen
refresh
To set pixel (row,col) on/off:
(1) word = RAM[16384 + 32*row + col/16]
(2) Set the (col % 16)th bit of word to 0 or 1
(3) RAM[ i ] = word
0
1
2
3
4
5
6
7
511
0
1
255
Display Unit (256 by 512, b/w)
Input
data
RAM
ROM
instruc-
tions
CPU
data
RAM
ROM
instruc-
tions
CPU
Input
Kbd mem map
The physical keyboard is associated with a keyboard memory map.
0000000000000000
Keyboard
Memory mapped input
k
Scan-code of ‘k’ = 75
0000000001001011
4
Scan-code of ‘4’ = 52
0000000000110100
space
Scan-code of ‘space’ = 32
0000000000100000
Scan-code of ‘’ = 131
0000000010000011
When a key is pressed on the keyboard, the key’s scan code appears in the keyboard memory map
0000000000000000
Keyboard
Memory mapped input
When no key is pressed, the resulting code is 0.
The Hack character set
key code
(space) 32
! 33
“ 34
# 35
$ 36
% 37
& 38
‘ 39
( 40
) 41
* 42
+ 43
, 44
– 45
. 46
/ 47
key code
0 48
1 49
… …
9 57
: 58
; 59
< 60
= 61
> 62
? 63
@ 64
key code
A 65
B 66
C …
… …
Z 90
[ 91
/ 92
] 93
^ 94
_ 95
` 96
key code
a 97
b 98
c 99
… …
z 122
key code
newline 128
backspace 129
left arrow 130
up arrow 131
right arrow 132
down arrow 133
home 134
end 135
Page up 136
Page down 137
insert 138
delete 139
esc 140
f1 141
… …
f12 152
{ 123
| 124
} 125
~ 126
Handling the keyboard
0000000000000000
Keyboard
k
Scan-code of ‘k’ = 75
0000000001101011
0000000000110100
0000000001001011
To check which key is currently pressed:
Probe the contents of the Keyboard chip
In the Hack computer: probe the contents of RAM[24576].
/docProps/thumbnail.jpeg