Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
www.idc.ac.il/tecs
Machine Language
Usage and Copyright Notice:
Copyright 2005 Noam Nisan and Shimon Schocken
This presentation contains lecture materials that accompany the textbook The Elements of
Computing Systems by Noam Nisan & Shimon Schocken, MIT Press, 2005.
We provide both PPT and PDF versions.
The book web site, www.idc.ac.il/tecs , features 13 such presentations, one for each book
chapter. Each presentation is designed to support about 3 hours of classroom or self-study
instruction.
You are welcome to use or edit this presentation as you see fit for instructional and noncommercial purposes.
If you use our materials, we will appreciate it if you will include in them a reference to the books
web site.
If you have any questions or comments, you can reach us at tecs.ta@gmail.com
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 1
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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
Electrical
Engineering
Physics
slide 2
slide 3
ADD
ADD R1,
R1, R2,
R2, R3
R3
Evolution:
Physical coding
Symbolic documentation
Symbolic coding
Translation and execution
Requires a translator.
Jacquard loom
(1801)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 4
Lecture plan
Symbolic version
Binary version
Perspective
(The assembler will be covered in lecture 6).
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 5
//
// 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
And(R1,R2)
And(R1,R2) (bit-wise)
(bit-wise)
JMP
JMP addr
addr
//
// PC
PC
addr
addr
JEQ
JEQ R1,R1,addr
R1,R1,addr
//
// IF
IF R1
R1 == R2
R2 THEN
THEN PC
PC
addr
addr ELSE
ELSE PC++
PC++
LOAD
LOAD R1,
R1, addr
addr
//
// R1
R1
RAM[addr]
RAM[addr] Where
Where vv is
is an
an address
address
STORE
STORE R1,
R1, addr
addr
//
// RAM[addr]
RAM[addr]
R1
R1 where
where vv is
is an
an address
address
NOOP
NOOP
//
// Do
Do nothings
nothings
//
// Plus
Plus several
several more
more commands
commands that
that are
are essentially
essentially versions
versions
//
// or
or extensions
extensions of
of the
the above
above commands.
commands.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 6
Instruction memory:
Registers:
Control: The ROM is loaded with a sequence of 16-bit instructions, one per memory
location, beginning at address 0. The next instruction is always fetched from
ROM[PC]
Instruction set: Two instructions: A-instruction, C-instruction.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 7
A-instruction
@value
@value
//
// AA
value
value
Used for:
Entering a constant value
( A = value)
Coding example:
@17
//
@17
// AA == 17
17
DD == AA //
// DD == 17
17
@17
//
@17
// AA == 17
17
DD == MM //
// DD == RAM[17]
RAM[17]
Later
Selecting a ROM location
( fetch ROM[A] )
@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.idc.ac.il/tecs , Chapter 4: Machine Language
slide 8
Hack
Hackcommands:
commands:
@value
@value
//
//set
setAAto
tovalue
value
Set A to 17
Set D to A-1
op
op isis ++ or
or -xx isis A,
A, D,
D, or
or MM
yy isis A,
A, D,
D, MM or
or 11
(op
(op y)
y) is
is optional
optional
Set D to 19
dest
dest isis DD, ,MM, ,MD
MD, ,AA, ,AM
AM, ,AD
AD, ,AMD,
AMD, or
ornull
null
Set RAM[5034] to D - 1
dest
dest == xx op
op yy
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 9
sum = 0
Hack
Hackcommands:
commands:
@value
@value
//
//set
setAAto
tovalue
value
dest
dest == xx op
op yy
j = j + 1
q = sum + 12 j
op
op isis ++ or
or -xx isis A,
A, D,
D, or
or MM
yy isis A,
A, D,
D, MM or
or 11
(op
(op y)
y) is
is optional
optional
dest
dest isis DD, ,MM, ,MD
MD, ,AA, ,AM
AM, ,AD
AD, ,AMD,
AMD, or
ornull
null
arr[7] = 0
Symbol table:
Etc.
jj
sum
sum
qq
arr
arr
17
17
22
22
21
21
16
16
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 10
address
ROM
Instruction
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 11
GOTO 50
IF D = 0 GOTO 112
Hack
Hackcommands:
commands:
@@value
value
////set
setAAtotovalue
value
dest
dest==comp
comp;;jump
jump ////dest
dest==isisoptional
optional
////Where:
Where:
comp
comp==00, ,11, ,-1
-1, ,DD, ,AA, ,!D
!D, ,!A
!A, ,-D
-D, ,-A
-A, ,D+1
D+1, ,
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
dest
dest==MM, ,DD, ,MD
MD, ,AA, ,AM
AM, ,AD
AD, ,AMD,
AMD,or
ornull
null
jump
jump==JGT
JGT, ,JEQ
JEQ, ,JGE
JGE, ,JLT
JLT, ,JNE
JNE, ,JLE
JLE, ,JMP,
JMP,or
ornull
null
All
Allconditional
conditionaljumps
jumpsrefer
refertotothe
thecurrent
currentvalue
valueofofD.
D.
Symbol table:
sum
sum
xx
200
200
4000
4000
ii
END
END
151
151
50
50
NEXT
NEXT
120
120
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 12
//
// comp
comp is
is mandatory
mandatory
//
// dest
dest and
and jump
jump are
are optional
optional
Where:
comp is one of:
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,
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+1,
M-1,D+M,D-M,M-D,D&M,D|M
M, !M,
!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.idc.ac.il/tecs , Chapter 4: Machine Language
slide 13
Hack:
if
if condition
condition {{
code
code segment
segment 1}
1}
DD
not
not condition)
condition)
@IF_TRUE
@IF_TRUE
else
else {{
code
code segment
segment 2}
2}
D;JEQ
D;JEQ
code
code segment
segment 22
//
// next
next instruction
instruction
@END
@END
0;JMP
0;JMP
(IF_TRUE)
(IF_TRUE)
code
code segment
segment 11
(END)
(END)
//
// next
next instruction
instruction
slide 14
Hack:
(LOOP)
(LOOP)
DD
not
not condition)
condition)
@END
@END
D;JEQ
D;JEQ
code
code segment
segment 11
@LOOP
@LOOP
0;JMP
0;JMP
(END)
(END)
//
// next
next instruction
instruction
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 15
Hack:
//// Adds
Adds 1+...+100.
1+...+100.
into
into ii == 1;
1;
into
into sum
sum == 0;
0;
while
while (i
(i <=
<= 100){
100){
sum
sum +=
+= i;
i;
i++;
i++;
}}
Demo
CPU emulator
//// Adds
Adds 1+...+100.
1+...+100.
@i
//// ii refers
@i
refers to
to some
some memo.
memo. location
location
M=1
//// i=1
M=1
i=1
@sum
//
sum
@sum
// sum refers
refers to
to some
some memo.
memo. location
location
M=0
//
sum=0
M=0
// sum=0
(LOOP)
(LOOP)
@i
@i
D=M
//// DD == ii
D=M
@100
@100
D=D-A
D=D-A //// DD == ii -- 100
100
@END
@END
D;JGT
got END
END
D;JGT //// If
If (i-100)
(i-100) >> 00 got
@i
@i
D=M
//// DD == ii
D=M
@sum
@sum
M=D+M
M=D+M //// sum
sum +=
+= ii
@i
@i
M=M+1
M=M+1 //// i++
i++
@LOOP
@LOOP
0;JMP
0;JMP //// Got
Got LOOP
LOOP
(END)
(END)
@END
@END
0;JMP
0;JMP //// Infinite
Infinite loop
loop
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 16
Lecture plan
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 17
A-instruction
Symbolic:
@value
value (v = 0 or 1)
Binary:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 18
C-instruction
Symbolic:
dest=comp;jump
comp
Binary:
c1 c2 c3 c4
dest
c5 c6
d1 d2
jump
d3 j1 j2 j3
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 19
Symbols (user-defined)
Label symbols: User-defined symbols, used
to label destinations of got commands.
Declared by the pseudo command (XXX).
This directive defines the symbol XXX to
refer to the instruction memory location
holding the next command in the program
Variable symbols: Any user-defined symbol
xxx appearing in an assembly program that
is not defined elsewhere using the (xxx)
directive is treated as a variable, and is
assigned a unique memory address by the
assembler, starting at RAM address 16
By convention, label symbols are uppercase and variable symbols are lower-case.
//
// Recto
Recto program
program
@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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 20
Symbols (pre-defined)
Virtual registers: R0,, R15 are predefined
to be 0,,15
I/O pointers: The symbols SCREEN and KBD
are predefined to be 16384 and 24576,
respectively (base addresses of the screen
and keyboard memory maps)
Predefined pointers: the symbols
SP, LCL, ARG, THIS, and THAT
are predefined to be 0 to 4, respectively.
//
// Recto
Recto program
program
@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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 21
Perspective
Hack is a simple machine language
User friendly syntax: D=D+A instead of ADD D,D,A
Hack is a -address machine
A Macro-language can be easily developed
A Hack assembler is needed and will be discusses and developed later in
the course.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language
slide 22
slide 23