Numbers and Arithmetic

Similar documents
Numbers and Arithmetic

Numbers & Arithmetic. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See: P&H Chapter , 3.2, C.5 C.

Hakim Weatherspoon CS 3410 Computer Science Cornell University

State and Finite State Machines

State & Finite State Machines

State & Finite State Machines

ECE380 Digital Logic. Positional representation

Floating Point Representation and Digital Logic. Lecture 11 CS301

Schedule. ECEN 301 Discussion #25 Final Review 1. Date Day Class No. 1 Dec Mon 25 Final Review. Title Chapters HW Due date. Lab Due date.

We are here. Assembly Language. Processors Arithmetic Logic Units. Finite State Machines. Circuits Gates. Transistors

Fundamentals of Digital Design

Binary addition example worked out

Combinational Logic. By : Ali Mustafa

14:332:231 DIGITAL LOGIC DESIGN. Why Binary Number System?

Hardware Design I Chap. 4 Representative combinational logic

CMP 334: Seventh Class

Sample Test Paper - I

ECE 341. Lecture # 3

ECE 545 Digital System Design with VHDL Lecture 1. Digital Logic Refresher Part A Combinational Logic Building Blocks

CprE 281: Digital Logic

UNSIGNED BINARY NUMBERS DIGITAL ELECTRONICS SYSTEM DESIGN WHAT ABOUT NEGATIVE NUMBERS? BINARY ADDITION 11/9/2018

Number System. Decimal to binary Binary to Decimal Binary to octal Binary to hexadecimal Hexadecimal to binary Octal to binary

CS61C : Machine Structures

CS61C : Machine Structures

Boolean Algebra and Digital Logic 2009, University of Colombo School of Computing

DE58/DC58 LOGIC DESIGN DEC 2014

Design of Sequential Circuits

Design of Digital Circuits Reading: Binary Numbers. Required Reading for Week February 2017 Spring 2017

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: Digital Logic

Lecture 2 Review on Digital Logic (Part 1)

Chapter 1 CSCI

14:332:231 DIGITAL LOGIC DESIGN. 2 s-complement Representation

Lecture 7: Logic design. Combinational logic circuits

ENGIN 112 Intro to Electrical and Computer Engineering

Digital Techniques. Figure 1: Block diagram of digital computer. Processor or Arithmetic logic unit ALU. Control Unit. Storage or memory unit

University of Florida EEL 3701 Fall 2014 Dr. Eric. M. Schwartz Department of Electrical & Computer Engineering Wednesday, 15 October 2014

E40M. Binary Numbers. M. Horowitz, J. Plummer, R. Howe 1

CpE358/CS381. Switching Theory and Logical Design. Summer

CHAPTER 2 NUMBER SYSTEMS

Why digital? Overview. Number Systems. Binary to Decimal conversion

CPE100: Digital Logic Design I

Digital Circuits ECS 371

ELEN Electronique numérique

Menu. Review of Number Systems EEL3701 EEL3701. Math. Review of number systems >Binary math >Signed number systems

Cs302 Quiz for MID TERM Exam Solved

Chapter 4. Combinational: Circuits with logic gates whose outputs depend on the present combination of the inputs. elements. Dr.

CSE 241 Digital Systems Spring 2013

Conversions between Decimal and Binary

COMBINATIONAL LOGIC FUNCTIONS

of Digital Electronics

Systems I: Computer Organization and Architecture

Review Problem 1. should be on. door state, false if light should be on when a door is open. v Describe when the dome/interior light of the car

Exam for Physics 4051, October 31, 2008

ECE/CS 250 Computer Architecture

ECE 2300 Digital Logic & Computer Organization

ECE 250 / CPS 250 Computer Architecture. Basics of Logic Design Boolean Algebra, Logic Gates

ELCT201: DIGITAL LOGIC DESIGN

Chapter 7 Logic Circuits

UNIT II COMBINATIONAL CIRCUITS:

ENG2410 Digital Design Introduction to Digital Systems. Fall 2017 S. Areibi School of Engineering University of Guelph

CPE100: Digital Logic Design I

COSC 243. Introduction to Logic And Combinatorial Logic. Lecture 4 - Introduction to Logic and Combinatorial Logic. COSC 243 (Computer Architecture)

Numbering Systems. Contents: Binary & Decimal. Converting From: B D, D B. Arithmetic operation on Binary.

MATH Dr. Halimah Alshehri Dr. Halimah Alshehri

Unit II Chapter 4:- Digital Logic Contents 4.1 Introduction... 4

CHAPTER 7. Exercises 17/ / /2 2 0

