SlideShare a Scribd company logo
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 1
www.nand2tetris.org
Building a Modern Computer From First Principles
Machine Language
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 2
Where we are at:
Assembler
Chapter 6
H.L. Language
&
Operating Sys.
abstract interface
Compiler
Chapters 10 - 11
VM Translator
Chapters 7 - 8
Computer
Architecture
Chapters 4 - 5
Gate Logic
Chapters 1 - 3 Electrical
Engineering
Physics
Virtual
Machine
abstract interface
Software
hierarchy
Assembly
Language
abstract interface
Hardware
hierarchy
Machine
Language
abstract interface
Hardware
Platform
abstract interface
Chips &
Logic Gates
abstract interface
Human
Thought
Abstract design
Chapters 9, 12
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 3
Machine language
Abstraction – implementation duality:
Machine language ( = instruction set) can be viewed as a programmer-
oriented abstraction of the hardware platform
The hardware platform can be viewed as a physical means for realizing
the machine language abstraction
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.nand2tetris.org , Chapter 4: Machine Language slide 4
Binary and symbolic notation
Evolution:
Physical coding
Symbolic documentation
Symbolic coding
Translation and execution
Requires a translator.
1010 0001 0010 1011 ADD R1, R2, R3
Jacquard loom
(1801)
Augusta Ada King,
Countess of Lovelace
(1815-1852)
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 what follows R1,R2,R3 are registers, PC is program counter,
// and addr is some value.
ADD R1,R2,R3 // R1 R2 + R3
ADDI R1,R2,addr // R1 R2 + addr
AND R1,R1,R2 // R1 R1 and R2 (bit-wise)
JMP addr // PC addr
JEQ R1,R2,addr // IF R1 == R2 THEN PC addr ELSE PC++
LOAD R1, addr // R1 RAM[addr]
STORE R1, addr // RAM[addr] R1
NOP // Do nothing
// Etc. – some 50-300 command variants
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 6
The Hack computer
A 16-bit machine consisting of the following elements:
Data memory: RAM – an addressable sequence of registers
Instruction memory: ROM – an addressable sequence of registers
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. Fetch-execute cycle: later
Instruction set: Two instructions: A-instruction, C-instruction.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 7
The A-instruction
@value // A value
Where value is either a number or a symbol referring to some number.
Used for:
Entering a constant value
( A = value) @17 // A = 17
D = A // D = 17
Coding example:
@17 // A = 17
D = M // D = RAM[17]
Selecting a RAM location
( register = RAM[A])
@17 // A = 17
JMP // fetch the instruction
// stored in ROM[17]
Selecting a ROM location
( PC = A )
Later
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 8
The C-instruction (first approximation)
Exercise: Implement the following tasks
using Hack commands:
Set D to A-1
Set both A and D to A + 1
Set D to 19
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.
dest = x + y
dest = x - y
dest = x
dest = 0
dest = 1
dest = -1
x = {A, D, M}
y = {A, D, M , 1}
dest = {A, D, M, MD, A, AM, AD, AMD, null}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 9
The C-instruction (first approximation)
j 3012
sum 4500
q 3812
arr 20561
Symbol table:
(All symbols and values
are arbitrary examples)
Exercise: Implement the following tasks
using Hack commands:
sum = 0
j = j + 1
q = sum + 12 – j
arr[3] = -1
arr[j] = 0
arr[j] = 17
etc.
dest = x + y
dest = x - y
dest = x
dest = 0
dest = 1
dest = -1
x = {A, D, M}
y = {A, D, M , 1}
dest = {A, D, M, MD, A, AM, AD, AMD, null}
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 10
Control (focus on the yellow chips only)
ALU
Mux
D
A/M
a-bit
D register
A register
A
MRAM
(selected
register)
ROM
(selected
register)
PC
Instruction
In the Hack architecture:
ROM = instruction memory
Program = sequence of 16-bit
numbers, starting at
ROM[0]
Current instruction = ROM[PC]
To select instruction n from the ROM,
we set A to n, using the instruction @n
address
input
address
input
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 11
Coding examples (practice)
Exercise: Implement the following
tasks using Hack commands:
goto 50
if D==0 goto 112
if D<9 goto 507
if RAM[12] > 0 goto 50
if sum>0 goto END
if x[i]<=0 goto NEXT.
sum 2200
x 4000
i 6151
END 50
NEXT 120
Symbol table:
(All symbols and
values in are
arbitrary examples)
In the command dest = comp; jump, the jump materialzes
Hack commands:
A-command: @value // set A to value
C-command: dest = comp ; jump // dest = and ;jump
// are optional
Where:
comp = 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
dest = M , D , MD , A , AM , AD , AMD, or null
jump = JGT , JEQ , JGE , JLT , JNE , JLE , JMP, or null
In the command dest = comp; jump, the jump materialzes
if (comp jump 0) is true. For example, in D=D+1,JLT,
we jump if D+1 < 0.
Hack convention:
True is represented by -1
False is represented by 0
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 12
if condition {
code block 1
}
else {
code block 2
}
code block 3
High level:
D condition
@IF_FALSE
D;J_NotConditionRelation
code block 1
@END
0;JMP
(IF_FALSE)
code block 2
(END)
code block 3
Hack:
IF logic – Hack style
Hack convention:
True is represented by -1
False is represented by 0
Re-write the condition so
that 0 is on one side and use
the Not of the condition’s
relation.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 13
WHILE logic – Hack style
while condition {
code block 1
}
Code block 2
High level:
(LOOP)
D condition
@END
D;J_NotConditionRelation
code block 1
@LOOP
0;JMP
(END)
code block 2
Hack:
Hack convention:
True is represented by -1
False is represented by 0
Re-write the condition so
that 0 is on one side and use
the Not of the condition’s
relation
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 14
Side note (focus on the yellow chip parts only)
ALU
Mux
D
A/M
a-bit
D register
A register
A
MRAM
(selected
register)
ROM
(selected
register)
PC
Instruction
address
input
address
input
In the Hack architecture, the A register
addresses both the RAM and the ROM,
simultaneously. Therefore:
Command pairs like @addr followed by
D=M;someJumpDirective make no sense
Best practice: in well-written Hack
programs, a C-instruction should contain
either a reference to M, or
a jump directive,
but not both.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 15
Complete program example
// Adds 1+...+100.
into i = 1;
into sum = 0;
while (i <= 100){
sum += i;
i++;
}
C language code:
100
END
LOOP
// Adds 1+...+100.
@i // i refers to some RAM location
M=1 // i=1
@sum // sum refers to some RAM location
M=0 // sum=0
(LOOP)
@i
D=M // D = i
@100
D=D-A // D = i - 100
@END
D;JGT // If (i-100) > 0 goto END
@i
D=M // D = i
@sum
M=D+M // sum += i
@i
M=M+1 // i++
@LOOP
0;JMP // Goto LOOP
(END)
@END
0;JMP // Infinite loop
Hack assembly code:
Demo
CPU emulator
Hack assembly convention:
Variables: lower-case
Labels: upper-case
Commands: upper-case
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 16
Symbols in Hack assembly programs
Symbols created by Hack programmers and code generators:
Label symbols: Used to label destinations of goto 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 (within the program, XXX is called “label”)
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 “automatically” assigned a unique RAM
address, starting at RAM address 16
By convention, Hack programmers use lower-case and upper-case letters
for variable names and labels, respectively.
Predefined symbols:
I/O pointers: The symbols SCREEN and KBD are “automatically”
predefined to refer to RAM addresses 16384 and 24576, respectively
(base addresses of the Hack platform’s screen and keyboard memory
maps)
Virtual registers: covered in future lectures.
VM control registers: covered in future lectures.
// Typical symbolic
// Hack code, meaning
// not important
@R0
D=M
@INFINITE_LOOP
D;JLE
@counter
M=D
@SCREEN
D=A
@addr
M=D
(LOOP)
@addr
A=M
M=-1
@addr
D=M
@32
D=D+A
@addr
M=D
@counter
MD=M-1
@LOOP
D;JGT
(INFINITE_LOOP)
@INFINITE_LOOP
0;JMP
Q: Who does all the “automatic” assignments of symbols to RAM addresses?
A: The assembler, which is the program that translates symbolic Hack
programs into binary Hack program. As part of the translation process,
the symbols are resolved to RAM addresses. (more about this in future lectures)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 17
Perspective
Hack is a simple machine language
User friendly syntax: D=D+A instead of ADD D,D,A
Hack is a “½-address machine”: any operation that needs to operate on the
RAM must be specified using two commands: an A-command to address the
RAM, and a subsequent C-command to operate on it
A Macro-language can be easily developed
A Hack assembler is needed and will be discusses and developed later in
the course.

