Binary Addition and Subtraction

Slides:



Advertisements
Similar presentations
Combinational Circuits. Analysis Diagram Designing Combinational Circuits In general we have to do following steps: 1. Problem description 2. Input/output.
Advertisements

L10 – Transistors Logic Math 1 Comp 411 – Spring /22/07 Arithmetic Circuits Didn’t I learn how to do addition in the second grade?
Arithmetic Functions 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.
Chapter 4 Operations on Bits
Assembly Language and Computer Architecture Using C++ and Java
Computer ArchitectureFall 2008 © August 25, CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (1)
Binary Arithmetic Math For Computers.
Building Adders & Sub tractors Dr Ahmed Telba. Introducing adder circuits Adder circuits are essential inside microprocessors as part of the ALU, or arithmetic.
Arithmetic for Computers
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Basic Arithmetic (adding and subtracting)
IT253: Computer Organization
07/19/2005 Arithmetic / Logic Unit – ALU Design Presentation F CSE : Introduction to Computer Architecture Slides by Gojko Babić.
Basic Arithmetic (adding and subtracting)
Digital Electronics and Computer Interfacing
DIGITAL CIRCUITS David Kauchak CS52 – Fall 2015.
COMP541 Arithmetic Circuits
 Lecture 2 Processor Organization  Control needs to have the  Ability to fetch instructions from memory  Logic and means to control instruction sequencing.
CS 232: Computer Architecture II Prof. Laxmikant (Sanjay) Kale.
Outline Binary Addition 2’s complement Binary Subtraction Half Adder
1 Fundamentals of Computer Science Combinational Circuits.
Addition and multiplication Arithmetic is the most basic thing you can do with a computer, but it’s not as easy as you might expect! These next few lectures.
Computer Arthmetic Chapter Four P&H. Data Representation Why do we not encode numbers as strings of ASCII digits inside computers? What is overflow when.
LOGIC CIRCUITLOGIC CIRCUIT. Goal To understand how digital a computer can work, at the lowest level. To understand what is possible and the limitations.
Arithmetic Circuits I. 2 Iterative Combinational Circuits Like a hierachy, except functional blocks per bit.
Addition and multiplication1 Arithmetic is the most basic thing you can do with a computer, but it’s not as easy as you might expect! These next few lectures.
Gunjeet Kaur Dronacharya Group of Institutions. Binary Adder-Subtractor A combinational circuit that performs the addition of two bits is called a half.
Computer Arthmetic Chapter Four P&H.
Invitation to Computer Science, C++ Version, Fourth Edition
Combinational Circuits
Prof. Sin-Min Lee Department of Computer Science
Negative Numbers and Subtraction
Hexadecimal, Signed Numbers, and Arbitrariness
Bitfields and Logic Basics
Registers and clocking issues
Minimization and Sequential Logic
Addition and multiplication
CS231: Computer Architecture I
CS/COE 0447 (term 2181) Jarrett Billingsley
Digital Systems Section 8 Multiplexers. Digital Systems Section 8 Multiplexers.
A Level Computing Component 2
University of Gujrat Department of Computer Science
Invitation to Computer Science, Java Version, Third Edition
MIPS ALU.
Addition, Signed Numbers, and Overflow
ELL100: INTRODUCTION TO ELECTRICAL ENGG.
Subtraction The arithmetic we did so far was limited to unsigned (positive) integers. Today we’ll consider negative numbers and subtraction. The main problem.
Bitwise Operations and Bitfields
Week 7: Gates and Circuits: PART II
Data Representation – Chapter 3
Digital Logic.
1.6) Storing Integer:.
Logic Gates.
Digital Systems Section 12 Binary Adders. Digital Systems Section 12 Binary Adders.
Multiplication and Division
FSMs, Multiplication, and Division
COMS 361 Computer Organization
Arithmetic and Decisions
Instructor: Alexander Stoytchev
Addition and multiplication
CS231: Computer Architecture I
Addition and multiplication
ECE 352 Digital System Fundamentals
Combinational Circuits
CS231: Computer Architecture I
Digital Circuits and Logic
COMS 361 Computer Organization
靜夜思 床前明月光, 疑是地上霜。 舉頭望明月, 低頭思故鄉。 ~ 李白 李商隱.
Presentation transcript:

Binary Addition and Subtraction CS/COE 0447 (term 2181) Jarrett Billingsley

Class announcements hi :) how did you feel about the exam how's the project goin BREAK THINGS UP INTO FUNCTIONS, OH MY LORD let's look at mine to refresh your memory on how it's supposed to look/work 10/3/2017 CS/COE 0447 term 2181

Binary arithmetic 10/3/2017 CS/COE 0447 term 2181

What makes a good word size? Can you think of an example of… 100 of something? a million of something? a billion? a quintillion (1018)? more? 28 = 256, 216 ≅ 65,000, 232 ≅ 4 billion, 264 ≅ 18 quintillion But for a given word size, all the circuitry has to be able to support that many bits. Tradeoffs, tradeoffs. That's engineering. 10/3/2017 CS/COE 0447 term 2181

Remember this? Explain to me, step-by-step, how to add these two binary numbers. 1011 0010 +0010 1111 8/31/2017 CS/COE 0447 term 2181

Formalizing the algorithm For each pair of bits starting at the LSB, Add the two bits and the carry. The low bit of the sum goes into the sum row. The high bit of the sum is the carry for the next higher bit. This is known as the grade school algorithm, cause it's how you learned to add in grade school! 10/3/2017 CS/COE 0447 term 2181