EECS150 - Digital Design Lecture 24 - Arithmetic Blocks, Part 2 + Shifters

Logic and Computer Design Fundamentals. Chapter 5 Arithmetic Functions and Circuits

CprE 281: Digital Logic

CSE140: Components and Design Techniques for Digital Systems. Decoders, adders, comparators, multipliers and other ALU elements. Tajana Simunic Rosing

Binary addition (1-bit) P Q Y = P + Q Comments Carry = Carry = Carry = Carry = 1 P Q

CHW 261: Logic Design

Combinational Logic Design Arithmetic Functions and Circuits

A crash course in Digital Logic

3. Complete the following table of equivalent values. Use binary numbers with a sign bit and 7 bits for the value

EE260: Digital Design, Spring n Digital Computers. n Number Systems. n Representations. n Conversions. n Arithmetic Operations.

Appendix B. Review of Digital Logic. Baback Izadi Division of Engineering Programs

CSE 20 DISCRETE MATH. Fall

CprE 281: Digital Logic

CSE 140L Spring 2010 Lab 1 Assignment Due beginning of the class on 14 th April

SAU1A FUNDAMENTALS OF DIGITAL COMPUTERS

Logic. Combinational. inputs. outputs. the result. system can

Save from: cs. Logic design 1 st Class أستاذ المادة: د. عماد

LOGIC CIRCUITS. Basic Experiment and Design of Electronics

CHAPTER1: Digital Logic Circuits Combination Circuits

( c) Give logic symbol, Truth table and circuit diagram for a clocked SR flip-flop. A combinational circuit is defined by the function

14:332:231 DIGITAL LOGIC DESIGN

CMSC 313 Lecture 17. Focus Groups. Announcement: in-class lab Thu 10/30 Homework 3 Questions Circuits for Addition Midterm Exam returned

Gates and Logic: From switches to Transistors, Logic Gates and Logic Circuits

CSE 20 Discrete Math. Algebraic Rules for Propositional Formulas. Summer, July 11 (Day 2) Number Systems/Computer Arithmetic Predicate Logic

Adders, subtractors comparators, multipliers and other ALU elements

Week No. 06: Numbering Systems

Total Time = 90 Minutes, Total Marks = 100. Total /10 /25 /20 /10 /15 /20

Digital Logic Appendix A

Digital Logic: Boolean Algebra and Gates. Textbook Chapter 3

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING CS6201 DIGITAL PRINCIPLES AND SYSTEM DESIGN

Boolean Algebra. Digital Logic Appendix A. Postulates, Identities in Boolean Algebra How can I manipulate expressions?

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) State any two Boolean laws. (Any 2 laws 1 mark each)

ENEL Digital Circuit Design. Final Examination

ALU (3) - Division Algorithms

Transcription:

Numbers and Arithmetic See: P&H Chapter 2.4 2.6, 3.2, C.5 C.6 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Big Picture: Building a Processor memory inst register file alu PC +4 +4 new pc offset target imm control extend =? cmp addr d in d out memory A Single cycle processor

Binary Operations Goals for Today Number representations One bit and four bit adders Negative numbers and two s compliment Addition (two s compliment) Subtraction (two s compliment) Performance Example Build a circuit (e.g. voting machine) Building blocks (encoders, decoders, multiplexors)

Recall: Binary Number Representations Two symbols (base 2): true and false; 0and 1 Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)?

Recall: Binary Number Representations Two symbols (base 2): true and false; 1and 0 Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)? We know represent numbers in Decimal (base 10). E.g. 6 3 7 10 2 10 1 10 0 Can just as easily use other bases Base 2 Binary Base 8 Octal Base 16 Hexadecimal 1 0 0 1 1 1 1 1 0 1 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0o 1 1 7 5 0 8 3 8 2 8 1 8 0 0x 2 7 d 16 2 16 1 16 0

Recall: Binary Number Representations Two symbols (base 2): true and false; 1and 0 Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)? We know represent numbers in Decimal (base 10). E.g. 6 3 7 10 2 10 1 10 0 6 10 2 + 3 10 1 + 7 10 0 = 637 Can just as easily use other bases Base 2 Binary 1 2 9 +1 2 6 +1 2 5 +1 2 4 +1 2 3 +1 2 2 +1 2 0 = 637 Base 8 Octal1 8 3 + 1 8 2 + 7 8 1 + 5 8 0 = 637 2 16 Base 16 Hexadecimal 2 + 7 16 1 + d 16 0 = 637 2 16 2 + 7 16 1 + 13 16 0 = 637

