Professional Documents
Culture Documents
il/tecs
Machine Language
Usage and Copyright Notice:
This presentation contains lecture materials that accompany the textbook “The Elements of
Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, 2005.
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 non-
commercial purposes.
If you use our materials, we will appreciate it if you will include in them a reference to the book’s
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
Where we are at:
Assembly
Language
Assembler
Chapter 6
abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
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.idc.ac.il/tecs , Chapter 4: Machine Language slide 2
Machine language is “the soul of the machine”
Duality:
Another duality:
Binary version
Symbolic version
Loose definition:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , 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
Evolution:
Physical coding
Symbolic documentation
Symbolic coding
Requires a translator.
Jacquard loom Augusta Ada King,
(1801) Countess of Lovelace
(1815-1852)
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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 5
Instructions in a typical machine language
//
// 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 aa 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 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 PC
THEN 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
The Hack computer
The 16-bit Hack computer consists of the following elements:
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]
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
Later
Selecting a ROM location @17
@17 //
// AA == 17
17
( fetch ROM[A] ) JMP
JMP //
// fetch the
fetch 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
Coding examples (programming practice)
Set RAM[5034] to D - 1
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 9
Coding examples (cont.)
j = j + 1 op isis ++ or
op or --
xx isis A,
A, D,
D, or
or MM
yy isis A,
A, D,
D, MM or
or 11
q = sum + 12 – j (op
(op y) y) is
is optional
optional
dest isis DD, ,MM, ,MD
dest MD, ,AA, ,AM
AM, ,AD AMD, or
AD, ,AMD, ornull
null
arr[7] = 0
Symbol table:
Etc. jj 17
17 (All symbols and
sum
sum 22
22 values in are
qq 21
21
arbitrary examples)
arr
arr 16
16
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 10
Control (first approximation)
address Instruction
Program = sequence of 16-bit ROM
numbers, starting at
ROM[0]
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 11
Coding examples (practice)
Hack
Hackcommands:
commands:
Write the Hack instructions that @@value
value ////set
setAAtotovalue
value
implement the following tasks:
dest
dest==comp
comp;;jump
jump ////“dest
“dest==““isisoptional
optional
////Where:
Where:
GOTO 50
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&AD&A, ,
IF D = 0 GOTO 112 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 < 9 GOTO 507 dest
dest==MM, ,DD, ,MD
MD, ,AA, ,AM
AM, ,AD AMD,or
AD, ,AMD, ornull
null
jump
jump==JGT
JGT, ,JEQ
JEQ, ,JGE
JGE, ,JLT
JLT, ,JNE
JNE, ,JLE JMP,or
JLE, ,JMP, ornull
null
IF RAM[12] > 0 GOTO 50 All
Allconditional
conditionaljumps
jumpsrefer
refertotothe
thecurrent
currentvalue
valueofofD.
D.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 12
C-instruction syntax (final version)
dest
dest == comp
comp ;; jump
jump //
// 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, !M,
!M, -M,
-M, M+1,
M+1, M-1,D+M,D-M,M-D,D&M,D|M
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
IF logic – Hack style
High level: Hack:
if
if condition
condition {{ DD
not
not condition)
condition)
code
code segment
segment 1}
1} @IF_TRUE
@IF_TRUE
else
else {{ D;JEQ
D;JEQ
code
code segment
segment 2}
2} 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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 14
WHILE logic – Hack style
while
while condition
condition {{ (LOOP)
(LOOP)
code
code segment
segment 11 DD
not
not condition)
condition)
}} @END
@END
//
// next
next instruction
instruction 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
Complete program example
C: Hack:
//// Adds
Adds 1+...+100.
1+...+100. //// Adds
Adds 1+...+100.
1+...+100.
into
into ii == 1;
1; @i
@i //// ii refers
refers to to some
some memo.
memo. location
location
into M=1 //// i=1
into sum
sum == 0;
0; M=1 i=1
while @sum
@sum //// sum refers
sum refers to to some
some memo.
memo. location
location
while (i(i <=
<= 100){
100){ M=0 // sum=0
sum M=0 // sum=0
sum +=
+= i;
i; (LOOP)
i++; (LOOP)
i++; @i
@i
}} D=M //// DD == ii
D=M
@100
@100
D=D-A //// DD == ii -- 100
D=D-A 100
@END
@END
D;JGT //// If
D;JGT (i-100) >> 00 got
If (i-100) got END
END
@i
@i
D=M
D=M //// DD == ii
Demo @sum
CPU emulator @sum
M=D+M //// sum
M=D+M sum +=+= ii
@i
@i
M=M+1 //// i++
M=M+1 i++
@LOOP
@LOOP
0;JMP //// Got
0;JMP Got LOOP
LOOP
(END)
(END)
@END
@END
0;JMP //// Infinite
0;JMP Infinite looploop
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
value (v = 0 or 1)
Binary: 0 v v v v v v v v v v v v v v v
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 // Either the dest or jump fields may be empty.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 19
Symbols (user-defined) //
// Recto
Recto program
program
@R0
@R0
D=M
D=M
Label symbols: User-defined symbols, used @INFINITE_LOOP
@INFINITE_LOOP
to label destinations of got commands. D;JLE
D;JLE
Declared by the pseudo command (XXX). @counter
@counter
M=D
M=D
This directive defines the symbol XXX to @SCREEN
@SCREEN
refer to the instruction memory location D=A
D=A
holding the next command in the program @addr
@addr
M=D
M=D
Variable symbols: Any user-defined symbol (LOOP)
(LOOP)
@addr
xxx appearing in an assembly program that @addr
A=M
A=M
is not defined elsewhere using the “(xxx)“ M=-1
M=-1
directive is treated as a variable, and is @addr
@addr
D=M
assigned a unique memory address by the D=M
@32
@32
assembler, starting at RAM address 16 D=D+A
D=D+A
@addr
@addr
By convention, label symbols are upper- M=D
M=D
case and variable symbols are lower-case. @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) //
// Recto
Recto program
program
@R0
@R0
D=M
D=M
Virtual registers: R0,…, R15 are predefined @INFINITE_LOOP
@INFINITE_LOOP
to be 0,…,15 D;JLE
D;JLE
@counter
@counter
M=D
M=D
I/O pointers: The symbols SCREEN and KBD @SCREEN
@SCREEN
are predefined to be 16384 and 24576, D=A
D=A
respectively (base addresses of the screen @addr
@addr
M=D
and keyboard memory maps) M=D
(LOOP)
(LOOP)
@addr
@addr
Predefined pointers: the symbols A=M
A=M
M=-1
SP, LCL, ARG, THIS, and THAT M=-1
@addr
@addr
are predefined to be 0 to 4, respectively. 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
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 22
End-note: a macro machine language (can be implemented rather easily)
Assignment:
1. x = constant (e.g. x = 17)
2. x = y
3. x = 0 , x = 1, x = -1
Arithmetic / logical:
4. x = y op z
where y, z are variables or constants and
op is some ALU operation like +, -, and, or, etc.
Control:
5. GOTO s
6. IF condo THEN GOTO s
where condo is an expression (x op y) {=|<|>|…} {0|1}
e.g. IF x+17>0 got loop
White space or comments:
7. White space: ignore
8. // comment to the end of the line: ignore.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 23