• 沒有找到結果。

with slides by Nisan & Schocken (www.nand2tetris.org)

N/A
N/A
Protected

Academic year: 2022

Share "with slides by Nisan & Schocken (www.nand2tetris.org)"

Copied!
54
0
0

加載中.... (立即查看全文)

全文

(1)

Course overview

Introduction to Computer Yung-Yu Chuang

with slides by Nisan & Schocken (www.nand2tetris.org)

(2)

Logistics

• Meeting time: 2:20pm-5:20pm, Tuesday

• Classroom: CSIE Room 101

• Instructor: 莊永裕 Yung-Yu Chuang

• Teaching assistant: 沈林承 魏敏家

• Webpage:

http://www.csie.ntu.edu.tw/~cyy/introcs

id / password

• Mailing list: introcs@cmlab.csie.ntu.edu.tw Please subscribe via

https://cmlmail.csie.ntu.edu.tw/mailman/listinfo/introcs/

(3)

Textbook

The Elements of Computing Systems, Noam Nisan,

Shimon Schocken, MIT Press

Nand2Tetris on Coursea

(4)

References (TOY)

Princeton’s Introduction to CS,

http://www.cs.princeton.edu/intro cs/50machine/

http://www.cs.princeton.edu/intro

cs/60circuits/

(5)

Grading (subject to change)

• Assignments (n projects, 50%) from the accompanying website

• Class participation (5%)

• Midterm quiz(20%)

• Final project (25%)

(6)

Early computers

(7)

Early programming tools

(8)

First popular PCs

(9)

Early PCs

• Intel 8086 processor

• 768KB memory

• 20MB disk

• Dot-Matrix

printer (9-pin)

(10)

GUI/IDE

(11)

More advanced architectures

• Pipeline

• SIMD

• Multi-core

• Cache

(12)

More advanced software

(13)

More “computers” around us

(14)

My computers

iPhone 6+

(A8, ARMv8-A) iPad 2

(dual-core A5 1GHz)

MacBook Air

(dual-core Intel Core i5, 1.3GHz) Desktop

(Intel Pentium D

3GHz, Nvidia 7900)

(15)

The downside

• “Once upon a time, every computer specialist

had a gestalt understanding of how computers

worked. … As modern computer technologies

have become increasingly more complex, this

clarity is all but lost.” Quoted from the textbook

(16)

How is it done?

// First Example in Programming 101 class Main {

function void main () {

do Output.printString("Hello World");

do Output.println(); // New line return;

}

}

(17)

Main secret of computer science

Don’t worry about the “how”

Only about the “what”

• Extremely complicated system

• Information hiding

implementation

abstraction

what our programming

language promises to do

(18)

Main secret of computer science

Don’t worry about the “how”

But, someone has to, for example, you.

(19)

Goal of the course

“The best way to understand how computers

work is to build one from scratch.” Quoted from the

textbook

(20)

The course at a glance

Objectives:

• Understand how hardware and software systems are built and how they work together

• Learn how to break complex problems into simpler ones

• Learn how large scale development projects are planned and executed

• Have fun

Methodology:

• Build a complete, general-purpose and working computer system

• Play and experiment with this computer, at any level of

interest

(21)

TOY machine

(22)

TOY machine

• Starting from a simple construct

(23)

Logic gates

NOT AND

OR

(24)

Components

PC

Registers

W W Data

A Data B Data W Addr A Addr B Addr +

1

Memory

W W Data Addr

R Data

IR op

d s t

A L U Cond

Eval

(25)

25

Toy machine

PC

Registers

W W Data

A Data B Data W Addr A Addr B Addr +

1-bit counter 1

5 2

4

=0

>0

Opcode Execute

Fetch Clock

Memory

W W Data Addr

R Data

IR op

d s t

Cond Eval

A L U

Control

Clock

10

10 0

1 10

1001 00

(26)

TOY machine

• Almost as good as any computers

(27)

TOY machine

A DUP 32

lda R1, 1 lda RA, A lda RC, 0

read ld RD, 0xFF bz RD, exit add R2, RA, RC sti RD, R2

add RC, RC, R1 bz R0, read exit jl RF, printr

hlt int A[32];

i=0;

Do {

RD=stdin;

if (RD==0) break;

A[i]=RD;

i=i+1;

} while (1);

printr();

10: C020

20: 7101

21: 7A00

22: 7C00

23: 8DFF

24: CD29

25: 12AC

26: BD02

27: 1CC1

28: C023

29: FF2B

2A: 0000

(28)

TOY machine

(29)

From NAND to Tetris

• The elements of computing systems

• Courses

• Software

• Cool stuffs

(30)

Pong on the Hack computer

Pong, 1985 Pong, 2011

Pong, on our

computer

(31)

Theme and structure of the book

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

(Abstraction–implementation paradigm)

(32)

Application level: Pong (an example)

Ball

abstraction Bat

abstraction

(33)

The big picture

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

(34)

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

return;

}

