SlideShare a Scribd company logo
1 of 22
Download to read offline
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 1
www.nand2tetris.org
Building a Modern Computer From First Principles
Assembler
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler 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 6: Assembler slide 3
Why care about assemblers?
Because …
Assemblers employ nifty programming tricks
Assemblers are the first rung up the software hierarchy ladder
An assembler is a translator of a simple language
Writing an assembler = low-impact practice for writing compilers.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 4
0000000000010000
1110111111001000
0000000000010001
1110101010001000
0000000000010000
1111110000010000
0000000000000000
1111010011010000
0000000000010010
1110001100000001
0000000000010000
1111110000010000
0000000000010001
...
0000000000010000
1110111111001000
0000000000010001
1110101010001000
0000000000010000
1111110000010000
0000000000000000
1111010011010000
0000000000010010
1110001100000001
0000000000010000
1111110000010000
0000000000010001
...
Target code
assemble
Assembly example
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
... // Etc.
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
... // Etc.
Source code (example)
The program translation challenge
Extract the program’s semantics from the source program,
using the syntax rules of the source language
Re-express the program’s semantics in the target language,
using the syntax rules of the target language
Assembler = simple translator
Translates each assembly command into one or more binary machine instructions
Handles symbols (e.g. i, sum, LOOP, …).
execute
For now,
ignore all
details!
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 5
Revisiting Hack low-level programming: an example
// Computes 1+...+RAM[0]
// And stores the sum in RAM[1].
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
// Computes 1+...+RAM[0]
// And stores the sum in RAM[1].
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
Assembly program (sum.asm) CPU emulator screen shot
after running this program
The CPU emulator allows loading and executing
symbolic Hack code. It resolves all the symbolic
symbols to memory locations, and executes the code.
program
generated
output
user
supplied
input
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 6
The assembler’s view of an assembly program
// Computes 1+...+RAM[0]
// And stores the sum in RAM[1].
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
// Computes 1+...+RAM[0]
// And stores the sum in RAM[1].
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
Assembly program
Assembly program =
a stream of text lines, each being
one of the following:
A-instruction
C-instruction
Symbol declaration: (SYMBOL)
Comment or white space:
// comment
The challenge:
Translate the program into a sequence
of 16-bit instructions that can be
executed by the target hardware
platform.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 7
Translating / assembling A-instructions
value (v = 0 or 1)
0 v v v v v v v v v v v v v v vBinary:
@value // Where value is either a non-negative decimal number
// or a symbol referring to such number.
Symbolic:
Translation to binary:
If value is a non-negative decimal number, simple
If value is a symbol, later.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 8
Translating / assembling C-instructions
jumpdestcomp
1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3
dest=comp;jump // Either the dest or jump fields may be empty.
// If dest is empty, the "=" is ommitted;
// If jump is empty, the ";" is omitted.
Symbolic:
Binary:
Translation to binary: simple!
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 9
The overall assembly logic
For each (real) command
Parse the command,
i.e. break it into its underlying fields
A-instruction: replace the symbolic
reference (if any) with the
corresponding memory address,
which is a number
(how to do it, later)
C-instruction: for each field in the
instruction, generate the
corresponding binary code
Assemble the translated binary codes
into a complete 16-bit machine
instruction
Write the 16-bit instruction to the
output file.
// Computes 1+...+RAM[0]
// And stores the sum in RAM[1].
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
// Computes 1+...+RAM[0]
// And stores the sum in RAM[1].
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
Assembly program
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 10
Assembly programs typically have many symbols:
Labels that mark destinations of goto commands
Labels that mark special memory locations
Variables
These symbols fall into two categories:
User–defined symbols (created by programmers)
Pre-defined symbols (used by the Hack platform).
Handling symbols (aka symbol resolution)
@R0
D=M
@END
D;JLE
@counter
M=D
@SCREEN
D=A
@x
M=D
(LOOP)
@x
A=M
M=-1
@x
D=M
@32
D=D+A
@x
M=D
@counter
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP
@R0
D=M
@END
D;JLE
@counter
M=D
@SCREEN
D=A
@x
M=D
(LOOP)
@x
A=M
M=-1
@x
D=M
@32
D=D+A
@x
M=D
@counter
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP
Typical symbolic Hack
assembly code:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 11
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
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
(why start at 16? Later.)
By convention, Hack programmers use lower-case and upper-
case to represent variable and label names, respectively
Q: Who does all the “automatic” assignments of symbols
to RAM addresses?
A: As part of the program translation process, the assembler
resolves all the symbols into RAM addresses.
Handling symbols: user-defined symbols
@R0
D=M
@END
D;JLE
@counter
M=D
@SCREEN
D=A
@x
M=D
(LOOP)
@x
A=M
M=-1
@x
D=M
@32
D=D+A
@x
M=D
@counter
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP
@R0
D=M
@END
D;JLE
@counter
M=D
@SCREEN
D=A
@x
M=D
(LOOP)
@x
A=M
M=-1
@x
D=M
@32
D=D+A
@x
M=D
@counter
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP
Typical symbolic Hack
assembly code:
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 12
Virtual registers:
The symbols R0,…, R15 are automatically predefined to
refer to RAM addresses 0,…,15
I/O pointers: The symbols SCREEN and KBD are automatically
predefined to refer to RAM addresses 16384 and 24576,
respectively (base addresses of the screen and keyboard
memory maps)
VM control pointers: the symbols SP, LCL, ARG, THIS, and THAT
(that don’t appear in the code example on the right) are
automatically predefined to refer to RAM addresses 0 to
4, respectively
(The VM control pointers, which overlap R0,…, R4 will come to
play in the virtual machine implementation, covered in the
next lecture)
@R0
D=M
@END
D;JLE
@counter
M=D
@SCREEN
D=A
@x
M=D
(LOOP)
@x
A=M
M=-1
@x
D=M
@32
D=D+A
@x
M=D
@counter
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP
@R0
D=M
@END
D;JLE
@counter
M=D
@SCREEN
D=A
@x
M=D
(LOOP)
@x
A=M
M=-1
@x
D=M
@32
D=D+A
@x
M=D
@counter
MD=M-1
@LOOP
D;JGT
(END)
@END
0;JMP
Typical symbolic Hack
assembly code:
Q: Who does all the “automatic” assignments of symbols
to RAM addresses?
A: As part of the program translation process, the assembler
resolves all the symbols into RAM addresses.
Handling symbols: pre-defined symbols
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 13
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@R1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@R1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
Source code (example)
This symbol table is generated by the
assembler, and used to translate the
symbolic code into binary code.
Handling symbols: symbol table
R0 0
R1 1
R2 2
... ...
R15 15
SCREEN 16384
KBD 24576
SP 0
LCL 1
ARG 2
THIS 3
THAT 4
WRITE 18
END 22
i 16
sum 17
R0 0
R1 1
R2 2
... ...
R15 15
SCREEN 16384
KBD 24576
SP 0
LCL 1
ARG 2
THIS 3
THAT 4
WRITE 18
END 22
i 16
sum 17
Symbol table
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 14
R0 0
R1 1
R2 2
...
R15 15
SCREEN 16384
KBD 24576
SP 0
LCL 1
ARG 2
THIS 3
THAT 4
WRITE 18
END 22
i 16
sum 17
R0 0
R1 1
R2 2
...
R15 15
SCREEN 16384
KBD 24576
SP 0
LCL 1
ARG 2
THIS 3
THAT 4
WRITE 18
END 22
i 16
sum 17
Symbol table
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@R1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@R1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
Source code (example)
Initialization: create an empty symbol table and
populate it with all the pre-defined symbols
First pass: go through the entire source code,
and add all the user-defined label symbols to
the symbol table (without generating any code)
Second pass: go again through the source code,
and use the symbol table to translate all the
commands. In the process, handle all the user-
defined variable symbols.
Handling symbols: constructing the symbol table
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 15
The assembly process (detailed)
Initialization: create the symbol table and initialize it with the pre-defined symbols
First pass: march through the source code without generating any code.
For each label declaration (LABEL) that appears in the source code,
add the pair <LABEL, n > to the symbol table
Second pass: march again through the source code, and process each line:
If the line is a C-instruction, simple
If the line is @xxx where xxx is a number, simple
If the line is @xxx and xxx is a symbol, look it up in the symbol table and proceed
as follows:
If the symbol is found, replace it with its numeric value and complete the
command’s translation
If the symbol is not found, then it must represent a new variable:
add the pair <xxx, n > to the symbol table, where n is the next available RAM
address, and complete the command’s translation.
(Platform design decision: the allocated RAM addresses are running,
starting at address 16).
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 16
Note that comment lines and pseudo-commands
(label declarations) generate no code.
0000000000010000
1110111111001000
0000000000010001
1110101010001000
0000000000010000
1111110000010000
0000000000000000
1111010011010000
0000000000010010
1110001100000001
0000000000010000
1111110000010000
0000000000010001
1111000010001000
0000000000010000
1111110111001000
0000000000000100
1110101010000111
0000000000010001
1111110000010000
0000000000000001
1110001100001000
0000000000010110
1110101010000111
0000000000010000
1110111111001000
0000000000010001
1110101010001000
0000000000010000
1111110000010000
0000000000000000
1111010011010000
0000000000010010
1110001100000001
0000000000010000
1111110000010000
0000000000010001
1111000010001000
0000000000010000
1111110111001000
0000000000000100
1110101010000111
0000000000010001
1111110000010000
0000000000000001
1110001100001000
0000000000010110
1110101010000111
Target code
assemble
The result ...
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@R1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
// Computes 1+...+RAM[0]
// And stored the sum in RAM[1]
@i
M=1 // i = 1
@sum
M=0 // sum = 0
(LOOP)
@i // if i>RAM[0] goto WRITE
D=M
@R0
D=D-M
@WRITE
D;JGT
@i // sum += i
D=M
@sum
M=D+M
@i // i++
M=M+1
@LOOP // goto LOOP
0;JMP
(WRITE)
@sum
D=M
@R1
M=D // RAM[1] = the sum
(END)
@END
0;JMP
Source code (example)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 17
Proposed assembler implementation
An assembler program can be written in any high-level language.
We propose a language-independent design, as follows.
Software modules:
Parser: Unpacks each command into its underlying fields
Code: Translates each field into its corresponding binary value,
and assembles the resulting values
SymbolTable: Manages the symbol table
Main: Initializes I/O files and drives the show.
Proposed implementation stages
Stage I: Build a basic assembler for programs with no symbols
Stage II: Extend the basic assembler with symbol handling capabilities.
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 18
Parser (a software module in the assembler program)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 19
Parser (a software module in the assembler program) / continued
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 20
Code (a software module in the assembler program)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 21
SymbolTable (a software module in the assembler program)
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 22
Perspective
Simple machine language, simple assembler
Most assemblers are not stand-alone, but rather encapsulated in a translator of a
higher order
C programmers that understand the code generated by a C compiler can improve
their code considerably
C programming (e.g. for real-time systems) may involve re-writing critical segments
in assembly, for optimization
Writing an assembler is an excellent practice for writing more challenging
translators, e.g. a VM Translator and a compiler, as we will do in the next lectures.

