Boolean Arithmetic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 2

Similar documents
Arithmetic Logic Unit (ALU)

Boolean Arithmetic. Building a Modern Computer From First Principles.

Boolean Logic. From Nand to Tetris Building a Modern Computer from First Principles. Chapter 1

Elements of Computing Systems, Nisan & Schocken, MIT Press. Boolean Arithmetic

Introduction: From Nand to Tetris

Computer Architecture

Introduction: Hello, World Below

Computer Architecture

Building a Virtual Computer

Arithmetic-logic units

Assembler Human Thought

Basic Arithmetic (adding and subtracting)

Computer Architecture

Assembler. Building a Modern Computer From First Principles.

CSC 220: Computer Organization Unit 10 Arithmetic-logic units

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Binary Adders: Half Adders and Full Adders

Chapter 6: Assembler

Sequential Logic. Building a Modern Computer From First Principles.

Arithmetic and Bitwise Operations on Binary Data

Advanced Computer Architecture-CS501

Arithmetic Operations

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Von Neumann Architecture

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

THE LOGIC OF COMPOUND STATEMENTS

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Digital Logic Design Exercises. Assignment 1

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

CS140 Lecture 03: The Machinery of Computation: Combinational Logic

LECTURE 4. Logic Design

Motivation. Compiler. Our ultimate goal: Hack code. Jack code (example) Translate high-level programs into executable code. return; } } return

Computer Architecture and Organization: L04: Micro-operations

Number System. Introduction. Decimal Numbers

How a Digital Binary Adder Operates

ECE 2030B 1:00pm Computer Engineering Spring problems, 5 pages Exam Two 10 March 2010

Machine (Assembly) Language Human Thought

