lecture 04 machine language
Building a Modern Computer From First Principles
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 1
Where we are at:
Human
Thought
Abstract design
Software
hierarchy
abstract interface
Chapters 9, 12
H.L. Language
&
Operating Sys.
Compiler
abstract interface
Chapters 10 - 11
Virtual
Machine
VM Translator
abstract interface
Chapters 7 - 8
Assembly
Language
Assembler
Chapter 6
abstract interface
Machine
Language
Computer
Architecture
abstract interface
Chapters 4 - 5
Hardware
Platform
Hardware
hierarchy
Gate Logic
abstract interface
Chapters 1 - 3
Chips &
Logic Gates
Electrical
Engineering
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
Physics
slide 2
Machine language
!
"
#
#
$
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
%
slide 3
Binary and symbolic notation
1010
1010 0001
0001 0010
0010 1011
1011
ADD
ADD R1,
R1, R2,
R2, R3
R3
&
'
#
#
$
( )
%
*+,*
*+*- *+-.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 4
Lecture plan
/
0
#
"
'
1%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 5
Typical machine language commands (a small sample)
//
// In
In what
what follows
follows R1,R2,R3
R1,R2,R3 are
are registers,
registers, PC
PC is
is program
program counter,
counter,
//
// and
and addr
addr is
is some
some value.
value.
ADD
ADD R1,R2,R3
R1,R2,R3
//
// R1
R1
R2
R2 ++ R3
R3
ADDI
ADDI R1,R2,addr
R1,R2,addr //
// R1
R1
R2
R2 ++ addr
addr
AND
AND R1,R1,R2
R1,R1,R2
//
// R1
R1
R1
R1 and
and R2
R2 (bit-wise)
(bit-wise)
JMP
JMP addr
addr
//
// PC
PC
addr
addr
JEQ
JEQ R1,R2,addr
R1,R2,addr
//
// IF
IF R1
R1 ==
== R2
R2 THEN
THEN PC
PC
LOAD
LOAD R1,
R1, addr
addr
//
// R1
R1
STORE
STORE R1,
R1, addr
addr
//
// RAM[addr]
RAM[addr]
NOP
NOP
//
// Do
Do nothing
nothing
addr
addr ELSE
ELSE PC++
PC++
RAM[addr]
RAM[addr]
R1
R1
//
// Etc.
Etc. –– some
some 50-300
50-300 command
command variants
variants
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 6
The Hack computer
*1
2
3
D4 A, M,
'
ALU,
'
3
)
ROM
(
5
)
RAM
M
RAM[A]
PC,
ROM
4
)
,% 6
$
*1
4
45
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 7
The A-instruction
@value
@value
//
// AA
value
value
%
7
5
8
&
A = value
@17
//
@17
// AA == 17
17
DD == AA //
// DD == 17
17
#
RAM
register = RAM[A]
#
PC
ROM
A
$
@17
//
@17
// AA == 17
17
DD == MM //
// DD == RAM[17]
RAM[17]
@17
@17
JMP
JMP
//
// AA == 17
17
//
fetch
// fetch the
the instruction
instruction
//
stored
in
ROM[17]
// stored in ROM[17]
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 8
The C-instruction (first approximation)
dest
dest == xx ++ yy
dest
dest == xx -- yy
dest
dest == xx
y
3
/ 0
#
dest
dest == 00
#
dest
dest == 11
#
dest
dest == -1
-1
x
&$
=
=
dest
{A4 D4 M}
D
0
A-1
A
D
D
A + 1
D
A + D
19
#
A
#
RAM[5034]
#
RAM[53]
D - 1
{A4 D4 M 4 1}
=
{A4 D4 M4 MD4 A4 AM4 AD4 AMD4 null}
1
171
RAM[7],
D.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 9
The C-instruction (first approximation)
&$
3
/ 0
dest
dest == xx ++ yy
dest
dest == xx -- yy
0
sum = 0
dest
dest == xx
j = j + 1
dest
dest == 00
q = sum + 12 – j
dest
dest == 11
arr[3] = -1
dest
dest == -1
-1
x
y
=
=
dest
{A4 D4 M}
arr[j] = 0
{A4 D4 M 4 1}
arr[j] = 17
=
{A4 D4 M4 MD4 A4 AM4 AD4 AMD4 null}
#
jj
sum
sum
qq
arr
arr
3012
3012
4500
4500
3812
3812
20561
20561
etc.
$
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 10
Control (focus on the yellow chips only)
D register
D
A register
A
Mux
address
input
RAM
ALU
a-bit
A/M
M
(selected
register)
3
/
0
ROM
'
PC
address
input
ROM
(selected
register)
)
Instruction
4
*1
ROM[0]
5
ROM[PC]
A
4
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
ROM4
@n
slide 11
Coding examples (practice)
&$
3
0
/
0
goto 50
// 00
AA
@value
@value
////set
setAAto
tovalue
value
CC
dest
comp; ;jump
jump ////dest
dest== and
and ;jump
;jump
dest==comp
////are
optional
are optional
Where:
Where:
comp
!D, ,!A
!A, ,-D
-D, ,-A
-A, ,D+1
D+1, ,
comp==00, ,11, ,-1-1, ,DD, ,AA, ,!D
A+1
A+1, ,D-1
D-1, ,A-1
A-1, ,D+A
D+A, ,D-A
D-A, ,A-D
A-D, ,D&A
D&A, ,
D|A
D|A, ,MM, ,!M
!M, ,-M
-M,M+1
,M+1, ,M-1
M-1, ,D+M
D+M, ,D-M
D-M, ,
M-D
M-D, ,D&M
D&M, ,D|M
D|M
if D==0 goto 112
if D 0 goto 50
33
::
if sum>0 goto END
::
,,
2;*
2;*==,%,%
9 9::
% % 66
44
$$
::
44
!!
22 2;*4<
2;*4< 4 4
#
if x[i] 4
%
PC
address
input
ROM
(selected
register)
Instruction
5
0 @addr
D=M;someJumpDirective
0
"
/
4
0
C
M4
:
4
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 15
Complete program example
5
/
//// Adds
Adds 1+...+100.
1+...+100.
into
into ii == 1;
1;
into
into sum
sum == 0;
0;
while
while (i
(i 00 goto
@i
@i
D=M
//// DD == ii
D=M
@sum
@sum
M=D+M
sum +=
+= ii
M=D+M //// sum
@i
@i
M=M+1
M=M+1 //// i++
i++
@LOOP
@LOOP
0;JMP
Got LOOP
LOOP
0;JMP //// Got
(END)
(END)
@END
@END
0;JMP
0;JMP //// Infinite
Infinite loop
loop
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 16
//
// Typical
Typical symbolic
symbolic
//
Hack
code,
// Hack code, meaning
meaning
//
not
important
// not important
Symbols in Hack assembly programs
#
/
0
8
%2
XXX
(XXX)%
$
@
4 XXX
?
A
xxx
(xxx)
4
(
4
"
4/
@
A
)
(
*1
0
4
%
'
3B>
SCREEN
(
/
KBD
*1C+D
0
@
.D-E14
0
F
?
A
%
?
%
G 7
@
A
(
4
H
/
/
0
%
(
0
4
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
@R0
@R0
D=M
D=M
@INFINITE_LOOP
@INFINITE_LOOP
D;JLE
D;JLE
@counter
@counter
M=D
M=D
@SCREEN
@SCREEN
D=A
D=A
@addr
@addr
M=D
M=D
(LOOP)
(LOOP)
@addr
@addr
A=M
A=M
M=-1
M=-1
@addr
@addr
D=M
D=M
@32
@32
D=D+A
D=D+A
@addr
@addr
M=D
M=D
@counter
@counter
MD=M-1
MD=M-1
@LOOP
@LOOP
D;JGT
D;JGT
(INFINITE_LOOP)
(INFINITE_LOOP)
@INFINITE_LOOP
@INFINITE_LOOP
0;JMP
0;JMP
slide 17
Perspective
/
0
8
/
(
(
$ D=D+A
0
@I
ADD D,D,A
A
A
4
/
)
C
0
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 18
www.nand2tetris.org
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 1
Where we are at:
Human
Thought
Abstract design
Software
hierarchy
abstract interface
Chapters 9, 12
H.L. Language
&
Operating Sys.
Compiler
abstract interface
Chapters 10 - 11
Virtual
Machine
VM Translator
abstract interface
Chapters 7 - 8
Assembly
Language
Assembler
Chapter 6
abstract interface
Machine
Language
Computer
Architecture
abstract interface
Chapters 4 - 5
Hardware
Platform
Hardware
hierarchy
Gate Logic
abstract interface
Chapters 1 - 3
Chips &
Logic Gates
Electrical
Engineering
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
Physics
slide 2
Machine language
!
"
#
#
$
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
%
slide 3
Binary and symbolic notation
1010
1010 0001
0001 0010
0010 1011
1011
ADD
ADD R1,
R1, R2,
R2, R3
R3
&
'
#
#
$
( )
%
*+,*
*+*- *+-.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 4
Lecture plan
/
0
#
"
'
1%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 5
Typical machine language commands (a small sample)
//
// In
In what
what follows
follows R1,R2,R3
R1,R2,R3 are
are registers,
registers, PC
PC is
is program
program counter,
counter,
//
// and
and addr
addr is
is some
some value.
value.
ADD
ADD R1,R2,R3
R1,R2,R3
//
// R1
R1
R2
R2 ++ R3
R3
ADDI
ADDI R1,R2,addr
R1,R2,addr //
// R1
R1
R2
R2 ++ addr
addr
AND
AND R1,R1,R2
R1,R1,R2
//
// R1
R1
R1
R1 and
and R2
R2 (bit-wise)
(bit-wise)
JMP
JMP addr
addr
//
// PC
PC
addr
addr
JEQ
JEQ R1,R2,addr
R1,R2,addr
//
// IF
IF R1
R1 ==
== R2
R2 THEN
THEN PC
PC
LOAD
LOAD R1,
R1, addr
addr
//
// R1
R1
STORE
STORE R1,
R1, addr
addr
//
// RAM[addr]
RAM[addr]
NOP
NOP
//
// Do
Do nothing
nothing
addr
addr ELSE
ELSE PC++
PC++
RAM[addr]
RAM[addr]
R1
R1
//
// Etc.
Etc. –– some
some 50-300
50-300 command
command variants
variants
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 6
The Hack computer
*1
2
3
D4 A, M,
'
ALU,
'
3
)
ROM
(
5
)
RAM
M
RAM[A]
PC,
ROM
4
)
,% 6
$
*1
4
45
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 7
The A-instruction
@value
@value
//
// AA
value
value
%
7
5
8
&
A = value
@17
//
@17
// AA == 17
17
DD == AA //
// DD == 17
17
#
RAM
register = RAM[A]
#
PC
ROM
A
$
@17
//
@17
// AA == 17
17
DD == MM //
// DD == RAM[17]
RAM[17]
@17
@17
JMP
JMP
//
// AA == 17
17
//
fetch
// fetch the
the instruction
instruction
//
stored
in
ROM[17]
// stored in ROM[17]
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 8
The C-instruction (first approximation)
dest
dest == xx ++ yy
dest
dest == xx -- yy
dest
dest == xx
y
3
/ 0
#
dest
dest == 00
#
dest
dest == 11
#
dest
dest == -1
-1
x
&$
=
=
dest
{A4 D4 M}
D
0
A-1
A
D
D
A + 1
D
A + D
19
#
A
#
RAM[5034]
#
RAM[53]
D - 1
{A4 D4 M 4 1}
=
{A4 D4 M4 MD4 A4 AM4 AD4 AMD4 null}
1
171
RAM[7],
D.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 9
The C-instruction (first approximation)
&$
3
/ 0
dest
dest == xx ++ yy
dest
dest == xx -- yy
0
sum = 0
dest
dest == xx
j = j + 1
dest
dest == 00
q = sum + 12 – j
dest
dest == 11
arr[3] = -1
dest
dest == -1
-1
x
y
=
=
dest
{A4 D4 M}
arr[j] = 0
{A4 D4 M 4 1}
arr[j] = 17
=
{A4 D4 M4 MD4 A4 AM4 AD4 AMD4 null}
#
jj
sum
sum
arr
arr
3012
3012
4500
4500
3812
3812
20561
20561
etc.
$
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 10
Control (focus on the yellow chips only)
D register
D
A register
A
Mux
address
input
RAM
ALU
a-bit
A/M
M
(selected
register)
3
/
0
ROM
'
PC
address
input
ROM
(selected
register)
)
Instruction
4
*1
ROM[0]
5
ROM[PC]
A
4
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
ROM4
@n
slide 11
Coding examples (practice)
&$
3
0
/
0
goto 50
// 00
AA
@value
@value
////set
setAAto
tovalue
value
CC
dest
comp; ;jump
jump ////dest
dest== and
and ;jump
;jump
dest==comp
////are
optional
are optional
Where:
Where:
comp
!D, ,!A
!A, ,-D
-D, ,-A
-A, ,D+1
D+1, ,
comp==00, ,11, ,-1-1, ,DD, ,AA, ,!D
A+1
A+1, ,D-1
D-1, ,A-1
A-1, ,D+A
D+A, ,D-A
D-A, ,A-D
A-D, ,D&A
D&A, ,
D|A
D|A, ,MM, ,!M
!M, ,-M
-M,M+1
,M+1, ,M-1
M-1, ,D+M
D+M, ,D-M
D-M, ,
M-D
M-D, ,D&M
D&M, ,D|M
D|M
if D==0 goto 112
if D 0 goto 50
33
::
if sum>0 goto END
::
,,
2;*
2;*==,%,%
9 9::
% % 66
44
$$
::
44
!!
22 2;*4<
2;*4< 4 4
#
if x[i] 4
%
PC
address
input
ROM
(selected
register)
Instruction
5
0 @addr
D=M;someJumpDirective
0
"
/
4
0
C
M4
:
4
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 15
Complete program example
5
/
//// Adds
Adds 1+...+100.
1+...+100.
into
into ii == 1;
1;
into
into sum
sum == 0;
0;
while
while (i
(i 00 goto
@i
@i
D=M
//// DD == ii
D=M
@sum
@sum
M=D+M
sum +=
+= ii
M=D+M //// sum
@i
@i
M=M+1
M=M+1 //// i++
i++
@LOOP
@LOOP
0;JMP
Got LOOP
LOOP
0;JMP //// Got
(END)
(END)
@END
@END
0;JMP
0;JMP //// Infinite
Infinite loop
loop
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 16
//
// Typical
Typical symbolic
symbolic
//
Hack
code,
// Hack code, meaning
meaning
//
not
important
// not important
Symbols in Hack assembly programs
#
/
0
8
%2
XXX
(XXX)%
$
@
4 XXX
?
A
xxx
(xxx)
4
(
4
"
4/
@
A
)
(
*1
0
4
%
'
3B>
SCREEN
(
/
KBD
*1C+D
0
@
.D-E14
0
F
?
A
%
?
%
G 7
@
A
(
4
H
/
/
0
%
(
0
4
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
@R0
@R0
D=M
D=M
@INFINITE_LOOP
@INFINITE_LOOP
D;JLE
D;JLE
@counter
@counter
M=D
M=D
@SCREEN
@SCREEN
D=A
D=A
@addr
@addr
M=D
M=D
(LOOP)
(LOOP)
@addr
@addr
A=M
A=M
M=-1
M=-1
@addr
@addr
D=M
D=M
@32
@32
D=D+A
D=D+A
@addr
@addr
M=D
M=D
@counter
@counter
MD=M-1
MD=M-1
@LOOP
@LOOP
D;JGT
D;JGT
(INFINITE_LOOP)
(INFINITE_LOOP)
@INFINITE_LOOP
@INFINITE_LOOP
0;JMP
0;JMP
slide 17
Perspective
/
0
8
/
(
(
$ D=D+A
0
@I
ADD D,D,A
A
A
4
/
)
C
0
%
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language
slide 18