More Related Content

What's hot

コンピュータシステムの理論と実装 6章 アセンブラ
コンピュータシステムの理論と実装 6章 アセンブラコンピュータシステムの理論と実装 6章 アセンブラ
コンピュータシステムの理論と実装 6章 アセンブラYoko Yama
 
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to EmbeddedLAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to EmbeddedLinaro
 
XPDS16: Keeping coherency on ARM - Julien Grall, ARM
XPDS16: Keeping coherency on ARM - Julien Grall, ARMXPDS16: Keeping coherency on ARM - Julien Grall, ARM
XPDS16: Keeping coherency on ARM - Julien Grall, ARMThe Linux Foundation
 
Interactive Music II SuperCollider入門 4 - 楽器を定義、変調合成(RM, AM, FM)
Interactive Music II SuperCollider入門 4 -  楽器を定義、変調合成(RM, AM, FM)Interactive Music II SuperCollider入門 4 -  楽器を定義、変調合成(RM, AM, FM)
Interactive Music II SuperCollider入門 4 - 楽器を定義、変調合成(RM, AM, FM)Atsushi Tadokoro
 
Building Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 ArchBuilding Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 ArchSherif Mousa
 
Tuning Android for low RAM
Tuning Android for low RAMTuning Android for low RAM
Tuning Android for low RAMChris Simmonds
 
