Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 352 Digital System Fundamentals

Similar presentations


Presentation on theme: "ECE 352 Digital System Fundamentals"— Presentation transcript:

1 ECE 352 Digital System Fundamentals
Arithmetic Structures In this presentation, we will discuss how digital logic is used to implement basic arithmetic operations.

2 Topics Arithmetic Structures Addition and Subtraction
Contraction and Specialized Structures Other Arithmetic Operations First we’ll look at addition and subtraction of binary values.

3 Review: Binary Addition
Same operation at every bit position: Add two bits plus carry-in if present If result larger than representable by one bit, carry-out from that position How should we create hardware for this? Design hardware for one bit position Replicate! Addition is a very regular operation. At each bit position, we add the bits from the two numbers plus a carry-in, creating a 2-bit sum. The 1s digit of that sum is the result bit for that position; the 2s digit is the carry to the next bit position. Since the operation is the same at every bit, we can design the logic for a single bit, and then replicate it for each bit.

4 Review: Half/Full Adders
Half Adder Adds two single-bit inputs Full Adder Adds three single-bit inputs Built from two half-adders plus an OR gate Need a full adder to propagate the carry! Let’s review what these structures look like. A half adder adds 2 bits, and generates a sum bit and a carry bit. A full adder adds 3 single bit inputs, and produces a sum bit and a carry bit. Note that the full adder is in fact larger than two half adders, since it also requires an OR gate to combine the carry bits of the half adders. The full adder has all the logic required for a single bit position – it can accept two bits of input plus a carry-in from the previous bit position.

5 Ripple Carry Adder Combinational circuit to add two binary vectors
In an n-bit number the LSb has subscript 0 and the MSb has subscript n-1 Series of Full Adders A carry can occur at any bit position and ripple through towards the most significant bit. A ripple carry adder is a combinational logic circuit that can add two binary vectors. Any N-bit binary number is really a vector of N bits, where the least significant bit has index 0 and the most significant has index N-1. The ripple carry adder is constructed from a series of full adders. In this case, we are adding two 4-bit numbers, A and B, and producing a 4-bit sum S. It is called a ripple carry adder because the result is not valid until the carry signal from the least significant bit has “rippled” all the way through to the most significant bit of the adder. If there are a lot of bits, this delay can be a problem, so later we’ll look at ways of speeding it up. the carry-out of each full adder is the carry-in to the next

6 Binary Subtractor Modify ripple-carry adder: A + (–B)
Bitwise complement B before the addition Add the 1 to B using the carry-in “flip all the bits” We don’t normally do subtraction in computer hardware. Instead we add the negation of a number. Remember, we’re using 2’s-complement format. So, to implement A – B, we add A to negative B, which we get by flipping all the bits of B and adding 1. The reason we use the 2’s-complement format is that it makes this process easy, and lets us use the same full adder structures with only a little additional logic. “and add 1”

7 Adder/Subtractor Use the same structure to both add and subtract
Conditionally negate the second operand If we want to add, don’t negate If we want to subtract, negate: “Flip all the bits and add 1” Augment the structure with additional logic and a special control signal Control input to the adder/subtractor chooses the mode of operation Depending on its value, the structure adds or subtracts To create a single structure that can add or subtract, we can conditionally negate the second operand for subtraction, but leave it as is for addition. To do this, we need to an input to the structure that signals which operation it is supposed to do.

8 Binary Adder-Subtractor
Subtract if SUB is 1, add if SUB is 0 XOR it with all bits of second operand “Flip all the bits IF we want to subtract (but do not if we add)” Use it as the carry input “Add 1 IF we want to subtract (but not if we add)” We’ve chosen to call the signal SUB. Since we assume signals are active-high unless told otherwise, we set SUB to 1 to do subtraction and set SUB to 0 to do addition. We could have used a signal called ADD instead, but we’ll see that SUB is simpler. We augment each full adder with an XOR gate at the B input. The other input of each XOR, as well as the carry-in to the complete adder, is connected to the SUB signal. If SUB = 0, then the carry-in is 0 and the output of each XOR gate is simply the associated B bit. This means the circuit performs A + B. If SUB = 1, then each XOR gate complements its B bit, and we add 1 to the overall result, so the circuit performs A + (-B) and we have a subtractor.

