Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University.

Slides:



Advertisements
Similar presentations
Combinational Circuits
Advertisements

Combinational Circuits. Analysis Diagram Designing Combinational Circuits In general we have to do following steps: 1. Problem description 2. Input/output.
Representing Numbers: Integers
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 Arithmetic See: P&H Chapter 3.1-3, C.5-6.
Arithmetic Operations and Circuits
CS 151 Digital Systems Design Lecture 3 More Number Systems.
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)
1 Binary Arithmetic, Subtraction The rules for binary arithmetic are: = 0, carry = = 1, carry = = 1, carry = = 0, carry =
Digital Arithmetic Wen-Hung Liao, Ph.D.. Objectives Perform binary addition, subtraction, multiplication, and division on two binary numbers. Add and.
Number Representation (1) Fall 2005 Lecture 12: Number Representation Integers and Computer Arithmetic.
ECE 301 – Digital Electronics
DIGITAL SYSTEMS TCE1111 Representation and Arithmetic Operations with Signed Numbers Week 6 and 7 (Lecture 1 of 2)
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
1 Binary Numbers Again Recall that N binary digits (N bits) can represent unsigned integers from 0 to 2 N bits = 0 to 15 8 bits = 0 to bits.
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.
+ CS 325: CS Hardware and Software Organization and Architecture Exam 1: Study Guide.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Number Representations and Computer Arithmetic. CS 21a 9/23/02 Odds and Ends Slide 2 © Luis F. G. Sarmenta and John Paul Vergara, Ateneo de Manila University.
1 Arithmetic and Logical Operations - Part II. Unsigned Numbers Addition in unsigned numbers is the same regardless of the base. Given a pair of bit sequences.
Data Representation – Binary Numbers
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.
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.
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ć.
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.
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.
Numbers and Arithmetic Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University See: P&H Chapter , 3.2, C.5 – C.6.
1 Lecture 6 BOOLEAN ALGEBRA and GATES Building a 32 bit processor PH 3: B.1-B.5.
08 ARTH Page 1 ECEn/CS 224 Number Representation and Binary Arithmetic.
The Karnaugh Map.
Topics covered: Arithmetic CSE243: Introduction to Computer Architecture and Hardware/Software Interface.
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 –
Digital Representations ME 4611 Binary Representation Only two states (0 and 1) Easy to implement electronically %0= (0) 10 %1= (1) 10 %10= (2) 10 %11=
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 IKI20210 Pengantar Organisasi Komputer Kuliah No. 23: Aritmatika 18 Desember 2002 Bobby Nazief Johny Moningka
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.
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.
Lecture No. 4 Computer Logic Design. Negative Number Representation 3 Options –Sign-magnitude –One’s Complement –Two’s Complement  used in computers.
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.
Addition and Subtraction
University of Gujrat Department of Computer Science
Numbers and Arithmetic
Numbers and Arithmetic
Presentation transcript:

Kavita Bala CS 3410, Spring 2014 Computer Science Cornell University

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

Binary Operations Number representations One-bit and four-bit adders Negative numbers and two’s complement Addition (two’s complement) Subtraction (two’s complement) Performance

Recall: binary Two symbols (base 2): true and false; 1 and 0 Basis of logic circuits and all digital computers How to represent numbers in binary (base 2)?

Know how to represent numbers in decimal (base 10) –E.g Other bases –Base 2 — Binary –Base 8 — Octal –Base 16 — Hexadecimal x 2 7 d o

abcdef abcdef Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) a b c d e f

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

abcdef abcdef Dec (base 10) Bin (base 2) Oct (base 8) Hex (base 16) a b c d e f b = 255 0b = 256 0o 77 = 63 0o 100 = 64 0x ff = 255 0x 100 = 256

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

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) lsb msb

n bits: 0 to 2 n -1 E.g., 4 bits 0000 to 1111 is 0 to 15

Convert a base 2 number to base 8 (oct) or 16 (hex) Binary to Hexadecimal Convert each nibble (group of 4 bits) from binary to hex A nibble (4 bits) ranges in value from 0…15, which is one hex digit –Range: 0000…1111 (binary) => 0x0 …0xF (hex) => 0…f –E.g. 0b –0b10 = 0x2 –0b0111 = 0x7 –0b1101 = 0xd –Thus, 637 = 0x27d = 0b Similarly for base 2 to base 8

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

Binary Arithmetic: Add and Subtract two binary numbers

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 requires Add of two bits PLUS carry-in Also, carry-out if necessary

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

A B S AB S S AB

A B S AB S S AB

A B S C in C out Full Adder Adds three 1-bit numbers Computes 1-bit result and 1- bit carry Can be cascaded ABC in C out S

A B S C in C out ABC in C out S Full Adder Adds three 1-bit numbers Computes 1-bit result and 1-bit carry Can be cascaded

A B S C in C out ABC in C out S

A B S C in C out ABC in C out S AB S C in

A B S C out ABC in C out S C in AB C in AB C out S Using Karnaugh maps

ABC in C out S

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

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

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

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 Others attempts One’s complement IBM = = = = -0

What is used: Two’s Complement Representation To negate any number: complement all the bits (i.e. flip all the bits) then add 1 Nonnegative numbers are represented as usual 0 = 0000, 1 = 0001, 3 = 0011, 7 = 0111 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  : 0  0000  1111  0000 (this is good, -0 = +0)

One more example. How do we represent -20? 20 = = =

Negatives (two’s complement: flip then add 1): flip = = 0000 flip = = 1111 flip = = 1110 flip = = 1101 flip = = 1100 flip = = 1011 flip = = 1010 flip = = 1001 flip = = 1000 Non-negatives (as usual): +0 = = = = = = = = = 1000

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

Extending to larger size 1111 = = = = 7 Truncate to smaller size = 15 BUT, = 1111 = -1

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

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

Why create a new circuit? Just use addition using two’s complement math How?

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) S0S0 S1S1 S2S2 S3S3 1 A0A0 B0B0 A1A1 B1B1 A2A2 B2B2 A3A3 B3B3 C out

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)

In general, how do we detect and handle overflow?

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

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 in to msb != carry out of msb

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 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 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 S0S0 S1S1 S2S2 S3S3 over flow A0A0 B0B0 A1A1 B1B1 A2A2 B2B2 A3A3 B3B3 mux 0=add 1=sub

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

decoder 8 8 S 0=add 1=sub A B 8

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

Is this design fast enough? Can we generalize to 32 bits? 64? more? A 0 B 0 S0S0 A 1 B 1 S1S1 A 2 B 2 S2S2 A 3 B 3 S3S3 C0C0

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

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

Binary Operations Number representations One-bit and four-bit adders Negative numbers and two’s complement Addition (two’s complement) Subtraction (two’s complement) Performance

We can now implement combinatorial logic circuits 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 Can add and subtract numbers (in two’s complement)! Next time, state and finite state machines…

Check online syllabus/schedule Slides and Reading for lectures Office Hours Homework and Programming Assignments Schedule is subject to change