You are on page 1of 23

Elements of Computing Systems, Nisan & Schocken, MIT Press 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 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:

Human Abstract design Software


abstract interface
Thought hierarchy
Chapters 9, 12
H.L. Language Compiler
& abstract interface
Chapters 10 - 11
Operating Sys.
Virtual VM Translator
abstract interface
Machine Chapters 7 - 8

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:

 Machine language ( = instruction set) can be viewed as an abstract


(programmer-oriented) description of the hardware platform

 The hardware can be viewed as a physical means for realizing an


abstract machine language

Another duality:

 Binary version

 Symbolic version

Loose definition:

 Machine language = an agreed upon formalism for manipulating


a memory using a processor and a set of registers

 Same spirit but different syntax across different hardware platforms.

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

 Translation and execution

 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

 Machine languages at a glance

 The Hack machine language:

 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
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:

Data memory: RAM – a series of 16-bit words

Instruction memory: ROM – a series of 16-bit words

Registers: D, A, M, where M stands for RAM[A]

Processing: ALU, capable of computing various functions

Program counter: PC, holding an address

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

Where value is either a number or a symbol referring to some number.

Used for: Coding example:

 Entering a constant value


( A = value) @17
@17 //
// AA == 17
17
DD == AA //
// DD == 17
17

 Selecting a RAM location @17


@17 //
// AA == 17
17
( register = RAM[A]) DD == MM //
// DD == RAM[17]
RAM[17]

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)

Write the Hack instructions that Hack


Hackcommands:
commands:
implement the following tasks:
@value
@value //
//set
setAAto
tovalue
value
 Set A to 17 dest
dest == xx op
op yy

 Set D to A-1 op isis ++ or


op or --
xx isis A,
A, D,
D, or
or MM
 Set both A and D to A + 1 yy isis A,
A, D,
D, MM or
or 11
(op
(op y) y) is
is optional
optional
 Set D to 19 dest isis DD, ,MM, ,MD
dest MD, ,AA, ,AM
AM, ,AD AMD, or
AD, ,AMD, ornull
null

 Set both A and D to A + D

 Set RAM[5034] to D - 1

 Set RAM[53] to 171

 Add 1 to RAM[7], and store the result in D.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 9
Coding examples (cont.)

Write the Hack instructions that


Hack
Hackcommands:
commands:
implement the following tasks:
@value
@value //
//set
setAAto
tovalue
value
 sum = 0
dest
dest == xx op
op yy

 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)

 ROM = instruction memory

address Instruction
 Program = sequence of 16-bit ROM
numbers, starting at
ROM[0]

 Current instruction = ROM[A]

 To select instruction n from the ROM,


we set A to n, using the instruction @n A

(The actual architecture is slightly


different, as we’ll see in the next
lecture)

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.

 IF sum > 0 GOTO END Symbol table:


sum
sum 200
200
 IF axis] <= 0 GOTO NEXT. xx 4000
4000 (All symbols and
ii 151 values in are
151
arbitrary examples)
END
END 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
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

dest is one of:


null,
null, M,
M, D,
D, MD,
MD, A,
A, AM,
AM, AD,
AD, AMD
AMD

jump is one of:


null,
null, JGT,
JGT, JEQ,
JEQ, JGE,
JGE, JLT,
JLT, JNE,
JNE, JLE,
JLE, JMP
JMP

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

 To prevent conflicting use of the A register, in well-written


Hack programs a C-instruction that includes a jump directive
should not contain a reference to M, and vice versa.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 14
WHILE logic – Hack style

High level: Hack:

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

 Symbolic machine language

 Binary machine language

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 4: Machine Language slide 17
A-instruction

Symbolic: @value // Where value is either a non-negative decimal number


// or a symbol referring to such number.

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.

comp dest jump


Binary: 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 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) //
// 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

 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
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

You might also like