More Related Content

What's hot

Compilers
CompilersCompilers
Compilers
Bense Tony
 
Types of Programming Languages
Types of Programming LanguagesTypes of Programming Languages
Types of Programming Languages
Juhi Bhoyar
 
Assembly language
Assembly languageAssembly language
Assembly language
shashank puthran
 
Computer languages
Computer languagesComputer languages
Computer languages
Buxoo Abdullah
 
High level and Low level Language
High level and Low level Language High level and Low level Language
High level and Low level Language
adnan usmani
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
Deva Singh
 
Graphical programming
Graphical programmingGraphical programming
Graphical programming
Bilal Maqbool ツ
 
Operating system presentation
Operating system presentationOperating system presentation
Operating system presentation
ashanrajpar
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
Neeru Mittal
 
Computer system bus
Computer system busComputer system bus
Computer system bus
Goran W. Hama Ali
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Syed Zaid Irshad
 
Programming in c
Programming in cProgramming in c
Programming in c
indra Kishor
 
Chapter 04 computer codes
Chapter 04 computer codesChapter 04 computer codes
Chapter 04 computer codesIIUI
 
Computer Organization and Architecture.
Computer Organization and Architecture.Computer Organization and Architecture.
Computer Organization and Architecture.
CS_GDRCST
 
