Building a Modern Computer From First Principles

Slides:



Advertisements
Similar presentations
Chapter 4: Machine Language
Advertisements

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 1: Boolean Logic slide 1www.idc.ac.il/tecs Chapter 1: Boolean.
Slide 4-1 Copyright © 2004 Pearson Education, Inc. Operating Systems: A Modern Perspective, Chapter 4 Computer Organization.
Chapter 10- Instruction set architectures
Slide 1CPU Emulator Tutorial This program is part of the software suite that accompanies the book The Digital Core, by Noam Nisan and Shimon Schocken 2003,
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 5: Computer Architecture slide 1www.idc.ac.il/tecs Chapter.
The Elements of Computing Systems
Execution of an instruction
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Computer ArchitectureFall 2007 © Sep 10 th, 2007 Majd F. Sakr CS-447– Computer Architecture.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 3: Sequential Logic slide 1www.idc.ac.il/tecs Chapter 3:
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 8: VM II: Program Control slide 1www.idc.ac.il/tecs Chapter.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 7: VM I: Stack Arithmetic slide 1www.idc.ac.il/tecs Chapter.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Introduction: Hello, World Below slide 1www.idc.ac.il/tecs Introduction:
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 2: Boolean Logic slide 1www.idc.ac.il/tecs Chapter 2: Boolean.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs Compiler.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virtual Machine I: Stack Arithmetic slide 1www.nand2tetris.org.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virutal Machine, Part I slide 1www.idc.ac.il/tecs Virtual Machine.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 4: Machine Language slide 1www.idc.ac.il/tecs Machine Language.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 6: Assembler slide 1www.idc.ac.il/tecs Assembler Elements of Computing.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 5: Computer Architecture slide 1www.idc.ac.il/tecs Computer Architecture.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 5: Computer Architecture slide 1www.nand2tetris.org Building a.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Slide 1/22Assembler Tutorial, Index This program is part of the software suite that accompanies the book The Elements of Computing.
Cosc 2150: Computer Organization
Computer Systems Organization CS 1428 Foundations of Computer Science.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Building a Modern Computer From First Principles
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 11: Compiler II: Code Generation slide 1www.idc.ac.il/tecs.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Boolean Logic slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Boolean Logic slide 1www.idc.ac.il/tecs Boolean Logic Elements.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 2: Boolean Arithmetic slide 1www.nand2tetris.org Building a Modern.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 8: Virtual Machine, Part II slide 1www.idc.ac.il/tecs Virtual Machine.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 8: Virtual Machine, Part II slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 1: Compiler II: Code Generation slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 11: Compiler II: Code Generation slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virutal Machine, Part I slide 1www.nand2tetris.org Building.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 3: Sequential Logic slide 1www.idc.ac.il/tecs Sequential Logic.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 3: Sequential Logic slide 1www.nand2tetris.org Building a Modern.
Execution of an instruction
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
1 Text Reference: Warford. 2 Computer Architecture: The design of those aspects of a computer which are visible to the programmer. Architecture Organization.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 9: High-Level Language slide 1www.nand2tetris.org Building a Modern.
September 26, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 2: Implementation of a Simplified Computer Jeremy R. Johnson Wednesday,
Electronic Analog Computer Dr. Amin Danial Asham by.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 7: Virutal Machine, Part I slide 1www.nand2tetris.org Building.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 6: Assembler slide 1www.nand2tetris.org Building a Modern Computer.
Processor Fundamentals Assembly Language. Learning Objectives Show understanding of the relationship between assembly language and machine code, including.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 4: Machine Language slide 1www.nand2tetris.org Building a Modern.
Computer Organization Instructions Language of The Computer (MIPS) 2.
1 Basic Processor Architecture. 2 Building Blocks of Processor Systems CPU.
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 4: Machine Language slide 1www.nand2tetris.org Building a Modern.
Jeremy R. Johnson William M. Mongan
1 Chapter 1 Basic Structures Of Computers. Computer : Introduction A computer is an electronic machine,devised for performing calculations and controlling.
CS2100 Computer Organisation
Introduction: From Nand to Tetris
Control Unit Lecture 6.
Virtual Machine Part I: Stack Arithmetic
Virtual Machine Emulator Tutorial
Compiler I: Sytnax Analysis
Fundamentals of Computer Organisation & Architecture
Systems Architecture I (CS ) Lecture 2: A Simplified Computer
CPU Emulator Tutorial This program is part of the software suite
Processor Fundamentals
Instruction encoding We’ve already seen some important aspects of processor design. A datapath contains an ALU, registers and memory. Programmers and compilers.
Introduction to Computer Systems
Computer Architecture Assembly Language
Presentation transcript:

Building a Modern Computer From First Principles Machine Language Building a Modern Computer From First Principles www.nand2tetris.org 1

We provide both PPT and PDF versions. Usage and Copyright Notice: Copyright © Noam Nisan and Shimon Schocken This presentation contains lecture materials that accompany the textbook “The Elements of Computing Systems” by Noam Nisan & Shimon Schocken, MIT Press, 2005. We provide both PPT and PDF versions. Our web site, www.nand2tetris.org ,features a set of presentations, one for each book chapter. Each presentation is designed to support about 3 hours of classroom or self-study instruction. You are welcome to use or edit this presentation as you see fit for instructional and non-commercial purposes. If you use our materials, please include a reference to www.nand2tetris.org If you have any questions or comments, please write us at nand2tetris@gmail.com 2