/** 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 if ((x + width) > 511) {

let x = 511 - width;

}

do draw(true); // re-draw the bat in the new location return;

} }

Ball abstraction

Bat abstraction

Typical call to

an OS method

(35)

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

// 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) { let y = y0;

while(y < y1) {

do Screen.drawPixel(x,y);

let y = y+1;

}

let x = x+1;

} } }

Ball abstraction

Bat abstraction

(36)

The big picture

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

(37)

A modern compilation model

. . .

RISC machine

VM language

other digital platforms, each equipped with its VM implementation RISC

machine language

Hack computer

Hack machine language CISC

machine language

CISC machine

. . .

Projects 10-11

written in a high-level

language

Any computer

. . .

VM implementation

over CISC platforms

VM imp.

over RISC platforms

VM imp.

over the Hack platform

VM emulator Some Other

language

Jack language

Some

compiler Some Other compiler

Jack compiler

. . .

Projects 7-8 Some

language

. . .

Projects 1-6 Proj. 9: building an app.

Proj. 12: building the OS

(38)

Compilation 101

Observations:

 Modularity

 Abstraction / implementation interplay

 The implementation uses abstract services from the level below.

parsing Code

generation Source code

(x + width) > 511

Abstraction

push x push width add

push 511 gt

Intermediate code

Implementation Syntax

Analysis Parse

Tree Semantic

Synthesis

width x

+ 511

>

(39)

The virtual machine (VM modeled after JVM)

// VM implementation push x // s1 push width // s2 add // s3 push 511 // s4 gt // s5 if-goto L1 // s6 goto L2 // s7 L1:

push 511 // s8 push width // s9 sub // s10 pop x // s11 L2:

...

if ((x+width)>511) { let x=511-width;

}

75 450

sp

memory (before)

s2

450

...

x width

75

...

...

525 511

sp

1

sp

511 450

sp

s4 s5 s9

61

sp

s10

450

...

x width

61

...

...

memory (after)

(40)

The big picture

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

(41)

Low-level programming (on Hack)

...

push x push width add

push 511 gt

if-goto L1 goto L2 L1:

push 511 push width sub

pop x L2:

...

Virtual machine program

(42)

Low-level programming (on Hack)

...

push x push width add

push 511 gt

if-goto L1 goto L2 L1:

push 511 push width sub

pop x L2:

...

// push 511

@511

D=A // D=511

@SP A=M

M=D // *SP=D

@SP

M=M+1 // SP++

Virtual machine program

Assembly program

VM translator

push 511

(43)

Low-level programming (on Hack)

...

push x push width add

push 511 gt

if-goto L1 goto L2 L1:

push 511 push width sub

pop x L2:

...

// push 511

@511

D=A // D=511

@SP A=M

M=D // *SP=D

@SP

M=M+1 // SP++

Virtual machine program

Assembly program

0000000000000000 1110110010001000

Executable

VM

translator

Assembler push 511

@SP

M=M+1 // SP++

(44)

The big picture

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

(45)

Machine language semantics (Hack)

Code syntax

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

(0=“address” inst.) Address

ALU operation code

(M-1)

Destination Code

(M)

Jump Code (no jump)

Code semantics

, as interpreted by the Hack hardware platform

Instruction code (1=“compute” inst.) 0000000000000000

1111110111001000

@0 M=M-1

• We need a hardware architecture that realizes this semantics

• The hardware platform should be designed to:

o Parse instructions, and

o Execute them.

(46)

Computer architecture (Hack)

• A typical Von Neumann machine

Data Memory

(M)

ALU

Instruction Memory

instruction

A D

M

Program Counter

address of next instruction

data in

data out

RAM(A)

(47)

The big picture

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

(48)

Logic design

• Combinational logic (leading to an ALU)

• Sequential logic (leading to a RAM)

• Putting the whole thing together (leading to a computer)

Using … gate logic

(49)

Gate logic

Xor a

b

out

0 0 0 0 1 1 1 0 1 1 1 0 a b out

Interface

And

And Not

Or out

a

b

Not

Implementation

 Hardware platform = inter-connected set of chips

 Chips are made of simpler chips, all the way down to elemantary logic 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

implementation , specifying HOW it is doing it.

(50)

Hardware description language (HDL)

And

And Not

Or out

a

b

Not

CHIP Xor { IN a,b;

OUT out;

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

}

(51)

The tour ends:

0 0 1 0 1 1 1 0 1 1 1 0 a b out

out a

b Nand

Interface One implementation option (CMOS)

(52)

Electronics Digital System Design

Computer Architecture

Operating System

Compiler

The tour map, revisited

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

Course overview:

Building this world, from the ground up

(53)

What you will learn

• Number systems

• Combinational logic

• Sequential logic

• Basic principle of computer architecture

• Assembler

• Virtual machine

• High-level language

• Fundamentals of compilers

• Basic operating system

• Application programming

(54)

In short

參考文獻

相關文件

• Suppose the input graph contains at least one tour of the cities with a total distance at most B. – Then there is a computation path for

The/That new smartphone has a better camera and (a) thinner screen than the/that old

Managing and Evaluating an HCS siRNA Screen of the p53 Pathway with AcuityXpress Software.

• When light is refracted into two rays each polarized with the vibration directions.. oriented at right angles to one another, and traveling at

Write the following problem on the board: “What is the area of the largest rectangle that can be inscribed in a circle of radius 4?” Have one half of the class try to solve this

 For students of other class levels, arrangements of reading out the papers / using screen readers / asking for pronunciations of words are only applicable

In digital systems, a register transfer operation is a basic operation that consists of a transfer of binary information from one set of registers into another set of

The remaining positions contain //the rest of the original array elements //the rest of the original array elements.