Instruction format
Instruction formatInstruction format
Instruction format
Sanjeev Patel
 
Von Neumann Architecture
Von Neumann ArchitectureVon Neumann Architecture
Von Neumann Architecture
Chamodi Adikaram
 
History of C Programming Language
History of C Programming LanguageHistory of C Programming Language
History of C Programming Language
Niloy Biswas
 
Introduction of c programming
Introduction of c programmingIntroduction of c programming
Introduction of c programming
Tarun Sharma
 
Von-Neumann machine and IAS architecture
Von-Neumann machine and  IAS architectureVon-Neumann machine and  IAS architecture
Von-Neumann machine and IAS architecture
Shishir Aryal
 
Fetch decode-execute presentation
Fetch decode-execute presentationFetch decode-execute presentation
Fetch decode-execute presentationchantellemallia
 

What's hot (20)

Compilers
CompilersCompilers
Compilers
 
Types of Programming Languages
Types of Programming LanguagesTypes of Programming Languages
Types of Programming Languages
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Computer languages
Computer languagesComputer languages
Computer languages
 
High level and Low level Language
High level and Low level Language High level and Low level Language
High level and Low level Language
 
Algorithms and Flowcharts
Algorithms and FlowchartsAlgorithms and Flowcharts
Algorithms and Flowcharts
 
Graphical programming
Graphical programmingGraphical programming
Graphical programming
 
Operating system presentation
Operating system presentationOperating system presentation
Operating system presentation
 
Introduction to programming
Introduction to programmingIntroduction to programming
Introduction to programming
 