Where we are at: Software hierarchy Hardware Abstract design Human 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 Software hierarchy Assembly Language Hardware Platform Chips & Logic Gates Human Thought Abstract design Chapters 9, 12

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.

Binary and symbolic notation 1010 0001 0010 1011 ADD R1, R2, R3 Jacquard loom (1801) Evolution: Physical coding Symbolic documentation Symbolic coding Translation and execution Requires a translator. Augusta Ada King, Countess of Lovelace (1815-1852)

Lecture plan Machine languages at a glance The Hack machine language: Symbolic version Binary version Perspective (The assembler will be covered in lecture 6).

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

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.

The A-instruction @value // A  value Used for: 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: Selecting a RAM location ( register = RAM[A]) @17 // A = 17 D = M // D = RAM[17] Later @17 // A = 17 JMP // fetch the instruction // stored in ROM[17] Selecting a ROM location ( PC = A )

The C-instruction (first approximation) dest = x + y dest = x - y dest = x dest = 0 dest = 1 dest = -1 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. x = {A, D, M} y = {A, D, M , 1} dest = {A, D, M, MD, A, AM, AD, AMD, null}

The C-instruction (first approximation) Exercise: Implement the following tasks using Hack commands: sum = 0 j = j + 1 q = sum + 12 – j arr[7] = 0 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} j 17 sum 22 q 21 arr 16 Symbol table: (All symbols and values in are arbitrary examples)

Control (focus on the yellow chips only) ALU Mux D A/M a D register A register A M RAM (selected register) ROM 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

Coding examples (practice) Hack commands: @value // set A to value dest = comp ; jump // “dest = “ is 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 All conditional jumps refer to the current value of D. 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 200 x 4000 i 151 END 50 NEXT 120 Symbol table: (All symbols and values in are arbitrary examples)

C-instruction syntax (final version) dest = comp ; jump // comp is mandatory // dest and jump are optional Where: 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 comp is one of: null, M, D, MD, A, AM, AD, AMD dest is one of: null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP jump is one of:

IF logic – Hack style if condition { code block 1} else { High level: D  not condition @IF_TRUE D;JEQ code block 2 @END 0;JMP (IF_TRUE) code block 1 (END) code block 3 Hack: Hack convention: True is represented by -1 False is represented by 0

WHILE logic – Hack style while condition { code block 1 } Code block 2 High level: (LOOP) D  not condition) @END D;JEQ code block 1 @LOOP 0;JMP (END) code block 2 Hack: Hack convention: True is represented by -1 False is represented by 0

Side note Given the “multi-tasking” nature of the A register: ALU Mux D A/M a D register A register A M RAM (selected register) ROM PC Instruction Given the “multi-tasking” nature of the A register: Command pairs like @100 followed by D=M;JEQ 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.

Complete program example // Adds 1+...+100. into i = 1; into sum = 0; while (i <= 100){ sum += i; i++; } C: // Adds 1+...+100. @i // i refers to some memo. location M=1 // i=1 @sum // sum refers to some memo. 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 got END @sum M=D+M // sum += i M=M+1 // i++ @LOOP 0;JMP // Got LOOP (END) 0;JMP // Infinite loop Hack: Hack assembly convention: Variables: lower-case Labels: upper-case Commands: upper-case Demo CPU emulator

Lecture plan Symbolic machine language Binary machine language

A-instruction

C-instruction

Symbols (user-defined) D=M @INFINITE_LOOP D;JLE @counter M=D @SCREEN D=A @addr (LOOP) A=M M=-1 @32 D=D+A MD=M-1 @LOOP D;JGT (INFINITE_LOOP) 0;JMP 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 assigned a unique memory address by the assembler, starting at RAM address 16 By convention, label symbols are upper- case and variable symbols are lower-case.

Symbols (pre-defined) D=M @INFINITE_LOOP D;JLE @counter M=D @SCREEN D=A @addr (LOOP) A=M M=-1 @32 D=D+A MD=M-1 @LOOP D;JGT (INFINITE_LOOP) 0;JMP Virtual registers: R0,…, R15 are predefined to be 0,…,15 I/O pointers: The symbols SCREEN and KBD are predefined to be 16384 and 24576, respectively (base addresses of the screen and keyboard memory maps) Predefined pointers: the symbols SP, LCL, ARG, THIS, and THAT are predefined to be 0 to 4, respectively.

Perspective Hack is a simple machine language User friendly syntax: D=D+A instead of ADD D,D,A Hack is a “½-address machine”: it normally takes two commands to get something done: A-command to address, C-command to process A Macro-language can be easily developed A Hack assembler is needed and will be discusses and developed later in the course.

End-note: a macro machine language (optional, can be implemented rather easily) Assignment: x = constant (e.g. x = 17) x = y x = 0 , x = 1, x = -1 Arithmetic / logical: x = y op z where y, z are variables or constants and op is some ALU operation like +, -, and, or, etc. Control: GOTO s IF cond GOTO s where cond is an expression (x op y) {=|<|>|…} {0|1} e.g. IF x+17 > 0 goto loop White space or comments: White space: ignore // comment to the end of the line: ignore.