系統程式 -- 第 11 章
系統程式 -- 第 11 章系統程式 -- 第 11 章
系統程式 -- 第 11 章鍾誠 陳鍾誠
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver艾鍗科技
 
Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)RuggedBoardGroup
 
Codeigniter4の比較と検証
Codeigniter4の比較と検証Codeigniter4の比較と検証
Codeigniter4の比較と検証ME iBotch
 

What's hot (20)

ARM and SoC Traning Part I -- Overview
ARM and SoC Traning Part I -- OverviewARM and SoC Traning Part I -- Overview
ARM and SoC Traning Part I -- Overview
 
BeagleBone Black Bootloaders
BeagleBone Black BootloadersBeagleBone Black Bootloaders
BeagleBone Black Bootloaders
 
ARM and SoC Traning Part II - System
ARM and SoC Traning Part II - SystemARM and SoC Traning Part II - System
ARM and SoC Traning Part II - System
 
Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
 
from Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Worksfrom Source to Binary: How GNU Toolchain Works
from Source to Binary: How GNU Toolchain Works
 
コンピュータシステムの理論と実装 6章 アセンブラ
コンピュータシステムの理論と実装 6章 アセンブラコンピュータシステムの理論と実装 6章 アセンブラ
コンピュータシステムの理論と実装 6章 アセンブラ
 
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to EmbeddedLAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
LAS16-402: ARM Trusted Firmware – from Enterprise to Embedded
 
