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

Slides:



Advertisements
Similar presentations
Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University See P&H Appendix B.7. B.8, B.10, B.11.
Advertisements

EE1A2 Microprocessor Systems & Digital Logic Part I Digital Electronic System Design Dr. T. Collins.
Mohamed Younis CMCS 411, Computer Architecture 1 CMCS Computer Architecture Lecture 7 Arithmetic Logic Unit February 19,
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H Appendix B.7. B.8, B.10, B.11.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See P&H Appendix B.7. B.8, B.10, B.11.
Arithmetic Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.4 (signed), 2.5, 2.6, C.6, and Appendix C.6.
Numbers & Arithmetic Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Circuits & Numbers See: P&H Chapter 2.4, 2.5, 3.2, C.5.
Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University Arithmetic See: P&H Chapter 3.1-3, C.5-6.
Arithmetic Operations and Circuits
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
Assembly Language and Computer Architecture Using C++ and Java
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Gates and Logic Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell Universty See: P&H Appendix C.0, C.1, C.2.
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University See: P&H Chapter 2.4, 3.2, B.2, B.5, B.6.
Chapter 12 Digital Logic Circuit Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Numbers & Arithmetic Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
+ CS 325: CS Hardware and Software Organization and Architecture Exam 1: Study Guide.
CS 105 Digital Logic Design
Dr. Bernard Chen Ph.D. University of Central Arkansas
ES 244: Digital Logic Design Chapter 1 Chapter 1: Introduction Uchechukwu Ofoegbu Temple University.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
1 CHAPTER 4: PART I ARITHMETIC FOR COMPUTERS. 2 The MIPS ALU We’ll be working with the MIPS instruction set architecture –similar to other architectures.
Logic Design CS221 1 st Term combinational circuits Cairo University Faculty of Computers and Information.
Flip-flops, Latches and State Prof. Sirer COMP 303 Koç University.
Outline Analysis of Combinational Circuits Signed Number Arithmetic
1 Modified from  Modified from 1998 Morgan Kaufmann Publishers Chapter Three: Arithmetic for Computers citation and following credit line is included:
Basic Arithmetic (adding and subtracting)
1 Digital Systems and Binary Numbers EE 208 – Logic Design Chapter 1 Sohaib Majzoub.
Chapter 6-1 ALU, Adder and Subtractor
07/19/2005 Arithmetic / Logic Unit – ALU Design Presentation F CSE : Introduction to Computer Architecture Slides by Gojko Babić.
Information Representation. Digital Hardware Systems Digital Systems Digital vs. Analog Waveforms Analog: values vary over a broad range continuously.
CSE 241 Computer Organization Lecture # 9 Ch. 4 Computer Arithmetic Dr. Tamer Samy Gaafar Dept. of Computer & Systems Engineering.
Basic Arithmetic (adding and subtracting)
Number Systems. Today Decimal Hexadecimal Binary –Unsigned Binary –1’s Complement Binary –2’s Complement Binary.
Engineering 1040: Mechanisms & Electric Circuits Spring 2014 Number Systems.
1 DLD Lecture 18 Recap. 2 Recap °Number System/Inter-conversion, Complements °Boolean Algebra °More Logic Functions: NAND, NOR, XOR °Minimization with.
Computer Arithmetic and the Arithmetic Unit Lesson 2 - Ioan Despi.
Numbers & Arithmetic Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
Memory Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University.
Numbers and Arithmetic Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
1 Lecture 6 BOOLEAN ALGEBRA and GATES Building a 32 bit processor PH 3: B.1-B.5.
CDA 3101 Fall 2013 Introduction to Computer Organization The Arithmetic Logic Unit (ALU) and MIPS ALU Support 20 September 2013.
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
CPS3340 Computer Architecture Fall Semester, 2013
EEL-3705 TPS QUIZZES Chapter 4. Quiz 4-1 Using the 2x4 Decoder shown below and two-input OR gates, design a logic circuit which implements.
1 ELEN 033 Lecture 4 Chapter 4 of Text (COD2E) Chapters 3 and 4 of Goodman and Miller book.
CHAPTER 2 Digital Combinational Logic/Arithmetic Circuits
1 Fundamentals of Computer Science Combinational Circuits.
Registers and Binary Arithmetic Prof. Sirer CS 316 Cornell University.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Arithmetic-Logic Units. Logic Gates AND gate OR gate NOT gate.
Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University See P&H Appendix B.7. B.8, B.10, B.11.
LECTURE 4 Logic Design. LOGIC DESIGN We already know that the language of the machine is binary – that is, sequences of 1’s and 0’s. But why is this?
Number Representation (Part 2) Computer Architecture (Fall 2006)
1 Ethics of Computing MONT 113G, Spring 2012 Session 4 Binary Addition.
MicroProcessors Lec. 4 Dr. Tamer Samy Gaafar. Course Web Page —
President UniversityErwin SitompulDigital Systems 7/1 Lecture 7 Digital Systems Dr.-Ing. Erwin Sitompul President University
ETE 204 – Digital Electronics Combinational Logic Design Single-bit and Multiple-bit Adder Circuits [Lecture: 9] Instructor: Sajib Roy Lecturer, ETE,ULAB.
CHAPTER 3 BINARY NUMBER SYSTEM. Computers are electronic machines which operate using binary logic. These devices use two different values to represent.
Number Systems. The position of each digit in a weighted number system is assigned a weight based on the base or radix of the system. The radix of decimal.
Addition and Subtraction
University of Gujrat Department of Computer Science
Numbers and Arithmetic
ELL100: INTRODUCTION TO ELECTRICAL ENGG.
Numbers and Arithmetic
Presentation transcript:

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

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