9 Overflow Detection We know we have overflow if the result cannot “fit” in the required number of bits But how can we automatically detect this? Unsigned overflow if C = 1 Cout of MSb is 1 Signed overflow if V = 1 Cin to MSb does not match Cout from MSb When we do an arithmetic operation, it’s often important to know if there was overflow. An operation overflows if the number of available result bits cannot correctly represent the result of the operation. Another way of saying this is that if the result is incorrect, overflow must have occurred. But how can we detect this automatically? The answer depends on whether we are using unsigned or signed numbers. If we are operating on unsigned numbers, then a carry out from the adder indicates that overflow occurred. If instead we are operating on 2’s-complement numbers, the carry-out value is not indicative of overflow – think about what happens if you add (-1) + (-1). Instead, we look at the carry-in and carry-out at the most significant bit. If those signals are different, then overflow has occurred. This isn’t an obvious relationship – it is derived from a truth table of the A, B, carry-in and carry-out signals for the most significant bit. You can find the derivation in your textbook. One thing to remember, though, is that we need to know what representation we’re using to know whether or not we have overflow. The circuit does not know the representation – it calculates the sum the same way regardless.

10 Topics Arithmetic Structures Addition and Subtraction
Contraction and Specialized Structures Other Arithmetic Operations Next we’ll talk about structures that can be optimized if one of the operands is a constant value.

11 Contraction If an input to a circuit will always be a constant value, we can simplify the circuit Apply Boolean algebra rules to reduce and/or remove gates Contraction makes the circuit smaller, faster, and less complex (more efficient for a specific task) X + 1 = 1 X + 0 = X X·1 = X X·0 = 0 X + X = X X + X = 1 X·X = X X· X = 0 X = X Contraction is the process of taking advantage of constant 1/0 inputs and using Boolean algebra to simplify the circuit accordingly. We apply basic Boolean algebra identities to eliminate or reduce the gates that have constant inputs applied. For example, if an AND gate has a constant 0 on one input, the output will always be 0 and we can eliminate that gate by replacing its output with a constant 0. If that constant 0 were an input to another gate, we would repeat the simplification process, using the appropriate Boolean identity. We will use contraction here for arithmetic circuits, but contraction is a more general idea. It lets us optimize a general-purpose circuit structure for a specific task, so we end up with a more efficient implementation for that task.

12 Example: Incrementer Note: if an amount is not specified, should assume “increment” means “add 1” Simplified adder structure Set the B input to 0012, then simplify 1 A common example is creating an incrementer from an adder. In general, assume increment means “add 1” unless it is specifically stated otherwise, such as “increment-by-4”. Similarly, decrement means “subtract 1” unless specified otherwise. Starting with a 3-bit adder, we apply the constant value 1 to the B input, and then we simplify.

13 Example: Incrementer Note: if an amount is not specified, should assume “increment” means “add 1” Simplified adder structure Set the B input to 0012, then simplify inverter 1 XORing with 1 complements the other input, while XORing with 0 passes the other input through. So the output of the top XOR is always /A0, and the second XOR passes that through to become S0. ANDing with 1 passes the other input through, while ANDing with 0 always produces 0. ORing with 0 passes the other input through, so the carry into the next stage will simply be A0, A0 = A0

14 Example: Incrementer Note: if an amount is not specified, should assume “increment” means “add 1” Simplified adder structure Set the B input to 0012, then simplify and so we find that the entire first adder stage can be replaced with a single inverter. In the second adder, we replace the top XOR with a wire from A1, and replace the AND gate in the top half-adder with a constant 0. Since one input to the OR gate is now 0, we can replace it with a wire as well.

15 Example: Incrementer Note: if an amount is not specified, should assume “increment” means “add 1” Simplified adder structure Set the B input to 0012, then simplify After we identify these optimizations, we can greatly simplify the second full adder. In fact, we turned it into only a half adder. Since the second stage really only adds 2 bits, this makes sense. In the most significant bit, we can make similar simplifications, resulting in a similar structure.