XPDS16: Keeping coherency on ARM - Julien Grall, ARM
XPDS16: Keeping coherency on ARM - Julien Grall, ARMXPDS16: Keeping coherency on ARM - Julien Grall, ARM
XPDS16: Keeping coherency on ARM - Julien Grall, ARM
 
Interactive Music II SuperCollider入門 4 - 楽器を定義、変調合成(RM, AM, FM)
Interactive Music II SuperCollider入門 4 -  楽器を定義、変調合成(RM, AM, FM)Interactive Music II SuperCollider入門 4 -  楽器を定義、変調合成(RM, AM, FM)
Interactive Music II SuperCollider入門 4 - 楽器を定義、変調合成(RM, AM, FM)
 
Building Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 ArchBuilding Mini Embedded Linux System for X86 Arch
Building Mini Embedded Linux System for X86 Arch
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
Uart
UartUart
Uart
 
Dakota+openFoam1
Dakota+openFoam1Dakota+openFoam1
Dakota+openFoam1
 
Tuning Android for low RAM
Tuning Android for low RAMTuning Android for low RAM
Tuning Android for low RAM
 
GDB Rocks!
GDB Rocks!GDB Rocks!
GDB Rocks!
 
系統程式 -- 第 11 章
系統程式 -- 第 11 章系統程式 -- 第 11 章
系統程式 -- 第 11 章
 
用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver用Raspberry Pi 學Linux I2C Driver
用Raspberry Pi 學Linux I2C Driver
 
Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)Embedded Linux BSP Training (Intro)
Embedded Linux BSP Training (Intro)
 
Virtual Machine Constructions for Dummies
Virtual Machine Constructions for DummiesVirtual Machine Constructions for Dummies
Virtual Machine Constructions for Dummies
 
Codeigniter4の比較と検証
Codeigniter4の比較と検証Codeigniter4の比較と検証
Codeigniter4の比較と検証
 

Similar to Lecture 06 assembler

nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言鍾誠 陳鍾誠
 
The ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdfThe ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdffederaleyecare
 
Parallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterParallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterSudhang Shankar
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdfXxUnnathxX
 
Lecture 08 virtual machine ii
Lecture 08 virtual machine iiLecture 08 virtual machine ii
Lecture 08 virtual machine ii鍾誠 陳鍾誠
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topicsveningstonk
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computerMartial Kouadio
 
MapReduce: teoria e prática
MapReduce: teoria e práticaMapReduce: teoria e prática
MapReduce: teoria e práticaPET Computação
 

Similar to Lecture 06 assembler (20)