Goals for Today Binary Operations 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)

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

Recall: Binary Two symbols (base 2): true and false; 1 and 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 Can just as easily use other bases –Base 2 — Binary –Base 8 — Octal –Base 16 — Hexadecimal Number Representations x 2 7 d o

Recall: Binary Two symbols (base 2): true and false; 1 and 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 Can just as easily use other bases –Base 2 — Binary –Base 8 — Octal –Base 16 — Hexadecimal Number Representations 6∙ ∙ ∙10 0 = 637 1∙2 9 +1∙2 6 +1∙2 5 +1∙2 4 +1∙2 3 +1∙2 2 +1∙2 0 = 637 1∙ ∙ ∙ ∙8 0 = 637 2∙ ∙ d∙16 0 = 637 2∙ ∙ ∙16 0 = 637

abcdef abcdef Number Representations: Activity #1 Counting How do we count in different bases? Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) a b c d e f

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 637  2 = 318 remainder  2 = 159 remainder  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 = (can also be written as 0b ) lsb (least significant bit) msb (most significant bit) lsbmsb

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 637  16 = 39 remainder  16 = 2 remainder 7 2  16 = 0 remainder = 0x = 0x 2 7 d Thus, 637 = 0x27d lsb 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. 0b –0b10 = 0x2 –0b0111 = 0x7 –0b1101 = 0xd –Thus, 637 = 0x27d = 0b 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. 0b –0b1 = 0x1 –0b001 = 0x1 –0b111 = 0x7 –0b101 = 0x5 –Thus, 637 = 0o1175 = 0b

Recall: Binary Two symbols (base 2): true and false; 0 and 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 Can just as easily use other bases –Base 2 — Binary –Base 8 — Octal –Base 16 — Hexadecimal Number Representations x 2 7 d o

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 Addition works the same way regardless of base Add the digits in each position Propagate the carry Unsigned binary addition is pretty easy Combine two bits at a time Along with a carry How do we do arithmetic in binary?

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

1-bit Adder with Carry A B S C in AB C out S 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

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 A[4] B[4] S[4] C out C in

4-bit Adder 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 A 0 B 0 S0S0 A 1 B 1 S1S1 A 2 B 2 S2S2 A 3 B 3 S3S3 C out C in

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 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 = = = = 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  : 3  0011  1100  : 7  0111  1000  : 8  1000  0111  : 0  0000  1111  0000 (this is good, -0 = +0)

Two’s Complement Non-negatives (as usual): +0 = = = = = = = = = 1000 Negatives (two’s complement: flip then add 1): ~0 = = 0000 ~1 = = 1111 ~2 = = 1110 ~3 = = 1101 ~4 = = 1100 ~5 = = 1011 ~3 = = 1010 ~7 = = 1001 ~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  ( ) … ( ) –-127 … 128

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

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

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

Binary Subtraction Q: What if (-B) overflows? S0S0 S1S1 S2S2 S3S3 1 A0A0 B0B0 A1A1 B1B1 A2A2 B2B2 A3A3 B3B3 Q: How do we detect and handle overflows? C out

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 adder mux decoder S A B 8 0=add 1=sub

Next Goal Performance

Is this design fast enough? Can we generalize to 32 bits? 64? more? Efficiency and Generality A 0 B 0 R0R0 A 1 B 1 R1R1 A 2 B 2 R2R2 A 3 B 3 R3R3 C0C0

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

4-bit Ripple Carry Adder A3A3 B3B3 S3S3 C4C4 A1A1 B1B1 S1S1 A2A2 B2B2 S2S2 A0A0 B0B0 C0C0 S0S0 C1C1 C2C2 C3C3 First full adder, 2 gate delay Second full adder, 2 gate delay … Carry ripples from lsb to msb

Goals for Today Binary Operations 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 Use array of N sensors for voting machine input i0 i1 i2 i3 i5 i4 i6 Vdd

Output 7-Segment LED photons emitted when electrons fall into holes d7d6 d5d4 d3d2 d1d0

Block Diagram detect 8N

Encoders N might be large Routing wires is expensive More efficient encoding? encoder N...

Encoder Truth Table a b 1 c d o1o1 A 3-bit encoder with 4 inputs for simplicity o0o0 o1o1 o2o2

Encoder Truth Table a b 1 c d o1o1 A 3-bit encoder with 4 inputs for simplicity abcdo2o1o o0o0 o1o1 o2o2 o2 = abcd o1 = abcd + abcd o0 = abcd + abcd

Ballot Reading detect enc 83 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 3 inputs encode 0 – 7 in binary 7 outputs one for each LED 7LED decode

7 Segment LED Decoder Implementation d0 d1 d2 d3 d4 d5 d6 b2b1b0d6d5d4d3d2d1d

7 Segment LED Decoder Implementation d0 d1 d2 d3 d4 d5 d6 b2b1b0d6d5d4d3d2d1d

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

Building Blocks binary encoder 2N2N N binary decoder N 2N2N Multiplexor N M N N N N M -1

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 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…