Course overview
Introduction to Computer p Yung-Yu Chuang
with slides by Nisan & Schocken (www.nand2tetris.org)
Logistics
• Meeting time: 2:20pm-5:20pm, Tuesday Classroom: CSIE Room 104
• Classroom: CSIE Room 104
• Instructor: 莊永裕 Yung-Yu Chuang T hi i t t TBD
• Teaching assistant: TBD
• Webpage:
http://www.csie.ntu.edu.tw/~cyy/introcs
id / password p
• Mailing list: introcs@cmlab.csie.ntu.edu.tw Please subscribe via
Please subscribe via
https://cmlmail.csie.ntu.edu.tw/mailman/listinfo/introcs/
Textbook
The Elements of Computing Systems Noam Nisan
Systems, Noam Nisan,
Shimon Schocken, MIT Press
References (TOY)
Princeton’s Introduction to CS,
htt // i t d /i t
http://www.cs.princeton.edu/intro cs/50machine/
http://www.cs.princeton.edu/intro
cs/60circuits/
References
CODE: The Hidden Language of
C t H d d S ft
Computer Hardware and Software, Charles Petzold, Microsoft Press.
Digital Design and Computer Digital Design and Computer
Architecture, 2 nd Edition, David
Harris and Sarah Harris Morgan
Harris and Sarah Harris, Morgan
Kaufmann.
Grading (subject to change)
• Assignments (n projects, 60%) from the accompanying website
accompanying website
• Class participation (4%)
• Midterm exam (16%)
• Final project (20%) p j ( )
Early computers
Early programming tools
First popular PCs
Early PCs
• Intel 8086 processor processor
• 768KB memory
• 20MB disk
• Dot-Matrix
printer (9-pin)
GUI/IDE
More advanced architectures
• Pipeline SIMD
• SIMD
• Multi-core
• Cache
More advanced software
More “computers” around us
My computers
Desktop
(Intel Pentium D 3GHz Nvidia 7900)
VAIO Z46TD
(I l C 2 D P9700 2 8GH ) 3GHz, Nvidia 7900)
(Intel Core 2 Duo P9700 2.8GHz)
iPhone 5
iPad 2 ARM Cortex-A15?) (A6, iPad 2
(dual-core A5 1GHz)
Goal of the course
The course at a glance
Objectives:
U d t d h h d d ft t
• Understand how hardware and software systems are built and how they work together
• Learn how to break complex problems into simpler ones
• Learn how to break complex problems into simpler ones
• Learn how large scale development projects are planned and executed
planned and executed
• Have fun
Methodology:
B ild l t l d ki
• Build a complete, general-purpose and working computer system
Play and experiment with this computer at any level of
• Play and experiment with this computer, at any level of
interest
TOY machine
TOY machine
• Starting from a simple construct
TOY machine
• Build several components and connect them together
together
TOY machine
• Almost as good as any computers
TOY machine
A DUP 32
int A[32]; 10: C020
lda R1, 1 lda RA, A
20: 7101 21: 7A00 lda RC, 0
d ld RD 0 FF i=0;
Do {
RD tdi
22: 7C00 23 8DFF read ld RD, 0xFF
bz RD, exit add R2 RA RC RD=stdin;
if (RD==0) break;
23: 8DFF 24: CD29 25: 12AC add R2, RA, RC
sti RD, R2 add RC, RC, R1 A[i]=RD;
i=i+1;
25: 12AC 26: BD02 27: 1CC1 bz R0, read
it jl RF i t } while (1);
i t ()
28: C023 29 FF2B exit jl RF, printr
hlt
printr(); 29: FF2B
2A: 0000
From NAND to Tetris
• The elements of computing systems C
• Courses
• Software
• Cool stuffs
Pong on the Hack computer
Pong, 1985 Pong, 2011
Pong, on our g,
computer
Theme and structure of the book
abstract interface
Software
Human Abstract design
H.L. Language
&
Operating Sys.
abstract interface
Compiler
Chapters 10 - 11
VM Translator Vi t l
abstract interface
hierarchy
Thought
Chapters 9, 12
VM Translator
Chapters 7 - 8
Virtual Machine
Assembly Language
abstract interface
Assembler
Chapter 6
abstract interface
Computer Architecture
Chapters 4 - 5
Gate Logic Machine
Language
abstract interface
H d
abstract interface
Gate Logic
Chapters 1 - 3 Electrical
Engineering
Physics
Hardware hierarchy
Hardware Platform
Chips &
Logic Gates
abstract interface
hierarchy
(Abstraction–implementation paradigm)
Application level: Pong (an example)
Ball
abstraction Bat Bat
abstraction
The big picture
H.L. Language
&
abstract interface
Compiler
abstract interface
Software hierarchy
Human Thought
Abstract design
Chapters 9, 12
Operating Sys. Chapters 10 - 11
VM Translator
Chapters 7 - 8
Virtual Machine
Assembly
abstract interface
Assembler Ch t 6
Assembly Language
Chapter 6
Computer Architecture Machine
L
abstract interface
abstract interface Chapters 4 - 5
Gate Logic
Chapters 1 - 3 Electrical
Engineering
H d
Language
Hardware Platform
Chips &
abstract interface
Engineering
Physics
Hardware hierarchy
Chips &
Logic Gates
High-level programming (Jack language)
/** A Graphic Bat for a Pong Game */
class Bat {
field int x, y; // screen location of the bat's top-left corner field int width, height; // bat's width & height
// The class constructor and most of the class methods are omitted
/** Draws (color=true) or erases (color=false) the bat */
method void draw(boolean color) { do Screen.setColor(color);
do Screen.drawRectangle(x,y,x+width,y+height);
Typical call to OS th d
g ( ,y, ,y g );
return;
}
/** Moves the bat one step (4 pixels) to the right. */
an OS method
/ Moves the bat one step (4 pixels) to the right. / method void moveR() {
do draw(false); // erase the bat at the current location let x = x + 4; // change the bat's X-location
// but don't go beyond the screen's right border // but don t go beyond the screen s right border
if ((x + width) > 511) { let x = 511 - width;
}
do draw(true); // re draw the bat in the new location
Ball abstraction
Bat abstraction
do draw(true); // re-draw the bat in the new location return;
} }
Operating system level (Jack OS)
/** An OS-level screen driver that abstracts the computer's physical screen */
class Screen {
static boolean currentColor; // the current color static boolean currentColor; // the current color
// The Screen class is a collection of methods, each implementing one // abstract screen-oriented operation. Most of this code is omitted.
/** Draws a rectangle in the current color. */
// the rectangle's top left corner is anchored at screen location (x0,y0) // and its width and length are x1 and y1, respectively.
function void drawRectangle(int x0, int y0, int x1, int y1) { var int x, y;
let x = x0;
while (x < x1) { while (x < x1) { let y = y0;
while(y < y1) {
do Screen.drawPixel(x,y);
let y = y+1;
}
let x = x+1;
}
Ball abstraction
Bat abstraction
} } }
The big picture
abstract interface
Software
hi h
Human Th ht
Abstract design
H.L. Language
&
Operating Sys.
Compiler
Chapters 10 - 11
VM Translator Virtual
abstract interface
hierarchy
Thought
Chapters 9, 12
Chapters 7 - 8
Virtual Machine
Assembly Language
abstract interface
Assembler
Chapter 6
abstract interface
Computer Architecture
Chapters 4 - 5
Gate Logic Machine
Language
Hardware
abstract interface
g
Chapters 1 - 3 Electrical
Engineering
Physics
Hardware hierarchy
Hardware Platform
Chips &
Logic Gates
abstract interface
hierarchy
A modern compilation model
Some Other language
Jack language
. . .
Some
language
. . .
Proj. 9: building an app.Proj. 12: building the OS
Projects Some
compiler Some Other compiler
Jack compiler
VM language
10-11
VM implementation
over CISC platforms
VM imp.
over RISC platforms
VM imp.
over the Hack platform
VM emulator
Projects 7-8
RISC machine
Hack hi CISC
machine
written in a high level
. . .
machine language
machine language machine
language
. . .
a high-levellanguage
. . .
Projects1 6. . .
RISC machine
other digital platforms, each equipped with its VM implementation
Hack computer
CISC machine
Any computer
1-6
Compilation 101
Source code push x
Intermediate code
>
parsing Code
generation Source code
(x + width) > 511
push x
push width add
push 511
+ 511
push 511 gt
width x
Abstraction Syntax Implementation
Analysis Parse
Tree Semantic
Synthesis
Observations:
Modularity
Abstraction / implementation interplay
The implementation uses abstract services from the level below.
The virtual machine (VM modeled after JVM)
if ((x+width)>511) {
let x=511-width; 75
memory (before)
s2...
// VM implementation
let x 511 width;
}
75 450
sp450
x width
75
...
push x // s1 push width // s2 add // s3
75
...
s4 s5 s9
push 511 // s4 gt // s5 if-goto L1 // s6
525 511
sp1
sp511 450
spgoto L2 // s7 L1:
push 511 // s8 s10
memory (after)
push width // s9 sub // s10 pop x // s11
61
sp
450
...
width
...
memory (after)
L2:
...
x 61
...
The big picture
abstract interface
Software
hi h
Human Th ht
Abstract design
H.L. Language
&
Operating Sys.
Compiler
Chapters 10 - 11
VM Translator Virtual
abstract interface
hierarchy
Thought
Chapters 9, 12
Chapters 7 - 8
Virtual Machine
Assembly Language
abstract interface
Assembler
Chapter 6
abstract interface
Computer Architecture
Chapters 4 - 5
Gate Logic Machine
Language
Hardware
abstract interface
g
Chapters 1 - 3 Electrical
Engineering
Physics
Hardware hierarchy
Hardware Platform
Chips &
Logic Gates
abstract interface
hierarchy
Low-level programming (on Hack)
Virtual machine program
...
push x push width p add
push 511 gt
gt
if-goto L1 goto L2 L1:
L1:
push 511 push width
sub sub pop x L2:
...
Low-level programming (on Hack)
Virtual machine program
...
push x push width p add
push 511 gt
Assembly program
VM translator gt
if-goto L1 goto L2 L1:
// push 511
@511
D=A // D=511 L1:
push 511 push width
sub
D A // D 511
@SP A=M
M=D // *SP=D push 511
sub pop x L2:
M D // SP D
@SP
M=M+1 // SP++
...
Low-level programming (on Hack)
Virtual machine program
...
push x push width p add
push 511 gt
Assembly program
VM
translator gt
if-goto L1 goto L2 L1:
// push 511
@511
D=A // D=511 L1:
push 511 push width
sub
//
@SP A=M
M=D // *SP=D Assembler Executable
push 511
sub pop x L2:
M D // SP D
@SP
M=M+1 // SP++
0000000000000000 1110110010001000
@SP
M=M+1 // SP++
...
The big picture
abstract interface
Software
hi h
Human Th ht
Abstract design
H.L. Language
&
Operating Sys.
Compiler
Chapters 10 - 11
VM Translator Virtual
abstract interface
hierarchy
Thought
Chapters 9, 12
Chapters 7 - 8
Virtual Machine
Assembly Language
abstract interface
Assembler
Chapter 6
abstract interface
Computer Architecture
Chapters 4 - 5
Gate Logic Machine
Language
Hardware
abstract interface
g
Chapters 1 - 3 Electrical
Engineering
Physics
Hardware hierarchy
Hardware Platform
Chips &
Logic Gates
abstract interface
hierarchy
Machine language semantics (Hack)
Instruction code
Code semantics
, as interpreted by the Hack hardware platformCode syntax
Instruction code
(0=“address” inst.) Address
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 0 1 1 1 0 0 1 0 0 0 0000000000000000
1111110111001000
@0 M=M-1
ALU operation code
Destination Code
Jump Code Instruction code
(1=“compute” inst.) operation code (M-1)
Code (M)
Code (no jump) (1 compute inst.)
• We need a hardware architecture that realizes this semantics
• The hardware platform should be designed to:
o Parse instructions, and ,
o Execute them.
Computer architecture (Hack)
instruction
Data Memory
(M)
ALU
Instruction
Memory A
D
data out (M)
M
data out
address of next instruction
Program Counter
instruction
data in RAM(A)
• A typical Von Neumann machine
The big picture
Software
Human Abstract design
H.L. Language
&
Operating Sys.
abstract interface
Compiler
Chapters 10 - 11
abstract interface
Software hierarchy
Human Thought
g
Chapters 9, 12
p g y
VM Translator
Chapters 7 - 8
Virtual Machine
Assembly Language
abstract interface
Assembler Chapter 6
Language
Computer Architecture
Chapters 4 - 5
Machine Language
abstract interface
abstract interface
Gate Logic
Chapters 1 - 3 Electrical
Engineering
Physics
Hardware
Hardware Platform
Chips &
Logic Gates
abstract interface
y
hierarchy
Logic GatesLogic design
• Combinational logic (leading to an ALU) S i l l i (l di RAM)
• Sequential logic (leading to a RAM)
• Putting the whole thing together (leading to a computer)
Using … gate logic
Gate logic
Hardware platform = inter-connected set of chips
Chips are made of simpler chips, all the way down to elemantary logic gates h ps are made of s mpler ch ps, all the way down to elemantary log c gates
Logic gate = hardware element that implements a certain Boolean function
Every chip and gate has an interface specifying WHAT it is doing and an
Every chip and gate has an interface , specifying WHAT it is doing, and an implementation , specifying HOW it is doing it.
Interface
a
Implementation
Xor a
b
out And
O t
a
Not
0 0 0
0 1 1
a b out
And Not
Or out
0 1 1 1 0 1 1 1 0
b And
Hardware description language (HDL)
And
a
And
Or out
Not
And Not
b CHIP Xor { CHIP Xor {
IN a,b;
OUT out;
PARTS PARTS:
Not(in=a,out=Nota);
Not(in=b,out=Notb);
And(a=a,b=Notb,out=w1);
And(a=Nota,b=b,out=w2);
Or(a=w1,b=w2,out=out);
}
The tour ends:
Interface One implementation option (CMOS)
out a
b Nand
0 0 1
a b out
b
0 0 1
0 1 1
1 0 1
1 1 0
The tour map, revisited
Compiler
abstract interface
Software
hierarchy
Human Thought
Abstract design
Ch 9 12
H.L. Language
&
Operating Sys.
Compiler
Chapters 10 - 11
VM Translator Virtual
abstract interface
hierarchy
abstract interface
g Chapters 9, 12
Operating System
Machine Chapters 7 - 8Assembly Language
abstract interface
Course overview:
Assembler Chapter 6
Computer
abstract interface
Course overview:
Building this world, from the ground up
C t A hit t
Computer Architecture
Chapters 4 - 5
Gate Logic Machine
Language
Hardware
abstract interface
abstract interface
Di it l S t D i
Computer Architecture
Chapters 1 - 3 Electrical EngineeringPhysics
Hardware hierarchy
Platform
Chips &
Logic Gates
abstract interface