Machine language
Machine languageMachine language
Machine language
 
nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言nand2tetris 舊版投影片 -- 第四章 機器語言
nand2tetris 舊版投影片 -- 第四章 機器語言
 
Mp lab manual
Mp lab manualMp lab manual
Mp lab manual
 
The ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdfThe ProblemUsing C programming language write a program that simul.pdf
The ProblemUsing C programming language write a program that simul.pdf
 
02 MATLAB programming
02 MATLAB programming02 MATLAB programming
02 MATLAB programming
 
Alp 05
Alp 05Alp 05
Alp 05
 
Parallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterParallel Programming on the ANDC cluster
Parallel Programming on the ANDC cluster
 
C programming
C programmingC programming
C programming
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
Mp &mc programs
Mp &mc programsMp &mc programs
Mp &mc programs
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 
Spr ch-02
Spr ch-02Spr ch-02
Spr ch-02
 
Lecture 08 virtual machine ii
Lecture 08 virtual machine iiLecture 08 virtual machine ii
Lecture 08 virtual machine ii
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Programming basic computer
Programming basic computerProgramming basic computer
Programming basic computer
 
מצגת פרויקט
מצגת פרויקטמצגת פרויקט
מצגת פרויקט
 
ICP - Lecture 6
ICP - Lecture 6ICP - Lecture 6
ICP - Lecture 6
 
MapReduce: teoria e prática
MapReduce: teoria e práticaMapReduce: teoria e prática
MapReduce: teoria e prática
 
Lecture 2 - Introductory Concepts
Lecture 2 - Introductory ConceptsLecture 2 - Introductory Concepts
Lecture 2 - Introductory Concepts
 

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

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 

Recently uploaded (20)

Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