Computer system bus
Computer system busComputer system bus
Computer system bus
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Chapter 04 computer codes
Chapter 04 computer codesChapter 04 computer codes
Chapter 04 computer codes
 
Computer Organization and Architecture.
Computer Organization and Architecture.Computer Organization and Architecture.
Computer Organization and Architecture.
 
Instruction format
Instruction formatInstruction format
Instruction format
 
Von Neumann Architecture
Von Neumann ArchitectureVon Neumann Architecture
Von Neumann Architecture
 
History of C Programming Language
History of C Programming LanguageHistory of C Programming Language
History of C Programming Language
 
Introduction of c programming
Introduction of c programmingIntroduction of c programming
Introduction of c programming
 
Von-Neumann machine and IAS architecture
Von-Neumann machine and  IAS architectureVon-Neumann machine and  IAS architecture
Von-Neumann machine and IAS architecture
 
Fetch decode-execute presentation
Fetch decode-execute presentationFetch decode-execute presentation
Fetch decode-execute presentation
 

Similar to Machine language

nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言
鍾誠 陳鍾誠
 
Lecture 06 assembler
Lecture 06 assemblerLecture 06 assembler
Lecture 06 assembler
鍾誠 陳鍾誠
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
鍾誠 陳鍾誠
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
XxUnnathxX
 
Introduction to computer architecture .pptx
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptx
Fatma Sayed Ibrahim
 
Lecture 08 virtual machine ii
Lecture 08 virtual machine iiLecture 08 virtual machine ii
Lecture 08 virtual machine ii
鍾誠 陳鍾誠
 
Buffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the StackBuffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the Stack
ironSource
 
Computer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docxComputer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docx
brownliecarmella
 
Introduction
IntroductionIntroduction
Introduction
鍾誠 陳鍾誠
 
CH06 (1).PPT
CH06 (1).PPTCH06 (1).PPT
CH06 (1).PPT
RaghadAbuJelban
 
Lecture 07 virtual machine i
Lecture 07 virtual machine iLecture 07 virtual machine i
Lecture 07 virtual machine i
鍾誠 陳鍾誠
 
Lecture 12 os
Lecture 12 osLecture 12 os
Lecture 12 os
鍾誠 陳鍾誠
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
veningstonk
 
Intro to assembly language
Intro to assembly languageIntro to assembly language
Intro to assembly language
United International University
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
LiemLe21
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computerMartial Kouadio
 
C programming
C programmingC programming
C programming
ASHISH KUMAR
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
Lakshmi Sarvani Videla
 
Chapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuChapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structu
EstelaJeffery653
 

Similar to Machine language (20)

nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言
 
Lecture 06 assembler
Lecture 06 assemblerLecture 06 assembler
Lecture 06 assembler
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
Introduction to computer architecture .pptx
Introduction to computer architecture .pptxIntroduction to computer architecture .pptx
Introduction to computer architecture .pptx
 
Lecture 08 virtual machine ii
Lecture 08 virtual machine iiLecture 08 virtual machine ii
Lecture 08 virtual machine ii
 
Buffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the StackBuffer Overflow - Smashing the Stack
Buffer Overflow - Smashing the Stack
 
Computer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docxComputer architecture is made up of two main components the Instruct.docx
Computer architecture is made up of two main components the Instruct.docx
 
Introduction
IntroductionIntroduction
Introduction
 
CH06 (1).PPT
CH06 (1).PPTCH06 (1).PPT
CH06 (1).PPT
 
Lecture 07 virtual machine i
Lecture 07 virtual machine iLecture 07 virtual machine i
Lecture 07 virtual machine i
 
Lecture 12 os
Lecture 12 osLecture 12 os
Lecture 12 os
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
Intro to assembly language
Intro to assembly languageIntro to assembly language
Intro to assembly language
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computer
 
C programming
C programmingC programming
C programming
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
Chapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structuChapter 1SyllabusCatalog Description Computer structu
Chapter 1SyllabusCatalog Description Computer structu
 