Number Representations: Activity #1 Counting How do we count in different bases? Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18.. 99 100 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 1 0000 1 0001 1 0010.. 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22.. 0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12..

Number Representations How to convert a number between different bases? Base conversion via repetitive division Divide by base, write remainder, move left with quotient 637 10 = 63 remainder 7 63 10 = 6 remainder 3 6 10 = 0 remainder 6 lsb (least significant bit) msb (most significant bit)

Number Representations Convert a base 10 number to a base 2 number Base conversion via repetitive division Divide by base, write remainder, move left with quotient lsb (least significant bit) 637 2 = 318 remainder 1 318 2 = 159 remainder 0 159 2 = 79 remainder 1 79 2 = 39 remainder 1 39 2 = 19 remainder 1 19 2 = 9 remainder 1 9 2 = 4 remainder 1 4 2 = 2 remainder 0 2 2 = 1 remainder 0 1 2 = 0 remainder 1 msb (most significant bit) 637 =10 0111 1101 (can also be written as 0b10 0111 1101) msb lsb

Number Representations Convert a base 10 number to a base 16 number Base conversion via repetitive division Divide by base, write remainder, move left with quotient lsb 637 16 = 39 remainder 13 39 16 = 2 remainder 7 2 16 = 0 remainder 2 637 = 0x 2 7 13 = 0x? 2 7 d Thus, 637 = 0x27d msb dec = hex 10 = 0xa 11 = 0xb 12 = 0xc 13 = 0xd 14 = 0xe 15 = 0xf = bin = 1010 = 1011 = 1100 = 1101 = 1110 = 1111

Number Representations Convert a base 2 number to base 8 (oct) or 16 (hex) Binary to Hexadecimal Convert each nibble (group of four bits) from binary to hex A nibble (four bits) ranges in value from 0 15, which is one hex digit Range: 0000 1111 (binary) => 0x0 0xF (hex) => 0 15 (decimal) E.g. 0b10 0111 1101 0b10 =0x2 0b0111 = 0x7 0b1101 = 0xd Thus, 637 = 0x27d =0b10 0111 1101 Binary to Octal Convert each group of three bits from binary to oct Three bits range in value from 0 7, which is one octal digit Range: 0000 1111 (binary) => 0x0 0xF (hex) => 0 15 (decimal) E.g. 0b1 001 111 101 0b1 =0x1 0b001 = 0x1 0b111 = 0x7 0b101 = 0x5 Thus, 637 = 0o1175 =0b10 0111 1101

Recall: Binary Number Representations Two symbols (base 2): true and false; 0and 1 Basis of Logic Circuits and all digital computers So, how do we represent numbers in Binary (base 2)? We know represent numbers in Decimal (base 10). E.g. 6 3 7 10 2 10 1 10 0 Can just as easily use other bases Base 2 Binary Base 8 Octal Base 16 Hexadecimal 1 0 0 1 1 1 1 1 0 1 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0o 1 1 7 5 0 8 3 8 2 8 1 8 0 0x 2 7 d 16 2 16 1 16 0

Takeaway Digital computers are implemented via logic circuits and thus represent all numbers in binary (base 2). We (humans) often write numbers as decimal and hexadecimal for convenience, so need to be able to convert to binary and back (to understand what computer is doing!).

Next Goal Binary Arithmetic: Add and Subtract two binary numbers

Binary Addition How do we do arithmetic in binary? 183 + 254 437 Addition works the same way regardless of base Add the digits in each position Propagate the carry 001110 + 011100 101010 Unsigned binary addition is pretty easy Combine two bits at a time Along with a carry

C out A S B 1 bit Adder Half Adder Adds two 1 bit numbers Computes 1 bit result and 1 bit carry No carry in A B C out S 0 0 0 1 1 0 1 1

C out A S B A B C in C out S 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 1 bit Adder with Carry C in Full Adder Adds three 1 bit numbers Computes 1 bit result and 1 bit carry Can be cascaded Activity: Truth Table and Sum of Product. Logic minimization via Karnaugh Maps and algebraic minimization. Draw Logic Circuits

C out A[4] B[4] S[4] C in 4 bit Adder 4 Bit Full Adder Adds two 4 bit numbers and carry in Computes 4 bit result and carry out Can be cascaded

4 bit Adder A 3 B 3 A 2 B 2 A 1 B 1 A 0 B 0 C out C in S 3 S 2 S 1 S 0 Adds two 4 bit numbers, along with carry in Computes 4 bit result and carry out Carry out = overflow indicates result does not fit in 4 bits

