Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic and Decisions

Similar presentations


Presentation on theme: "Arithmetic and Decisions"— Presentation transcript:

1 Arithmetic and Decisions
CS/COE 0447 Jarrett Billingsley

2 Class announcements hooooooOO! CS447

3 Binary addition, in logic
CS447

4 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 count up in binary 1 1 hey, this C column looks familiar… what about S? 1 1 1 1 1 great! But this is wrong. - whenever making a truth table, ALWAYS count up in binary – so easy to miss cases if you jump around - C and S form a 2-bit number - C just looks like A & B - S looks like A xor B CS447

5 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 has a carry output but not a carry input (which might be useful for the lowest bit) to make a full adder, we need 3 input bits Ci A B Co S 1 1 Co Ci 1 1 1 1 1 A 1 1 B 1 1 1 1 1 1 S 1 1 1 1 1 CS447

6 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" 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" there's a full adder example circuit for you Ci A B Co S 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 - Again, Co and S are a 2-bit number - it might not be obvious, but we can come up with a collection of gates that output each of these columns 1 1 1 1 1 CS447

7 Sweeping that under the rug…
in programming, we use functions to be able to reuse code in logic diagrams, we can group these 5 gates into a component here's the symbol for a one-bit full adder + B A Ci Co S outputs are like return values now we don't have to care how it adds, just that it does inputs are like parameters unfortunately Logisim flips this upside down but whatever. CS447

8 Making a component in Logisim
we can use Project > Add Circuit… to make a new component then we can edit it by double-clicking it on the left this is like editing a function's code a component has pins: its inputs and outputs one of the labs will go into more detail on how to make components. now we can place multiple instances of the component on the main circuit, like making function calls. CS447

9 Adding longer numbers CS447

10 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 becomes the carry in for the next higher place Bit Bucket..? - what is this called? - how can we handle it? CS447

11 + + + + 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 + - it's called ripple carry because the carry "ripples" from the LSB to the MSB one place at a time CS447

12 Gate Delay electrical signals can't move infinitely fast
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 is linear in the number of digits this is a diagram of how the outputs of a 16-bit ripple carry adder change over time they added 0xFFFF + 1 it's measured in picoseconds! so ~100ps total but if we went to 32 bits, it'd take 200ps and 64 bits, 400ps... there are more efficient ways of adding details, schmetails - look-ahead carry is faster but requires O(n^2) space 300 400 CS447 (courtesy of Kate Temkin)

13 + + Flip side A1 S1 ~B1 A0 S0 ~B0 1 remember how we do subtraction?
x - y = x + (-y) negation means NOT and add 1 NOT uses… NOT gates how do we add 1 without any extra circuitry? we use a full adder for the LSB, and when we're subtracting, set the "carry in" to 1 now we're doing x + (~y) + 1 which is x + (-y) ! + ~B1 A1 S1 + ~B0 A0 S0 1 CS447

14 Detecting unsigned overflow in hardware
how did we detect unsigned overflow? if the sum is smaller than either of the addends. but there's an easier way… if we add two n bit numbers, and get an n + 1 bit number, what will the last carry be? 1! so we can look at the MSB's carry-out. if it's 1, it's an overflow. 1 if overflow! + B2 A2 S2 + B1 A1 S1 + B0 A0 S0 CS447

15 Detecting signed overflow in hardware
how did we detect signed overflow? same input signs; different output sign. that feels truth-table-y. SA SB So O 1 1 when the input signs differ, we can't have an overflow, so those rows are all 0. 1 1 1 when the input signs match, overflow happens when the output sign differs. 1 1 1 1 1 1 1 1 1 CS447

16 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 for a given word size, all the circuitry has to be built to support it 64 1-bit adders 128 wires going in 64 wires coming out CS447

17 A new way of thinking about making decisions
CS447

18 How you're used to doing it
how do we make decisions in programming? with if-elses, conditional loops, switches… if(mode == 1) return A + B; else return A - B; if mode == 1, is the else clause run? No. when you make a decision in software, only one code path is run; the others never happen. CS447

19 How hardware does it sum = A + B; diff = A – B; if(mode == 1)
instead of making the decision first and then doing one thing… hardware does all possible things, then decides which to use. sum = A + B; diff = A – B; if(mode == 1) return sum; else return diff; this seems wasteful, but: these two paths can be run at the same time! when you make a decision in hardware, you do all possible things, then pick only the thing you need, and ignore the rest. CS447

20 Hardware that makes decisions
CS447

21 Y A B S Y A B S=0 A Y B S=1 Multiplexers
a multiplexer (mux) outputs one of its inputs based on a select. Y A B S Y A B S=0 A Y B S=1 the unselected input is ignored! This is the select input. - muxes are universal too! you can build any logic gate – and therefore an entire computer – out of muxes CS447

22 Big muxes a mux can have any (power-of-2) number of inputs here's one with 4 inputs! to choose among 4 values… how big does the select signal have to be now? 2 bits. A 00 B 01 Y C 10 D 11 2 S CS447

23 Mux anti-patterns A 1 A 1 ~A A B
just like with gates, there are some "pointless" ways to use muxes. A 1 A 1 ~A A B this is an overcomplicated wire. this is an overcomplicated NOT gate. the select signal should never be a constant. CS447

24 they are pretty much useless for this class.
Demultipliexers a demultiplexer is the opposite of a multiplexer, but... In S In S=0 In S=1 they are pretty much useless for this class. - please don't use them. okay? okay. 95% of the times I've seen people using them, they're pointless. so avoid them. CS447

25 Implementing this if-else
now we have enough information to do this: if(mode == 1) return A + B; else return A - B; A - B mode result else if + the select signal is basically the condition. we send the input values to all possible branches. CS447


Download ppt "Arithmetic and Decisions"

Similar presentations


Ads by Google