More from 鍾誠 陳鍾誠

用十分鐘瞭解 新竹科學園區的發展史
用十分鐘瞭解  新竹科學園區的發展史用十分鐘瞭解  新竹科學園區的發展史
用十分鐘瞭解 新竹科學園區的發展史
鍾誠 陳鍾誠
 
用十分鐘搞懂 λ-Calculus
用十分鐘搞懂 λ-Calculus用十分鐘搞懂 λ-Calculus
用十分鐘搞懂 λ-Calculus
鍾誠 陳鍾誠
 
交⼤資訊⼯程學系備審資料 ⾱詠祥
交⼤資訊⼯程學系備審資料 ⾱詠祥交⼤資訊⼯程學系備審資料 ⾱詠祥
交⼤資訊⼯程學系備審資料 ⾱詠祥
鍾誠 陳鍾誠
 
smallpt: Global Illumination in 99 lines of C++
smallpt:  Global Illumination in 99 lines of C++smallpt:  Global Illumination in 99 lines of C++
smallpt: Global Illumination in 99 lines of C++
鍾誠 陳鍾誠
 
西洋史 (你或許不知道但卻影響現代教育的那些事)
西洋史  (你或許不知道但卻影響現代教育的那些事)西洋史  (你或許不知道但卻影響現代教育的那些事)
西洋史 (你或許不知道但卻影響現代教育的那些事)
鍾誠 陳鍾誠
 
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
鍾誠 陳鍾誠
 
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
鍾誠 陳鍾誠
 
梯度下降法 (隱藏在深度學習背後的演算法) -- 十分鐘系列
梯度下降法  (隱藏在深度學習背後的演算法) -- 十分鐘系列梯度下降法  (隱藏在深度學習背後的演算法) -- 十分鐘系列
梯度下降法 (隱藏在深度學習背後的演算法) -- 十分鐘系列
鍾誠 陳鍾誠
 
用十分鐘理解 《微分方程》
用十分鐘理解  《微分方程》用十分鐘理解  《微分方程》
用十分鐘理解 《微分方程》
鍾誠 陳鍾誠
 
系統程式 -- 前言
系統程式 -- 前言系統程式 -- 前言
系統程式 -- 前言
鍾誠 陳鍾誠
 
系統程式 -- 附錄
系統程式 -- 附錄系統程式 -- 附錄
系統程式 -- 附錄
鍾誠 陳鍾誠
 
系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 12 章 系統軟體實作系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 12 章 系統軟體實作
鍾誠 陳鍾誠
 
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統
鍾誠 陳鍾誠
 
系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統
鍾誠 陳鍾誠
 
系統程式 -- 第 9 章 虛擬機器
系統程式 -- 第 9 章 虛擬機器系統程式 -- 第 9 章 虛擬機器
系統程式 -- 第 9 章 虛擬機器
鍾誠 陳鍾誠
 
系統程式 -- 第 8 章 編譯器
系統程式 -- 第 8 章 編譯器系統程式 -- 第 8 章 編譯器
系統程式 -- 第 8 章 編譯器
鍾誠 陳鍾誠
 
系統程式 -- 第 7 章 高階語言
系統程式 -- 第 7 章 高階語言系統程式 -- 第 7 章 高階語言
系統程式 -- 第 7 章 高階語言
鍾誠 陳鍾誠
 
系統程式 -- 第 6 章 巨集處理器
系統程式 -- 第 6 章 巨集處理器系統程式 -- 第 6 章 巨集處理器
系統程式 -- 第 6 章 巨集處理器
鍾誠 陳鍾誠
 
系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入
鍾誠 陳鍾誠
 
系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器
鍾誠 陳鍾誠
 

More from 鍾誠 陳鍾誠 (20)

用十分鐘瞭解 新竹科學園區的發展史
用十分鐘瞭解  新竹科學園區的發展史用十分鐘瞭解  新竹科學園區的發展史
用十分鐘瞭解 新竹科學園區的發展史
 