Takeaway Digital computers are implemented via logic circuits and thus represent all numbers in binary (base 2). We (humans) often write numbers as decimal and hexadecimal for convenience, so need to be able to convert to binary and back (to understand what computer is doing!). Adding two 1 bit numbers generalizes to adding two numbers of any size since 1 bit full adders can be cascaded.

Next Goal How do we subtract two binary numbers? Equivalent to adding with a negative number How do we represent negative numbers?

First Attempt: Sign/Magnitude Representation First Attempt: Sign/Magnitude Representation 1 bit for sign (0=positive, 1=negative) N 1 bits for magnitude Problem? Two zero s: +0 different than 0 Complicated circuits IBM 7090

Two s Complement Representation Better: Two s Complement Representation Nonnegative numbers are represented as usual 0 = 0000 1 = 0001 3 = 0011 7 = 0111 Leading 1 s for negative numbers To negate any number: complement all the bits (i.e. flip all the bits) then add 1 1: 1 0001 1110 1111 3: 3 0011 1100 1101 7: 7 0111 1000 1001 8: 8 1000 0111 1000 0: 0 0000 1111 0000 (this is good, 0 = +0)

Two s Complement Non negatives (as usual): +0 = 0000 +1 = 0001 +2 = 0010 +3 = 0011 +4 = 0100 +5 = 0101 +6 = 0110 +7 = 0111 +8 = 1000 Negatives (two s complement: flip then add 1): ~0 = 1111 0 = 0000 ~1 = 1110 1 = 1111 ~2 = 1101 2 = 1110 ~3 = 1100 3 = 1101 ~4 = 1011 4 = 1100 ~5 = 1010 5 = 1011 ~3 = 1001 6 = 1010 ~7 = 1000 7 = 1001 ~8 = 0111 8 = 1000

Two s Complement Facts Signed two s complement Negative numbers have leading 1 s zero is unique: +0 = 0 wraps from largest positive to largest negative N bits can be used to represent unsigned: range 0 2 N 1 eg: 8 bits 0 255 signed (two s complement): (2 N 1 ) (2 N 1 1) ex: 8 bits (1000 000) (0111 1111) 127 128

Sign Extension & Truncation Extending to larger size 1111 = 1 1111 1111 = 1 0111 = 7 0000 0111 = 7 Truncate to smaller size 0000 1111 = 15 BUT, 0000 1111 = 1111 = 1

Two s Complement Addition Addition with two s complement signed numbers Perform addition as usual, regardless of sign (it just works) Examples 1 + 1 = 0001 + 1111 = 0000 (0) 3 + 1 = 1101 + 1111 = 1100 ( 4) 7 + 3 = 1001 + 0011 = 1100 ( 4) 7 + ( 3) = 0111 + 1101 = 0100 (4) What is wrong with the following additions? 7 + 1, 7 + 3, 7 + 1

Binary Subtraction Two s Complement Subtraction Why create a new circuit? Just use addition How?

Binary Subtraction Two s Complement Subtraction Subtraction is simply addition, where one of the operands has been negated Negation is done by inverting all bits and adding one A B = A + ( B) = A + (B + 1) B 3 B 2 B 1 B 0 A 3 A 2 A 1 A 0 C out 1 S 3 S 2 Q: How do we detect and handle overflows? Q: What if ( B) overflows? S 1 S 0

Takeaway Digital computers are implemented via logic circuits and thus represent all numbers in binary (base 2). We (humans) often write numbers as decimal and hexadecimal for convenience, so need to be able to convert to binary and back (to understand what computer is doing!). Adding two 1 bit numbers generalizes to adding two numbers of any size since 1 bit full adders can be cascaded. Using Two s complement number representation simplifies adder Logic circuit design (0 is unique, easy to negate). Subtraction is simply adding, where one operand is negated (two s complement; to negate just flip the bits and add 1)..

Next Goal How do we detect and handle overflow?

Overflow When can overflow occur? adding a negative and a positive? adding two positives? adding two negatives?

Takeaway Digital computers are implemented via logic circuits and thus represent all numbers in binary (base 2). We (humans) often write numbers as decimal and hexadecimal for convenience, so need to be able to convert to binary and back (to understand what computer is doing!). Adding two 1 bit numbers generalizes to adding two numbers of any size since 1 bit full adders can be cascaded. Using Two s complement number representation simplifies adder Logic circuit design (0 is unique, easy to negate). Subtraction is simply adding, where one operand is negated (two s complement; to negate just flip the bits and add 1). Overflow if sign of operands A and B!= sign of result S. Can detect overflow by testing C in!= C out of the most significant bit (msb), which only occurs when previous statement is true.