A B C S 1 1 1 1 1 1 1 The Tables of Truth Let's try to come up with a truth table for adding two bits. Each column will hold 1 bit. Let's name the outputs C and S, for Carry and Sum. Let's name the inputs A and B. A B C S Now let's fill in the output values. For the input values, we kinda count up in binary. 1 1 Hey, this C column looks familiar… so does the S column. 1 1 1 1 1 Great! But this is wrong. 10/3/2017 CS/COE 0447 term 2181

00111 110 1011 0010 +0010 1111 1110 0001 Co Ci A B S Half-truth tables What we just made was a half-adder. It produces a carry output but doesn't account for the carry input. To make a full adder, we need 3 input bits. To clarify… Ci A B Co S 1 1 Co Ci 1 1 00111 110 1011 0010 +0010 1111 1110 0001 1 1 1 A 1 1 B 1 1 1 1 1 1 S 1 1 1 1 1 10/3/2017 CS/COE 0447 term 2181

The logic of it all Ci A B Co S 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 It looks a little messy, but it kinda makes sense if you think of it like this: It counts how many input bits are "1". Co and S are a 2-bit number! If we look at the outputs in isolation: S is 1 if we have an odd number of "1s". Co is 1 if we have 2 or 3 "1s". It's a little weird, but we can build this out of AND, OR, and XOR gates. Ci A B Co S 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 10/3/2017 CS/COE 0447 term 2181

Sweeping that under the rug… In programming, we use functions to be able to reuse code. In hardware, we can group these 5 gates into a component. Here's the symbol for a one-bit full adder. + B A Ci Co S The outputs are analogous to return values. Now we don't have to care how it adds, just that it does. The inputs are analogous to parameters/arguments. 10/3/2017 CS/COE 0447 term 2181

Adding longer numbers 10/3/2017 CS/COE 0447 term 2181

1 0 1 1 0 0 1 0 +0 0 1 0 1 1 1 1 Where do the carries go? When you add one place, you might get a carry out. That then becomes the carry in for the next higher place. 1 0 1 1 0 0 1 0 +0 0 1 0 1 1 1 1 Bit Bucket..? 10/3/2017 CS/COE 0447 term 2181

+ + + + A2 A1 A0 B2 B1 B0 S2 S1 S0 Ripple Carry A2 S2 B2 A1 S1 B1 A0 If we want to add two three-bit numbers, we'll need three one-bit adders. We chain the carries from each place to the next higher place, like we do on paper. We have to split the numbers up like so: + B2 A2 S2 + B1 A1 S1 A2 A1 A0 B2 B1 B0 S2 S1 S0 + B0 A0 S0 + 10/3/2017 CS/COE 0447 term 2181

Gate Delay Electrical signals can't move infinitely fast. The transistors can't turn on and off infinitely fast. Since each digit must wait for the next smaller digit to compute its carry, ripple carry will take time linear in the number of digits. This is a diagram of how the outputs of a 16-bit ripple carry adder change over time (courtesy of @ktemkin) It's measured in picoseconds! So about 100ps for the carries to ripple all the way through. But if we went to 32 bits, it'd take 200ps. And 64 bits, 400ps... etc. There are more efficient ways of adding. 10/3/2017 CS/COE 0447 term 2181

How many bits? 99 +99 198 9999 +9999 19998 If you add two 2-digit decimal numbers, what's the largest number you can get? What about two 4-digit decimal numbers? What about two 4-bit numbers? What's the pattern of the number of digits? If you add two n-digit numbers in any base, the result will have at most n + 1 digits. That means if we add two 32-bit numbers… …we might get a 33-bit result! If we have more bits than we can store in our number, that's overflow. 1111 +1111 11110 10/3/2017 CS/COE 0447 term 2181

Detecting overflow 10/3/2017 CS/COE 0447 term 2181

Unsigned overflow What could we do with that 33rd bit if it's a 1? We could ignore it. This is usually a bad idea. You added two numbers and got an incorrect result. Now your program is broken. This is what addu in MIPS does – u for "unchecked" or "unsafe." We could cause an exception. This is what add in MIPS does – normally it crashes the program, but you can handle it yourself, too. We could put that 33rd bit somewhere else. Many other architectures do this, by putting it into a "carry bit" register, which can then be checked by the program. This is very useful for "arbitrary precision arithmetic" – if you want to add 2048-bit numbers, chain many 32-bit additions! 10/3/2017 CS/COE 0447 term 2181

Signed overflow -2 -3 +1 +2 +3 -1 -4 110 101 000 001 010 011 111 100 Remember that the signed number line looks like this: Basically, overflow occurs when we go off either end of the number line. In unsigned arithmetic, 1112 + 12 gives 10002, so that's an overflow. But in signed arithmetic, 1112 + 12 gives 0, which is fine! Detecting signed overflow is a bit more subtle… When you go off the right end of the number line, what kind of answer do you get? What about the left end? 110 -2 101 -3 000 001 +1 010 +2 011 +3 111 -1 100 -4 10/3/2017 CS/COE 0447 term 2181

Detecting signed overflow If you add two numbers of different signs (one negative, one positive), is it ever possible to go off the ends of the number line? No! In that case, you are always getting closer to 0. If you add two numbers of the same sign, how do you know there's an overflow? If you add two positive numbers and get a negative number. If you add two negative numbers and get a positive number. How do you check the signs of the three numbers? (It's easy!) 10/3/2017 CS/COE 0447 term 2181

Subtracting in binary? 10/3/2017 CS/COE 0447 term 2181

Flip side We could come up with a separate subtraction circuit, but… Algebra tells us that x - y = x + (-y) Negation means flip the bits and add 1. Flipping the bits uses NOT gates. How do we add the 1? We use a full adder for the LSB, and when we're subtracting, set the "carry in" to 1! + ~B1 A1 S1 + ~B0 A0 S0 1 10/3/2017 CS/COE 0447 term 2181