用十分鐘搞懂 λ-Calculus
用十分鐘搞懂 λ-Calculus用十分鐘搞懂 λ-Calculus
用十分鐘搞懂 λ-Calculus
 
交⼤資訊⼯程學系備審資料 ⾱詠祥
交⼤資訊⼯程學系備審資料 ⾱詠祥交⼤資訊⼯程學系備審資料 ⾱詠祥
交⼤資訊⼯程學系備審資料 ⾱詠祥
 
smallpt: Global Illumination in 99 lines of C++
smallpt:  Global Illumination in 99 lines of C++smallpt:  Global Illumination in 99 lines of C++
smallpt: Global Illumination in 99 lines of C++
 
西洋史 (你或許不知道但卻影響現代教育的那些事)
西洋史  (你或許不知道但卻影響現代教育的那些事)西洋史  (你或許不知道但卻影響現代教育的那些事)
西洋史 (你或許不知道但卻影響現代教育的那些事)
 
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
 
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列區塊鏈  (比特幣背後的關鍵技術)   -- 十分鐘系列
區塊鏈 (比特幣背後的關鍵技術) -- 十分鐘系列
 
梯度下降法 (隱藏在深度學習背後的演算法) -- 十分鐘系列
梯度下降法  (隱藏在深度學習背後的演算法) -- 十分鐘系列梯度下降法  (隱藏在深度學習背後的演算法) -- 十分鐘系列
梯度下降法 (隱藏在深度學習背後的演算法) -- 十分鐘系列
 
用十分鐘理解 《微分方程》
用十分鐘理解  《微分方程》用十分鐘理解  《微分方程》
用十分鐘理解 《微分方程》
 
系統程式 -- 前言
系統程式 -- 前言系統程式 -- 前言
系統程式 -- 前言
 
系統程式 -- 附錄
系統程式 -- 附錄系統程式 -- 附錄
系統程式 -- 附錄
 
系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 12 章 系統軟體實作系統程式 -- 第 12 章 系統軟體實作
系統程式 -- 第 12 章 系統軟體實作
 
系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統系統程式 -- 第 11 章 嵌入式系統
系統程式 -- 第 11 章 嵌入式系統
 
系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統系統程式 -- 第 10 章 作業系統
系統程式 -- 第 10 章 作業系統
 
系統程式 -- 第 9 章 虛擬機器
系統程式 -- 第 9 章 虛擬機器系統程式 -- 第 9 章 虛擬機器
系統程式 -- 第 9 章 虛擬機器
 
系統程式 -- 第 8 章 編譯器
系統程式 -- 第 8 章 編譯器系統程式 -- 第 8 章 編譯器
系統程式 -- 第 8 章 編譯器
 
系統程式 -- 第 7 章 高階語言
系統程式 -- 第 7 章 高階語言系統程式 -- 第 7 章 高階語言
系統程式 -- 第 7 章 高階語言
 
系統程式 -- 第 6 章 巨集處理器
系統程式 -- 第 6 章 巨集處理器系統程式 -- 第 6 章 巨集處理器
系統程式 -- 第 6 章 巨集處理器
 
系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入系統程式 -- 第 5 章 連結與載入
系統程式 -- 第 5 章 連結與載入
 
系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器系統程式 -- 第 4 章 組譯器
系統程式 -- 第 4 章 組譯器
 

Recently uploaded

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 

Recently uploaded (20)

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 