16 We can also remove AND gate if the carry-out is not needed
Example: Incrementer Note: if an amount is not specified, should assume “increment” means “add 1” Simplified adder structure Set the B input to 0012, then simplify We can also remove AND gate if the carry-out is not needed If we didn’t need the carry-out signal, we could also remove the AND gate that produces C3. When contracting a circuit, we also look at all unused outputs, and simplify the circuit by removing all logic that was used only for generating those outputs.

17 Example: Decrement-by-two
“Decrement by two” means add −2 Set the B input to 1102, then simplify We could use adder/subtractor in subtract mode and use B = 0102, but that requires more simplification! 1 Let’s look at another example. To create a decrement-by-2 circuit, we add negative 2. So we set the B input to -2, or 110 in 2’s-complement binary. Since we have a constant operation, it is simpler to start with an adder rather than using the adder/subtractor circuit. However, if you contracted the adder/subtractor instead, you would in fact end up with the same end result—it would just require more steps.

18 Example: Decrement-by-two
“Decrement by two” means add −2 Set the B input to 1102, then simplify We could use adder/subtractor in subtract mode and use B = 0102, but that requires more simplification! 1 1 Contracting each adder, we end up with this simplified circuit.

19 Example: Decrement-by-two
“Decrement by two” means add −2 Set the B input to 1102, then simplify We could use adder/subtractor in subtract mode and use B = 0102, but that requires more simplification! C3 = A2 + A2 A1 = A2 + A1 If we then examine the carry-out logic, we can see that an algebraic simplification is possible, resulting in an even simpler circuit.

20 Topics Arithmetic Structures Addition and Subtraction
Contraction and Specialized Structures Other Arithmetic Operations Now we’ll take brief look at some other common arithmetic operations.

21 Zero-Fill and Sign Extension
Sometimes need to represent an operand using additional bits For example, add 4-bit operand to an 8-bit operand If unsigned, need to “zero-fill” Append 0s to most significant end of vector Example: 0101 becomes Example: 1011 becomes If signed, need to “sign extend” Replicate MSb for additional upper bits Example: 1011 becomes We often need to take a value and represent it using more bits. For example, to add a 4-bit operand to an 8-bit operand, we need to extend the 4-bit operand to 8-bits, then we can use an 8-bit adder to compute the result. How we extend the operands depends on the number representation. The key idea here is that we want an operand with more bits, but we don’t want to change the VALUE of the operand. For an unsigned number, we “zero-fill”, which is simply padding the operand with additional 0s on the left. Note that in both examples, the value of the number was not changed. For a signed number, we do what is called “sign extension”. Note that when we use the term “signed”, it is assumed to refer to a 2’s-complement representation. To sign extend, we replicate the sign bit for each additional bit position. By doing this, the operand still has the same value. In the first example, the 4-bit operand was 5, and the value of the extended 8-bit operand is still 5. In the second example, both the 4-bit and 8-bit operands have value -5.

22 Multiplication/Division
General multipliers/dividers more complicated than we deal with in this class Powers of 2 make multiply/divide easy: just shift left/right by log2 of the multiplier/divisor… Example: × 410 = Example: ÷ 210 = Can use a sequence of additions to multiply by a constant value… Example:  × 610 General purpose circuits to perform multiplication and division are quite complex, and beyond the scope of this class. However, multiplying or dividing by powers of 2 is simple, since our operands are base-2 numbers. To multiply, we simply shift left, and to divide we shift right. Note that this is exactly the same principle as multiplication or division by powers of 10 in decimal. We can take advantage of this idea by decomposing a multiplier into a sum of powers of 2, computing each sub-product separately, and then adding the result. So, if we want to multiply a number by 6, we split 6 into (4 + 2), do each multiply as a shift, and add the two results. = (011102 × 410) + (011102 × 210) = =

23 ECE 352 Digital System Fundamentals
Arithmetic Structures This concludes our video on arithmetic structures.


Download ppt "ECE 352 Digital System Fundamentals"

Similar presentations


Ads by Google