A Calculator A 8 B 8 8 mux 8 adder 8 decoder 8 S 0=add 1=sub

Performance Next Goal

Efficiency and Generality Is this design fast enough? Can we generalize to 32 bits? 64? more? A 3 B 3 A 2 B 2 A 1 B 1 A 0 B 0 C 0 R 3 R 2 R 1 R 0

Performance Speed of a circuit is affected by the number of gates in series (on the critical path or the deepest level of logic) inputs arrive Combinational Logic outputs expected t combinational

4 bit Ripple Carry Adder A 3 B 3 A 2 B 2 A 1 B 1 A 0 B 0 C 4 C 3 C 2 C 1 C 0 S 3 S 2 S 1 Carry ripples from lsb to msb S 0 First full adder, 2 gate delay Second full adder, 2 gate delay

Binary Operations Goals for Today Number representations One bit and four bit adders Negative numbers and two s compliment Addition (two s compliment) Subtraction (two s compliment) Performance Example Build a circuit (e.g. voting machine) Building blocks (encoders, decoders, multiplexors)

Voting machine For now, let s just display the numerical identifier to the ballot supervisor we won t do counting yet, just decoding we can use four photo sensitive transistors to find out which hole is punched out A photo-sensitive transistor detects the presence of light Photo-sensitive material triggers the gate

Ballot Reading Input: paper with a hole in it Output: number the ballot supervisor can record Ballots The 3410 optical scan vote counter reader machine

Input Photo sensitive transistor photons replenish gate depletion region can distinguish dark and light spots on paper Vdd i0 i1 i2 i3 i4 i5 i6 Use array of N sensors for voting machine input

7 Segment LED photons emitted when electrons fall into holes Output d7 d6 d5 d4 d3 d2 d1 d0

Block Diagram detect N 8

0 1 2 Encoders N might be large Routing wires is expensive More efficient encoding? 3 4 5 6 7... N encoder...

Encoder Truth Table a 1 b 2 o 0 o 1 c 3 o 2 d 4 A 3-bit encoder with 4 inputs for simplicity

Encoder Truth Table a 1 a b c d o2 o1 o0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 b 2 o 0 0 1 0 0 0 1 0 0 0 1 0 0 1 1 o 1 0 0 0 1 1 0 0 c 3 o 2 d 4 o2 = abcd A 3-bit encoder with 4 inputs for simplicity o1 = abcd + abcd o0 = abcd + abcd

Ballot Reading detect enc 8 3 8

Ballot Reading Ok, we built first half of the machine Need to display the result Ballots The 3410 optical scan vote counter reader machine

7 Segment LED Decoder 7LED decode 3 inputs encode 0 7 in binary 7 outputs one for each LED

7 Segment LED Decoder Implementation b2 b1 b0 d6 d5 d4 d3 d2 d1 d0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 1 1 0 1 1 1 d1 d2 d0 d3 d4 d6 d5

7 Segment LED Decoder Implementation b2 b1 b0 d6 d5 d4 d3 d2 d1 d0 0 0 0 1 1 1 0 1 1 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 1 1 0 1 1 0 1 1 1 1 0 1 0 1 1 1 0 0 1 0 0 1 1 0 1 1 0 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 0 0 0 1 1 d1 d2 d0 d3 d4 d6 d5

Ballot Reading and Display detect enc 7LED decode 8 3 7 Ballots The 3410 optical scan vote counter reader machine

Building Blocks 2 N binary encoder N N binary decoder 2 N N N N... N 0 1 2 Multiplexor 2 M 1 N M

Administrivia Make sure you are Registered for class, can access CMS Have a Section you can go to Have project partner in same Lab Section Lab1 and HW1 are out Lab1 Design Doc due next Mon and complete lab following week HW1 due next Wednesday Work alone But, use your resources Lab Section, Piazza.com, Office Hours, Homework Help Session, Class notes, book, Sections, CSUGLab Homework Help Session Thursday and Monday from 6 8pm Location: B14 Hollister

Administrivia Check online syllabus/schedule http://www.cs.cornell.edu/courses/cs3410/2013sp/schedule.html Slides and Reading for lectures Office Hours Homework and Programming Assignments Prelims (in evenings): Tuesday, February 26 th Thursday, March 28 th April 25 th Schedule is subject to change

Summary We can now implement any combinational (combinatorial) logic circuit Decompose large circuit into manageable blocks Encoders, Decoders, Multiplexors, Adders,... Design each block Binary encoded numbers for compactness Can implement circuits using NAND or NOR gates Can implement gates using use P and N transistors And can add and subtract numbers (in two s compliment)! Next time, state and finite state machines