Digital Systems. John SUM Institute of Technology Management National Chung Hsing University Taichung, ROC. December 6, 2012

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Course overview. Introduction to Computer Yung-Yu Chuang. with slides by Nisan & Schocken (

Arithmetic Processing

Computer Organization and Levels of Abstraction

IWKS 3300: NAND to Tetris Spring John K. Bennett. Assembler

Computer Organization and Levels of Abstraction

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

COMBINATIONAL LOGIC CIRCUITS

Chapter 4 Arithmetic Functions

Chapter 10: Compiler I: Syntax Analysis

Arithmetic and Logical Operations

2. MACHINE REPRESENTATION OF TYPICAL ARITHMETIC DATA FORMATS (NATURAL AND INTEGER NUMBERS).

Chapter 4. Operations on Data

Machine (Assembly) Language

Number Systems and Computer Arithmetic

Software and Hardware

Arithmetic and Bitwise Operations on Binary Data

Boolean Algebra and Logic Gates

ECE 341 Midterm Exam

Combinational Circuits

DIGITAL ARITHMETIC: OPERATIONS AND CIRCUITS

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

Chapter 3: part 3 Binary Subtraction

Basic Definition INTEGER DATA. Unsigned Binary and Binary-Coded Decimal. BCD: Binary-Coded Decimal

ECEN 468 Advanced Logic Design

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

Verilog Dataflow Modeling

Chapter 3: Arithmetic for Computers

JAVA OPERATORS GENERAL

Register Transfer Language and Microoperations (Part 2)

Question Total Possible Test Score Total 100

CMPE223/CMSE222 Digital Logic Design. Positional representation

Binary Addition. Add the binary numbers and and show the equivalent decimal addition.

Digital Design with FPGAs. By Neeraj Kulkarni

14:332:231 DIGITAL LOGIC DESIGN. Hardware Description Languages

Unit 3. Operators. School of Science and Technology INTRODUCTION

Boolean logic. Boolean Algebra. Introduction to Computer Yung-Yu Chuang NOT AND NOT

Chapter 8: Virtual Machine II: Program Control

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Topic Notes: Bits and Bytes and Numbers

Organisasi Sistem Komputer

Computer Organization

Tailoring the 32-Bit ALU to MIPS

Numerical Representations On The Computer: Negative And Rational Numbers

ECE468 Computer Organization & Architecture. The Design Process & ALU Design

Register Transfer and Micro-operations

CS/COE 0447 Example Problems for Exam 2 Spring 2011

CDA 3103 Computer Organization Exam 1 (Sep. 22th, 2014)

Appendix A: Hardware Description Language (HDL) 1

COSC 243. Computer Architecture 1. COSC 243 (Computer Architecture) Lecture 6 - Computer Architecture 1 1

T02 Tutorial Slides for Week 2

EP1200 Introduction to Computing Systems Engineering. Computer Architecture

Extended Introduction to Computer Science CS1001.py. Lecture 5 Part A: Integer Representation (in Binary and other bases)

Compiler I: Sytnax Analysis

Topic Notes: Bits and Bytes and Numbers

Integer Representation

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Chapter 10 Binary Arithmetics

Transcription:

From Nand to Tetris Building a Modern Computer from First Principles Chapter 2 Boolean Arithmetic These slides support chapter 2 of the book The Elements of Computing Systems By Noam Nisan and Shimon Schocken MIT Press Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 1

Usage Notice You are welcome to use this presentation, or any part of it, in Nand to Tetris courses, or in other courses, as you see fit. Usage is free provided that it supports instruction in an educational, non-profit setting. Feel free to use the slides as-is, or modify them as needed. We ll appreciate it if you will give credit somewhere to Nand to Tetris, and put a reference to www.nand2tetris.org You are welcome to remove this slide from the presentation. If you make extensive changes to the slides, you can remove the copyright notice also. Happy teaching! Noam Nisam / Shimon Schocken Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 2

Chapter 2: Boolean arithmetic Binary numbers Binary addition Negative numbers Arithmetic Logic Unit Project 2 overview Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 3

0 1 00 01 10 11 3 bits 8 possibilities N bits 2 N possibilities Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 4

Representing numbers Binary Decimal 0 0 1 1 10 2 11 3 100 4 101 5... Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 5

Representing numbers 10 2 10 1 10 0 100s 10s 1s 7 8 9 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 6

Representing numbers 10 2 10 1 10 0 100s 10s 1s 7 8 9 ten = 7 10 2 + 8 10 1 + 9 10 0 = 789 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 7

Binary Decimal 2 2 2 1 2 0 4s 2s 1s 1 0 1 two Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 8

Binary Decimal 2 2 2 1 2 0 4s 2s 1s 1 0 1 two = 1 10 2 + 0 10 1 + 1 10 0 = 5 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 9

Binary Decimal Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 10

Binary Decimal b n b n-1 b n-2 b 1 b 0 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 11

Binary Decimal b n b n-1 b n-2 b 1 b 0 = n b i 2 i i=0 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 12

Binary Decimal b n b n-1 b n-2 b 1 b 0 = n b i 2 i i=0 Maximum value represented by k bits: 1 + 2 + 4 + + 2 k-1 = 2 k -1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 13

Fixed word size Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 14

Fixed word size We will use a fixed number of bits. Say 8 bits. 0000 0000 0000 0001 0000 0010 0000 0011 0111 1111 1000 0000 1000 0001 1111 1110 1111 1111 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 15

Fixed word size We will use a fixed number of bits. Say 8 bits. 0000 0000 0000 0001 0000 0010 0000 0011 0111 1111 1000 0000 1000 0001 1111 1110 1111 1111 2 8 = 256 values Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 16

Representing signed numbers We will use a fixed number of bits. Say 8 bits. 0000 0000 0000 0001 0000 0010 0000 0011 0111 1111 1000 0000 1000 0001 1111 1110 1111 1111 positive values negative values Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 17

Representing signed numbers We will use a fixed number of bits. Say 8 bits. 0000 0000 0000 0001 0000 0010 0000 0011 0111 1111 1000 0000 1000 0001 1111 1110 1111 1111 positive values That s one possible representation We ll use a better one, later negative values Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 18

Decimal Binary 87 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 19

Decimal Binary 87 ten =???????? two Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 20

Decimal Binary 87 ten = 64 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 21

Decimal Binary 87 ten = 64 + 16 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 22

Decimal Binary 87 ten = 64 + 16 + 4 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 23

Decimal Binary 87 ten = 64 + 16 + 4 + 2 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 24

Decimal Binary 87 ten = 64 + 16 + 4 + 2 + 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 25

Decimal Binary 87 ten = 64 + 16 + 4 + 2 + 1 =???????? two Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 26

Decimal Binary 87 ten = 64 + 16 + 4 + 2 + 1 = 0 1 0 1 0 1 1 1 two Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 27

Decimal Binary 87 ten = 64 + 16 + 4 + 2 + 1 = 0 1 0 1 0 1 1 1 two 32 8 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 28

Chapter 2: Boolean arithmetic Binary numbers Binary addition Negative numbers Arithmetic Logic Unit Project 2 overview Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 29

Boolean arithmetic Addition Subtraction Comparison (<,>,=) Multiplication Division implement get for free get for free postpone to software postpone to software Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 30

Addition + 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0???????? Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 31

Addition + 0 0 0 1 0 1 0 1 21 + 0 1 0 1 1 1 0 0 92 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 32

Addition + 0 0 0 1 0 1 0 1 21 + 0 1 0 1 1 1 0 0 92 113 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 33

Addition + 0 0 0 1 0 1 0 1 21 + 0 1 0 1 1 1 0 0 92 0 1 1 1 0 0 0 1 113 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 34

Addition Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 35

Addition + 5 7 8 3 2 4 5 6???? Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 36

Addition + 5 7 8 3 2 4 5 6 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 37

Addition + 5 7 8 3 2 4 5 6 9 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 38

Addition + 1 5 7 8 3 2 4 5 6 3 9 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 39

Addition + 1 1 5 7 8 3 2 4 5 6 2 3 9 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 40

Addition + 1 1 5 7 8 3 2 4 5 6 8 2 3 9 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 41

Addition Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 42

Addition + 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0???????? Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 43

Addition + 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 44

Addition + 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 45

Addition + 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 46

Addition + 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 47

Addition + 1 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 48

Addition + 1 1 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 1 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 49

Addition + 1 1 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 1 1 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 50

Addition + 1 1 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 1 1 1 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 51

Addition + 1 1 1 0 0 0 1 0 1 0 1 0 1 0 1 1 1 0 0 0 1 1 1 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 52

Overflow Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 53

Overflow + 1 0 0 1 0 1 0 1 1 1 0 1 1 1 0 0???????? Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 54

Overflow 1 0 0 0 1 1 1 0 0 + 1 0 0 1 0 1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 1 0 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 55

Building an Adder Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 56

Building an Adder + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 Half adder: adds two bits Full adder: adds three bits Adder: adds two integers Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 57

Half adder carry bit a b sum carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 sum bit Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 58

Half adder a b sum carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 HalfAdder.hdl /** Computes the sum of two bits. */ CHIP HalfAdder { IN a, b; OUT sum, carry; } PARTS: // Put your code here: Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 59

Full adder Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 60

Full adder + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 61

Full adder carry bit + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 sum bit Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 62

Full adder FullAdder.hdl /** Computes the sum of three bits. */ CHIP HalfAdder { IN a, b, c; OUT sum, carry; } PARTS: // Put your code here: Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 63

Multi-bit Adder Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 64

Multi-bit Adder + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 65

Multi-bit Adder + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 66

Multi-bit Adder + 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 1 Add16.hdl /* Adds two 16-bit, two s-complement values. * The most-significant carry bit is ignored. */ CHIP Add16 { IN a[16], b[16]; OUT out[16]; } PARTS: // Put you code here: Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 67

Chapter 2: Boolean arithmetic Binary numbers Binary addition Negative numbers Arithmetic Logic Unit Project 2 overview Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 68

Representing numbers (using 4 bits) 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 69

Representing numbers (using 4 bits) 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15 Using n bits, we can represent the positive integers in the range 0 2 n -1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 70

Representing negative numbers 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 71

Possible solution: use a sign bit 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000-0 1001-1 1010-2 1011-3 1100-4 1101-5 1110-6 1111-7 Use the left-most bit to represent the sign, -/+ Use the remaining bits to represent a positive number Complications: -0? x + (-x) is not 0 more complications Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 72

Two's Complement 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Represent the negative number -x using the positive number 2 n - x Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 73

Two's Complement 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000-8 (16-8) 1001-7 (16-9) 1010-6 (16-10) 1011-5 (16-11) 1100-4 (16-12) 1101-3 (16-13) 1110-2 (16-14) 1111-1 (16-15) Represent the negative number -x using the positive number 2 n - x Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 74

Two's Complement 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000-8 (16-8) 1001-7 (16-9) 1010-6 (16-10) 1011-5 (16-11) 1100-4 (16-12) 1101-3 (16-13) 1110-2 (16-14) 1111-1 (16-15) positive numbers range: 0 2 n-1-1 negative numbers range: -1-2 n - 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 75

Addition using two's complement + -2-3 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 76

Addition using two's complement + -2-3 + 14 13 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 77

Addition using two's complement + -2-3 + 14 13 + 1110 1101 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 78

Addition using two's complement + -2-3 + 14 13 + 1110 1101 11011 11011 = 27 ten 1011 = 11 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 79

Addition using two's complement + -2-3 + 14 13 + 1110 1101 11 11011 11011 = 27 ten 1011 = 27 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 80

Addition using two's complement + -2-3 + 14 13 + 1110 1101-5 11 11011 Two s complement rationale: representation is modulu 2 n addition is modulu 2 n 11011 = 27 ten 1011 = 27 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 81

Computing -x Input: x Output: -x (in two's complement) Insight: if we solve this we ll know how to subtract: y x = y + (-x) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 82

Computing -x Input: x Output: -x (in two's complement) Idea: 2 n x = 1 + (2 n - 1) - x 11111111 two 11111111-10101100 (some x example) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 83

Computing -x Input: x Output: -x (in two's complement) Idea: 2 n x = 1 + (2 n - 1) - x 11111111 two 11111111-10101100 (some x example) 01010011 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 84

Computing -x Input: x Output: -x (in two's complement) Idea: 2 n x = 1 + (2 n - 1) - x 11111111 two 11111111-10101100 (some x example) 01010011 (flip all the bits) Now add 1 to the result Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 85

Computing -x (example) Input: 4 Output: should be 12 (representing -4 in two s compelemt) Input: 0100 Flip the bits: 1011 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 86

Computing -x (example) Input: 4 Output: should be 12 (representing -4 in two s compelemt) Input: 0100 Flip the bits: 1011 + Add one: 1 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 87

Computing -x (example) Input: 4 Output: should be 12 (representing -4 in two s compelemt) Input: 0100 Flip the bits: 1011 + Add one: 1 Output: 1100 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 88

Computing -x (example) Input: 4 Output: should be 12 (representing -4 in two s compelemt) Input: 0100 Flip the bits: 1011 + Add one: 1 To add 1: Flip all the bits from right to left, stop when the first 0 flips to 1 Output: 1100 = 12 ten Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 89

Chapter 2: Boolean arithmetic Binary numbers Binary addition Negative numbers Arithmetic Logic Unit Project 2 overview Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 90

Von Neumann Architecture Computer System CPU Input Memory ALU Output Contro l Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 91

The Arithmetic Logical Unit Computer System CPU Input Memory ALU Output Contro l Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 92

The Arithmetic Logical Unit The ALU computes a function on the two inputs, and outputs the result input1 f f : one out of a family of pre-defined arithmetic and logical functions input2 ALU f (input1, input2) Arithmetic functions: integer addition, multiplication, division,... logical functions: And, Or, Xor, Which functions should the ALU perform? A hardware / software tradeoff. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 93

The Hack ALU Operates on two 16-bit, two s complement values Outputs a 16-bit, two s complement value Which function to compute is set by six 1-bit inputs out 0 Computes one out of a family of 18 functions 1 Also outputs two 1-bit values (to be discussed later). -1 x y zx nx zy ny f no!x!y -x -y x+1 y+1 x 16 bits x-1 ALU out y-1 y 16 bits x+y 16 bits x-y y-x x&y x y zr ng Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 94

The Hack ALU out 0 1-1 x y zx nx zy ny f no!x!y -x -y x+1 y+1 x 16 bits x-1 ALU out y-1 y 16 bits x+y 16 bits x-y y-x x&y x y zr ng Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 95

The Hack ALU x y To cause the ALU to compute a function, set the control bits to one of the binary combinations listed in the table. control bits zx nx zy ny f no out 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0-1 0 0 1 1 0 0 x 1 1 0 0 0 0 y zx nx zy ny f no 0 0 1 1 0 1!x 1 1 0 0 0 1!y 0 0 1 1 1 1 -x 1 1 0 0 1 1 -y 0 1 1 1 1 1 x+1 1 1 0 1 1 1 y+1 16 bits 0 0 1 1 1 0 x-1 ALU out 1 1 0 0 1 0 y-1 16 bits 0 0 0 0 1 0 x+y 16 bits 0 1 0 0 1 1 x-y 0 0 0 1 1 1 y-x 0 0 0 0 0 0 x&y 0 1 0 1 0 1 x y zr ng Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 96

The Hack ALU in action: compute y-x 2. Evaluate the chip logic Load tools/builtinchips/alu.hdl 3. Inspect the ALU outputs 1. Set the ALU s inputs and control bits to some test values (000111 codes output y-x ) The built-in ALU implementation has some GUI side-effects Built-in ALU implementation Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 97

The Hack ALU in action: compute x & y Set to binary I/O format Inspect the ALU outputs Set the ALU s inputs and control bits to some test values (000000 codes compute x&y ) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 98

Opening up the Hack ALU black box control bits x y out = f(control bits, x, y) Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 99

The Hack ALU operation pre-setting the x input pre-setting the y input selecting between computing + or & post-setting the output Resulting ALU output zx nx zy ny f no out if zx then x=0 if nx then x=!x if zy then y=0 if ny then y=!y if f then out=x+y else out=x&y if no then out=!out out(x,y)= zx nx zy ny f no x y 16 bits 16 bits ALU 16 bits out zr ng Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 100

The Hack ALU operation pre-setting the x input pre-setting the y input selecting between computing + or & post-setting the output Resulting ALU output zx nx zy ny f no out if zx then x=0 if nx then x=!x if zy then y=0 if ny then y=!y if f then out=x+y else out=x&y if no then out=!out out(x,y)= 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0-1 0 0 1 1 0 0 x 1 1 0 0 0 0 y 0 0 1 1 0 1!x 1 1 0 0 0 1!y 0 0 1 1 1 1 -x 1 1 0 0 1 1 -y 0 1 1 1 1 1 x+1 1 1 0 1 1 1 y+1 0 0 1 1 1 0 x-1 1 1 0 0 1 0 y-1 0 0 0 0 1 0 x+y 0 1 0 0 1 1 x-y 0 0 0 1 1 1 y-x 0 0 0 0 0 0 x&y 0 1 0 1 0 1 x y Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 101

ALU operation example: compute!x pre-setting the x input pre-setting the y input selecting between computing + or & post-setting the output Resulting ALU output zx nx zy ny f no out if zx then x=0 if nx then x=!x if zy then y=0 if ny then y=!y if f then out=x+y else out=x&y if no then out=!out out(x,y)= 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0-1 0 0 1 1 0 0 x 1 1 0 0 0 0 y 0 0 1 1 0 1!x 1 1 0 0 0 1!y Example: compute!x 0 0 1 1 1 1 -x 1 1 0 x: 0 1 1 01 0 1 -y 0 1 1 y: 1 1 0 1 1 1 x+1 1 1 0 1 1 1 y+1 0 0 1 Following 1 pre-setting: 1 0 x-1 1 1 0 x: 0 1 1 01 0 0 y-1 0 0 0 y: 0 1 1 1 1 0 x+y 0 1 0 0 1 1 x-y 0 0 0 Computation 1 and 1 post-setting: 1 y-x 0 0 0 x&y: 0 1 1 0 0 0 x&y 0 1 0!(x&y): 1 0 0 10 1 (!x) 1 x y Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 102

ALU operation example: compute y-x pre-setting the x input pre-setting the y input selecting between computing + or & post-setting the output Resulting ALU output zx nx zy ny f no out if zx then x=0 if nx then x=!x if zy then y=0 if ny then y=!y if f then out=x+y else out=x&y if no then out=!out out(x,y)= 1 0 1 0 1 0 0 1 1 1 1 1 1 1 Example: 1 compute 1 y-x 1 0 1 0-1 0 0 1 1 0 0 x x: 0 0 1 0 (2) y: 1 0 1 1 1 1 0 (7) 0 0 0 y 0 0 1 1 0 1!x Following 1 pre-setting: 1 0 0 0 1!y 0 0 1 1 1 1 -x x: 0 0 1 0 1 1 0 0 1 1 -y y: 1 0 0 0 0 1 1 1 1 1 x+1 1 1 0 1 1 1 y+1 Computation and post-setting: 0 0 1 1 1 0 x-1 x+y: 1 110 1 0 0 0 1 0 y-1!(x+y): 0 001 0 1 0(5) 0 1 0 x+y 0 1 0 0 1 1 x-y 0 0 0 1 1 1 y-x 0 0 0 0 0 0 x&y 0 1 0 1 0 1 x y Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 103

The Hack ALU output control bits zx nx zy ny f no x y 16 bits 16 bits ALU 16 bits out zr ng if (out == 0) then zr = 1, else zr = 0 if (out < 0) then ng = 1, else ng = 0 These two control bits will come into play when we build the complete computer s architecture. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 104

Perspective The Hack ALU is: Simple Elegant To implement it this ALU, you only need to know how to: Set a 16-bit value to 0000000000000000 Set a 16-bit value to 1111111111111111 Negate a 16-bit value (bit-wise) Compute plus or And on two 16-bit values That s it! Simplicity is the ultimate sophistication. Leonardo da Vinci Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 105

Chapter 2: Boolean arithmetic Binary numbers Binary addition Negative numbers Arithmetic Logic Unit Project 2 overview Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 106

Project 2 Given: All the chips built in Project 1 Goal: Build the following chips: HalfAdder FullAdder Add16 Inc16 ALU A family of combinational chips, from simple adders to an Arithmetic Logic Unit. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 107

Half Adder a b sum carry 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 HalfAdder.hdl /** Computes the sum of two bits. */ CHIP HalfAdder { IN a, b; OUT sum, carry; Implementation tip Can be built using two very elementary gates. } PARTS: // Put your code here: Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 108

Full Adder a b c sum carry 0 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 FullAdder.hdl /** Computes the sum of three bits. */ CHIP HalfAdder { IN a, b, c; OUT sum, carry; } PARTS: // Put your code here: 1 1 0 0 1 1 1 1 1 1 Implementation tips Can be built using two half-adders. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 109

16-bit adder Add16.hdl Implementation tips An n-bit adder can be built from n full-adder chips The carry bit is piped from right to left The MSB carry bit is ignored. /* * Adds two 16-bit, two s-complement values. * The most-significant carry bit is ignored. */ CHIP Add16 { IN a[16], b[16]; OUT out[16]; } PARTS: // Put you code here: Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 110

16-bit incrementor Implementation tip The single-bit 0 and 1 values are represented in HDL as false and true. Inc16.hdl /* * Outputs in + 1. * The most-significant carry bit is ignored. */ CHIP Inc16 { IN in[16]; OUT out[16]; } PARTS: // Put you code here: Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 111

ALU Implementation tips Building blocks: Add16, and some gates built in project 1 Can be built with ~20 lines of HDL code ALU.hdl /** The ALU. */ // Manipulates the x and y inputs as follows: // if (zx == 1) sets x = 0 // 16-bit true constant // if (nx == 1) sets x =!x // bitwise Not // if (zy == 1) sets y = 0 // 16-bit true constant // if (ny == 1) sets y =!y // bitwise Not // if (f == 1) sets out = x + y // int. 2's-complement addition // if (f == 0) sets out = x & y // bitwise And // if (no == 1) sets out =!out // bitwise Not // if (out == 0) sets zr = 1 // 1-bit true constant // if (out < 0) sets ng = 1 // 1-bit true constant... Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 112

Project 2 Resources www.nand2tetris.org All the necessary project 2 files are available in: nand2tetris / projects / 02 Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 113

More resources HDL Survival Guide Hardware Simulator Tutorial nand2tetris Q&A forum All available in: www.nand2tetris.org Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 114

Best practice advice Try to implement the chips in the given order If you don t implement some of the chips required in project 2, you can still use them as chip-parts in other chips. Just rename the given stub-files; this will cause the simulator to use the built-in versions of these chips You can invent new, helper chips ; however, this is not required: you can build any chip using previously-built chips only Strive to use as few chip-parts as possible You will have to use chips implemented in Project 1 For efficiency and consistency s sake, use their built-in versions rather than your own implementation. Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 115

Chapter 2: Boolean arithmetic Binary numbers Binary addition Negative numbers Arithmetic Logic Unit Project 2 overview Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 116

From Nand to Tetris Building a Modern Computer from First Principles Chapter 2 Boolean Arithmetic These slides support chapter 2 of the book The Elements of Computing Systems By Noam Nisan and Shimon Schocken MIT Press Nand to Tetris / www.nand2tetris.org / Chapter 2 / Copyright Noam Nisan and Shimon Schocken Slide 117