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
Representing Numbers: Integers
Advertisements

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
ECE 331 – Digital System Design
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)
ECE 301 – Digital Electronics
1  1998 Morgan Kaufmann Publishers Chapter Four Arithmetic for Computers.
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.
Numbers & Arithmetic Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
Topic 4 Computer Mathematics and Logic
+ 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
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.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
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 Lec # 2 Winter EECC341 - Shaaban Positional Number Systems A number system consists of an order set of symbols (digits) with relations.
IT253: Computer Organization
1 Digital Systems and Binary Numbers EE 208 – Logic Design Chapter 1 Sohaib Majzoub.
Digital Arithmetic and Arithmetic Circuits
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ć.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
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.
Instructor: Alexander Stoytchev CprE 281: Digital Logic.
DIGITAL CIRCUITS David Kauchak CS52 – Fall 2015.
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.
The Karnaugh Map.
Topics covered: Arithmetic CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.
Digital Logic Lecture 3 Binary Arithmetic By Zyad Dwekat The Hashemite University Computer Engineering Department.
MIPS ALU. Building from the adder to ALU ALU – Arithmetic Logic Unit, does the major calculations in the computer, including – Add – And – Or – Sub –
69 Decimal (Base 10) Numbers n Positional system - each digit position has a value n 2534 = 2*1, * *10 + 4*1 n Alternate view: Digit position.
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
Registers and Binary Arithmetic Prof. Sirer CS 316 Cornell University.
1 Digital Logic Design Lecture 2 More Number Systems/Complements.
Numbers and Arithmetic Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
Arithmetic-Logic Units. Logic Gates AND gate OR gate NOT gate.
Number Representation (Part 2) Computer Architecture (Fall 2006)
1 Ethics of Computing MONT 113G, Spring 2012 Session 4 Binary Addition.
MIPS ALU. Exercise – Design a selector? I need a circuit that takes two input bits, a and b, and a selector bit s. The function is that if s=0, f=a. if.
MicroProcessors Lec. 4 Dr. Tamer Samy Gaafar. Course Web Page —
EE204 L03-ALUHina Anwar Khan EE204 Computer Architecture Lecture 03- ALU.
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.
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.
1 Digital Logic Design (41-135) Chapter 5 Number Representation & Arithmetic Circuits Younglok Kim Dept. of Electrical Engineering Sogang University Spring.
Addition and Subtraction
University of Gujrat Department of Computer Science
Numbers and Arithmetic
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

Number Representations 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)?

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 b = ? 0b = ? 0o 77 = ? 0o 100 = ? 0x ff = ? 0x 100 = ?

How do we count in different bases? Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) Number Representations: Activity #1 Counting b = 255 0b = 256 0o 77 = 63 0o 100 = 64 0x ff = 255 0x 100 = 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  8 = 79 remainder 5 79  8 = 9 remainder 7 9  8 = 1 remainder 1 1  8 = 0 remainder = 0o 1175 lsb (least significant bit) msb (most significant bit) lsbmsb

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

We can represent any number in any base Base 10 – Decimal Base 2 — Binary Base 8 — Octal Base 16 — Hexadecimal Number Representations Summary x 2 7 d o ∙ ∙ ∙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

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? Carry-out Carry-in

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?

Binary Addition Binary addition requires Add of two bits PLUS carry-in Also, carry-out if necessary

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 A B S C out AB S S AB

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

1-bit Adder with Carry A B S C in AB C out S S AB C in

1-bit Adder with Carry A B S C in AB C out S C in AB C in AB C out S S AB C in

1-bit Adder with Carry A B S C in AB C out S C in AB C in AB C out S S AB C in

1-bit Adder with Carry A B S C in AB C out S C in AB C in AB C out S S AB C in

1-bit Adder with Carry A B S C in AB C out S C in AB C in AB C out S S AB C in C out

1-bit Adder with Carry A B S C in AB C out S S AB C in C in AB C in AB C out S

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

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

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 = 1000 = 0111 = = = = -0

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 Representation Is there only one zero! One more example. How do we represent -20?

Two’s Complement Non-negatives (as usual): +0 = = = = = = = = = 1000

Two’s Complement Non-negatives (as usual): +0 = = = = = = = = = 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  ( ) … ( ) –-128 … 127

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 = = = 7 + (-3) =

Two’s Complement Addition Addition with two’s complement signed numbers Perform addition as usual, regardless of sign (it just works) Examples = = = = = = 7 + (-3) = =

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, , overflow, overflow, 1000 fine

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

Binary Subtraction S0S0 S1S1 S2S2 S3S3 1 A0A0 B0B0 A1A1 B1B1 A2A2 B2B2 A3A3 B3B3 C out

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?

Overflow When can overflow occur? adding a negative and a positive? –Overflow cannot occur (Why?) –Always subtract larger magnitude from smaller adding two positives? –Overflow can occur (Why?) –Precision: Add two positives, and get a negative number! adding two negatives? –Overflow can occur (Why?) –Precision: add two negatives, get a positive number! Rule of thumb: Overflow happens iff carry into msb != carry out of msb

Overflow When can overflow occur? Rule of thumb: Overflow happened iff carry into msb != carry out of msb ABC in C out S S MSB over flow A MSB B MSB C out_MSB C in_MSB MSB Wrong Sign Wrong Sign

Two’s Complement Adder Two’s Complement Adder with overflow detection A 0 B 0 S0S0 A 1 B 1 S1S1 A 2 B 2 S2S2 A 3 B 3 S3S3 over flow 0

Two’s Complement Adder Two’s Complement Subtraction with overflow detection S0S0 S1S1 S2S2 S3S3 over flow 1 A0A0 B0B0 A1A1 B1B1 A2A2 B2B2 A3A3 B3B3

Two’s Complement Adder with overflow detection Two’s Complement Adder S0S0 S1S1 S2S2 S3S3 over flow A0A0 B0B0 A1A1 B1B1 A2A2 B2B2 A3A3 B3B3 mux 0=add 1=sub

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

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 S0S0 A 1 B 1 S1S1 A 2 B 2 S2S2 A 3 B 3 S3S3 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

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 Both due in one week, next Monday, start early Work alone But, use your resources ₋Lab Section, Piazza.com, Office Hours, Homework Help Session, ₋Class notes, book, Sections, CSUGLab Homework Help Session Wednesday and Friday from 3:30-5:30pm Location: 203 Thurston

Administrivia Check online syllabus/schedule Slides and Reading for lectures Office Hours Homework and Programming Assignments Prelims (in evenings): ₋Tuesday, February 28 th ₋Thursday, March 29 th ₋April 26 th Schedule is subject to change

Summary We can now implement combinational (combinatorial) logic circuit Design each block –Binary encoded numbers for compactness Decompose large circuit into manageable blocks –1-bit Half Adders, 1-bit Full Adders, n-bit Adders via cascaded 1-bit Full Adders,... Can implement circuits using NAND or NOR gates Can implement gates using use PMOS and NMOS- transistors And can add and subtract numbers (in two’s compliment)! Next time, state and finite state machines…