Machine language

  • 1. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 1 www.nand2tetris.org Building a Modern Computer From First Principles Machine Language
  • 2. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 2 Where we are at: Assembler Chapter 6 H.L. Language & Operating Sys. abstract interface Compiler Chapters 10 - 11 VM Translator Chapters 7 - 8 Computer Architecture Chapters 4 - 5 Gate Logic Chapters 1 - 3 Electrical Engineering Physics Virtual Machine abstract interface Software hierarchy Assembly Language abstract interface Hardware hierarchy Machine Language abstract interface Hardware Platform abstract interface Chips & Logic Gates abstract interface Human Thought Abstract design Chapters 9, 12
  • 3. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 3 Machine language Abstraction – implementation duality: Machine language ( = instruction set) can be viewed as a programmer- oriented abstraction of the hardware platform The hardware platform can be viewed as a physical means for realizing the machine language abstraction 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.
  • 4. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 4 Binary and symbolic notation Evolution: Physical coding Symbolic documentation Symbolic coding Translation and execution Requires a translator. 1010 0001 0010 1011 ADD R1, R2, R3 Jacquard loom (1801) Augusta Ada King, Countess of Lovelace (1815-1852)
  • 5. 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 what follows R1,R2,R3 are registers, PC is program counter, // and addr is some value. ADD R1,R2,R3 // R1 R2 + R3 ADDI R1,R2,addr // R1 R2 + addr AND R1,R1,R2 // R1 R1 and R2 (bit-wise) JMP addr // PC addr JEQ R1,R2,addr // IF R1 == R2 THEN PC addr ELSE PC++ LOAD R1, addr // R1 RAM[addr] STORE R1, addr // RAM[addr] R1 NOP // Do nothing // Etc. – some 50-300 command variants
  • 6. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 6 The Hack computer A 16-bit machine consisting of the following elements: Data memory: RAM – an addressable sequence of registers Instruction memory: ROM – an addressable sequence of registers 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. Fetch-execute cycle: later Instruction set: Two instructions: A-instruction, C-instruction.
  • 7. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 7 The A-instruction @value // A value Where value is either a number or a symbol referring to some number. Used for: Entering a constant value ( A = value) @17 // A = 17 D = A // D = 17 Coding example: @17 // A = 17 D = M // D = RAM[17] Selecting a RAM location ( register = RAM[A]) @17 // A = 17 JMP // fetch the instruction // stored in ROM[17] Selecting a ROM location ( PC = A ) Later
  • 8. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 8 The C-instruction (first approximation) Exercise: Implement the following tasks using Hack commands: Set D to A-1 Set both A and D to A + 1 Set D to 19 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. dest = x + y dest = x - y dest = x dest = 0 dest = 1 dest = -1 x = {A, D, M} y = {A, D, M , 1} dest = {A, D, M, MD, A, AM, AD, AMD, null}
  • 9. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 9 The C-instruction (first approximation) j 3012 sum 4500 q 3812 arr 20561 Symbol table: (All symbols and values are arbitrary examples) Exercise: Implement the following tasks using Hack commands: sum = 0 j = j + 1 q = sum + 12 – j arr[3] = -1 arr[j] = 0 arr[j] = 17 etc. dest = x + y dest = x - y dest = x dest = 0 dest = 1 dest = -1 x = {A, D, M} y = {A, D, M , 1} dest = {A, D, M, MD, A, AM, AD, AMD, null}
  • 10. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 10 Control (focus on the yellow chips only) ALU Mux D A/M a-bit D register A register A MRAM (selected register) ROM (selected register) PC Instruction In the Hack architecture: ROM = instruction memory Program = sequence of 16-bit numbers, starting at ROM[0] Current instruction = ROM[PC] To select instruction n from the ROM, we set A to n, using the instruction @n address input address input
  • 11. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 11 Coding examples (practice) Exercise: Implement the following tasks using Hack commands: goto 50 if D==0 goto 112 if D<9 goto 507 if RAM[12] > 0 goto 50 if sum>0 goto END if x[i]<=0 goto NEXT. sum 2200 x 4000 i 6151 END 50 NEXT 120 Symbol table: (All symbols and values in are arbitrary examples) In the command dest = comp; jump, the jump materialzes Hack commands: A-command: @value // set A to value C-command: dest = comp ; jump // dest = and ;jump // are optional Where: comp = 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 dest = M , D , MD , A , AM , AD , AMD, or null jump = JGT , JEQ , JGE , JLT , JNE , JLE , JMP, or null In the command dest = comp; jump, the jump materialzes if (comp jump 0) is true. For example, in D=D+1,JLT, we jump if D+1 < 0. Hack convention: True is represented by -1 False is represented by 0
  • 12. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 12 if condition { code block 1 } else { code block 2 } code block 3 High level: D condition @IF_FALSE D;J_NotConditionRelation code block 1 @END 0;JMP (IF_FALSE) code block 2 (END) code block 3 Hack: IF logic – Hack style Hack convention: True is represented by -1 False is represented by 0 Re-write the condition so that 0 is on one side and use the Not of the condition’s relation.
  • 13. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 13 WHILE logic – Hack style while condition { code block 1 } Code block 2 High level: (LOOP) D condition @END D;J_NotConditionRelation code block 1 @LOOP 0;JMP (END) code block 2 Hack: Hack convention: True is represented by -1 False is represented by 0 Re-write the condition so that 0 is on one side and use the Not of the condition’s relation
  • 14. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 14 Side note (focus on the yellow chip parts only) ALU Mux D A/M a-bit D register A register A MRAM (selected register) ROM (selected register) PC Instruction address input address input In the Hack architecture, the A register addresses both the RAM and the ROM, simultaneously. Therefore: Command pairs like @addr followed by D=M;someJumpDirective make no sense Best practice: in well-written Hack programs, a C-instruction should contain either a reference to M, or a jump directive, but not both.
  • 15. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 15 Complete program example // Adds 1+...+100. into i = 1; into sum = 0; while (i <= 100){ sum += i; i++; } C language code: 100 END LOOP // Adds 1+...+100. @i // i refers to some RAM location M=1 // i=1 @sum // sum refers to some RAM location M=0 // sum=0 (LOOP) @i D=M // D = i @100 D=D-A // D = i - 100 @END D;JGT // If (i-100) > 0 goto END @i D=M // D = i @sum M=D+M // sum += i @i M=M+1 // i++ @LOOP 0;JMP // Goto LOOP (END) @END 0;JMP // Infinite loop Hack assembly code: Demo CPU emulator Hack assembly convention: Variables: lower-case Labels: upper-case Commands: upper-case
  • 16. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 16 Symbols in Hack assembly programs Symbols created by Hack programmers and code generators: Label symbols: Used to label destinations of goto 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 (within the program, XXX is called “label”) 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 “automatically” assigned a unique RAM address, starting at RAM address 16 By convention, Hack programmers use lower-case and upper-case letters for variable names and labels, respectively. Predefined symbols: I/O pointers: The symbols SCREEN and KBD are “automatically” predefined to refer to RAM addresses 16384 and 24576, respectively (base addresses of the Hack platform’s screen and keyboard memory maps) Virtual registers: covered in future lectures. VM control registers: covered in future lectures. // Typical symbolic // Hack code, meaning // not important @R0 D=M @INFINITE_LOOP D;JLE @counter M=D @SCREEN D=A @addr M=D (LOOP) @addr A=M M=-1 @addr D=M @32 D=D+A @addr M=D @counter MD=M-1 @LOOP D;JGT (INFINITE_LOOP) @INFINITE_LOOP 0;JMP Q: Who does all the “automatic” assignments of symbols to RAM addresses? A: The assembler, which is the program that translates symbolic Hack programs into binary Hack program. As part of the translation process, the symbols are resolved to RAM addresses. (more about this in future lectures)
  • 17. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 4: Machine Language slide 17 Perspective Hack is a simple machine language User friendly syntax: D=D+A instead of ADD D,D,A Hack is a “½-address machine”: any operation that needs to operate on the RAM must be specified using two commands: an A-command to address the RAM, and a subsequent C-command to operate on it A Macro-language can be easily developed A Hack assembler is needed and will be discusses and developed later in the course.