Lecture 06 assembler

  • 1. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 1 www.nand2tetris.org Building a Modern Computer From First Principles Assembler
  • 2. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler 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 6: Assembler slide 3 Why care about assemblers? Because … Assemblers employ nifty programming tricks Assemblers are the first rung up the software hierarchy ladder An assembler is a translator of a simple language Writing an assembler = low-impact practice for writing compilers.
  • 4. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 4 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 ... 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 ... Target code assemble Assembly example // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT ... // Etc. // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT ... // Etc. Source code (example) The program translation challenge Extract the program’s semantics from the source program, using the syntax rules of the source language Re-express the program’s semantics in the target language, using the syntax rules of the target language Assembler = simple translator Translates each assembly command into one or more binary machine instructions Handles symbols (e.g. i, sum, LOOP, …). execute For now, ignore all details!
  • 5. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 5 Revisiting Hack low-level programming: an example // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @1 M=D // RAM[1] = the sum (END) @END 0;JMP // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @1 M=D // RAM[1] = the sum (END) @END 0;JMP Assembly program (sum.asm) CPU emulator screen shot after running this program The CPU emulator allows loading and executing symbolic Hack code. It resolves all the symbolic symbols to memory locations, and executes the code. program generated output user supplied input
  • 6. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 6 The assembler’s view of an assembly program // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @1 M=D // RAM[1] = the sum (END) @END 0;JMP // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @1 M=D // RAM[1] = the sum (END) @END 0;JMP Assembly program Assembly program = a stream of text lines, each being one of the following: A-instruction C-instruction Symbol declaration: (SYMBOL) Comment or white space: // comment The challenge: Translate the program into a sequence of 16-bit instructions that can be executed by the target hardware platform.
  • 7. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 7 Translating / assembling A-instructions value (v = 0 or 1) 0 v v v v v v v v v v v v v v vBinary: @value // Where value is either a non-negative decimal number // or a symbol referring to such number. Symbolic: Translation to binary: If value is a non-negative decimal number, simple If value is a symbol, later.
  • 8. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 8 Translating / assembling C-instructions jumpdestcomp 1 1 1 a c1 c2 c3 c4 c5 c6 d1 d2 d3 j1 j2 j3 dest=comp;jump // Either the dest or jump fields may be empty. // If dest is empty, the "=" is ommitted; // If jump is empty, the ";" is omitted. Symbolic: Binary: Translation to binary: simple!
  • 9. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 9 The overall assembly logic For each (real) command Parse the command, i.e. break it into its underlying fields A-instruction: replace the symbolic reference (if any) with the corresponding memory address, which is a number (how to do it, later) C-instruction: for each field in the instruction, generate the corresponding binary code Assemble the translated binary codes into a complete 16-bit machine instruction Write the 16-bit instruction to the output file. // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @1 M=D // RAM[1] = the sum (END) @END 0;JMP // Computes 1+...+RAM[0] // And stores the sum in RAM[1]. @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @1 M=D // RAM[1] = the sum (END) @END 0;JMP Assembly program
  • 10. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 10 Assembly programs typically have many symbols: Labels that mark destinations of goto commands Labels that mark special memory locations Variables These symbols fall into two categories: User–defined symbols (created by programmers) Pre-defined symbols (used by the Hack platform). Handling symbols (aka symbol resolution) @R0 D=M @END D;JLE @counter M=D @SCREEN D=A @x M=D (LOOP) @x A=M M=-1 @x D=M @32 D=D+A @x M=D @counter MD=M-1 @LOOP D;JGT (END) @END 0;JMP @R0 D=M @END D;JLE @counter M=D @SCREEN D=A @x M=D (LOOP) @x A=M M=-1 @x D=M @32 D=D+A @x M=D @counter MD=M-1 @LOOP D;JGT (END) @END 0;JMP Typical symbolic Hack assembly code:
  • 11. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 11 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 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 (why start at 16? Later.) By convention, Hack programmers use lower-case and upper- case to represent variable and label names, respectively Q: Who does all the “automatic” assignments of symbols to RAM addresses? A: As part of the program translation process, the assembler resolves all the symbols into RAM addresses. Handling symbols: user-defined symbols @R0 D=M @END D;JLE @counter M=D @SCREEN D=A @x M=D (LOOP) @x A=M M=-1 @x D=M @32 D=D+A @x M=D @counter MD=M-1 @LOOP D;JGT (END) @END 0;JMP @R0 D=M @END D;JLE @counter M=D @SCREEN D=A @x M=D (LOOP) @x A=M M=-1 @x D=M @32 D=D+A @x M=D @counter MD=M-1 @LOOP D;JGT (END) @END 0;JMP Typical symbolic Hack assembly code:
  • 12. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 12 Virtual registers: The symbols R0,…, R15 are automatically predefined to refer to RAM addresses 0,…,15 I/O pointers: The symbols SCREEN and KBD are automatically predefined to refer to RAM addresses 16384 and 24576, respectively (base addresses of the screen and keyboard memory maps) VM control pointers: the symbols SP, LCL, ARG, THIS, and THAT (that don’t appear in the code example on the right) are automatically predefined to refer to RAM addresses 0 to 4, respectively (The VM control pointers, which overlap R0,…, R4 will come to play in the virtual machine implementation, covered in the next lecture) @R0 D=M @END D;JLE @counter M=D @SCREEN D=A @x M=D (LOOP) @x A=M M=-1 @x D=M @32 D=D+A @x M=D @counter MD=M-1 @LOOP D;JGT (END) @END 0;JMP @R0 D=M @END D;JLE @counter M=D @SCREEN D=A @x M=D (LOOP) @x A=M M=-1 @x D=M @32 D=D+A @x M=D @counter MD=M-1 @LOOP D;JGT (END) @END 0;JMP Typical symbolic Hack assembly code: Q: Who does all the “automatic” assignments of symbols to RAM addresses? A: As part of the program translation process, the assembler resolves all the symbols into RAM addresses. Handling symbols: pre-defined symbols
  • 13. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 13 // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @R1 M=D // RAM[1] = the sum (END) @END 0;JMP // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @R1 M=D // RAM[1] = the sum (END) @END 0;JMP Source code (example) This symbol table is generated by the assembler, and used to translate the symbolic code into binary code. Handling symbols: symbol table R0 0 R1 1 R2 2 ... ... R15 15 SCREEN 16384 KBD 24576 SP 0 LCL 1 ARG 2 THIS 3 THAT 4 WRITE 18 END 22 i 16 sum 17 R0 0 R1 1 R2 2 ... ... R15 15 SCREEN 16384 KBD 24576 SP 0 LCL 1 ARG 2 THIS 3 THAT 4 WRITE 18 END 22 i 16 sum 17 Symbol table
  • 14. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 14 R0 0 R1 1 R2 2 ... R15 15 SCREEN 16384 KBD 24576 SP 0 LCL 1 ARG 2 THIS 3 THAT 4 WRITE 18 END 22 i 16 sum 17 R0 0 R1 1 R2 2 ... R15 15 SCREEN 16384 KBD 24576 SP 0 LCL 1 ARG 2 THIS 3 THAT 4 WRITE 18 END 22 i 16 sum 17 Symbol table // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @R1 M=D // RAM[1] = the sum (END) @END 0;JMP // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @R1 M=D // RAM[1] = the sum (END) @END 0;JMP Source code (example) Initialization: create an empty symbol table and populate it with all the pre-defined symbols First pass: go through the entire source code, and add all the user-defined label symbols to the symbol table (without generating any code) Second pass: go again through the source code, and use the symbol table to translate all the commands. In the process, handle all the user- defined variable symbols. Handling symbols: constructing the symbol table
  • 15. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 15 The assembly process (detailed) Initialization: create the symbol table and initialize it with the pre-defined symbols First pass: march through the source code without generating any code. For each label declaration (LABEL) that appears in the source code, add the pair <LABEL, n > to the symbol table Second pass: march again through the source code, and process each line: If the line is a C-instruction, simple If the line is @xxx where xxx is a number, simple If the line is @xxx and xxx is a symbol, look it up in the symbol table and proceed as follows: If the symbol is found, replace it with its numeric value and complete the command’s translation If the symbol is not found, then it must represent a new variable: add the pair <xxx, n > to the symbol table, where n is the next available RAM address, and complete the command’s translation. (Platform design decision: the allocated RAM addresses are running, starting at address 16).
  • 16. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 16 Note that comment lines and pseudo-commands (label declarations) generate no code. 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 1111000010001000 0000000000010000 1111110111001000 0000000000000100 1110101010000111 0000000000010001 1111110000010000 0000000000000001 1110001100001000 0000000000010110 1110101010000111 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000000000000 1111010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 1111000010001000 0000000000010000 1111110111001000 0000000000000100 1110101010000111 0000000000010001 1111110000010000 0000000000000001 1110001100001000 0000000000010110 1110101010000111 Target code assemble The result ... // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @R1 M=D // RAM[1] = the sum (END) @END 0;JMP // Computes 1+...+RAM[0] // And stored the sum in RAM[1] @i M=1 // i = 1 @sum M=0 // sum = 0 (LOOP) @i // if i>RAM[0] goto WRITE D=M @R0 D=D-M @WRITE D;JGT @i // sum += i D=M @sum M=D+M @i // i++ M=M+1 @LOOP // goto LOOP 0;JMP (WRITE) @sum D=M @R1 M=D // RAM[1] = the sum (END) @END 0;JMP Source code (example)
  • 17. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 17 Proposed assembler implementation An assembler program can be written in any high-level language. We propose a language-independent design, as follows. Software modules: Parser: Unpacks each command into its underlying fields Code: Translates each field into its corresponding binary value, and assembles the resulting values SymbolTable: Manages the symbol table Main: Initializes I/O files and drives the show. Proposed implementation stages Stage I: Build a basic assembler for programs with no symbols Stage II: Extend the basic assembler with symbol handling capabilities.
  • 18. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 18 Parser (a software module in the assembler program)
  • 19. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 19 Parser (a software module in the assembler program) / continued
  • 20. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 20 Code (a software module in the assembler program)
  • 21. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 21 SymbolTable (a software module in the assembler program)
  • 22. Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 6: Assembler slide 22 Perspective Simple machine language, simple assembler Most assemblers are not stand-alone, but rather encapsulated in a translator of a higher order C programmers that understand the code generated by a C compiler can improve their code considerably C programming (e.g. for real-time systems) may involve re-writing critical segments in assembly, for optimization Writing an assembler is an excellent practice for writing more challenging translators, e.g. a VM Translator and a compiler, as we will do in the next lectures.