Twos complement Our final idea is twos complement

  • Slides: 41
Download presentation
Two’s complement • • Our final idea is two’s complement. To negate a number,

Two’s complement • • Our final idea is two’s complement. To negate a number, complement each bit (just as for ones’ complement) and then add 1. Examples: 11012 0 1101 1 0010 1 0011 = 1310 = +1310 = -1310 01002 = 410 0 0100 = +410 1 1011 = -410 1 1100 = -410 June 25, 2002 (a 4 -bit unsigned number) (a positive number in 5 -bit two’s complement) (a negative number in 5 -bit ones’ complement) (a negative number in 5 -bit two’s complement) Arithmetic-logic units 1

More about two’s complement • Two other equivalent ways to negate two’s complement numbers:

More about two’s complement • Two other equivalent ways to negate two’s complement numbers: – You can subtract an n-bit two’s complement number from 2 n. 1 00000 - 0 1 1 0 1 (+1310) 1 0 0 1 1 (-1310) – • 1 00000 - 0 0 1 0 0 (+410) 1 1 1 0 0 (-410) You can complement all of the bits to the left of the rightmost 1. 01101 10011 = +1310 (a positive number in two’s complement) = -1310 (a negative number in two’s complement) 00100 11100 = +410 = -410 (a positive number in two’s complement) (a negative number in two’s complement) Often, people talk about “taking the two’s complement” of a number. This is a confusing phrase, but it usually means to negate some number that’s already in two’s complement format. June 25, 2002 Arithmetic-logic units 2

Two’s complement addition • • • Negating a two’s complement number takes a bit

Two’s complement addition • • • Negating a two’s complement number takes a bit of work, but addition is much easier than with the other two systems. To find A + B, you just have to: – Do unsigned addition on A and B, including their sign bits. – Ignore any carry out. For example, to find 0111 + 1100, or (+7) + (-4): – First add 0111 + 1100 as unsigned numbers: 01 1 1 + 1 1 001 1 – – Discard the carry out (1). The answer is 0011 (+3). June 25, 2002 Arithmetic-logic units 3

Another two’s complement example • • To further convince you that this works, let’s

Another two’s complement example • • To further convince you that this works, let’s try adding two negative numbers— 1101 + 1110, or (-3) + (-2) in decimal. Adding the numbers gives 11011: 1 1 01 + 1110 1 1 01 1 • Dropping the carry out (1) leaves us with the answer, 1011 (-5). June 25, 2002 Arithmetic-logic units 4

Why does this work? • For n-bit numbers, the negation of B in two’s

Why does this work? • For n-bit numbers, the negation of B in two’s complement is 2 n - B (this is one of the alternative ways of negating a two’s-complement number). A-B • • = A + (-B) = A + (2 n - B) = (A - B) + 2 n If A B, then (A - B) is a positive number, and 2 n represents a carry out of 1. Discarding this carry out is equivalent to subtracting 2 n, which leaves us with the desired result (A - B). If A B, then (A - B) is a negative number and we have 2 n - (A - B). This corresponds to the desired result, -(A - B), in two’s complement form. June 25, 2002 Arithmetic-logic units 5

Comparing the signed number systems • • • Here all the 4 -bit numbers

Comparing the signed number systems • • • Here all the 4 -bit numbers in the different systems. Positive numbers are the same in all three representations. Signed magnitude and one’s complement have two ways of representing 0. This makes things more complicated. Two’s complement has asymmetric ranges; there is one more negative number than positive number. Here, you can represent -8 but not +8. However, two’s complement is preferred because it has only one 0, and its addition algorithm is the simplest. June 25, 2002 Arithmetic-logic units 6

Ranges of the signed number systems • How many negative and positive numbers can

Ranges of the signed number systems • How many negative and positive numbers can be represented in each of the different systems on the previous page? • In general, with n-bit numbers including the sign, the ranges are: June 25, 2002 Arithmetic-logic units 7

Converting signed numbers to decimal • Convert 110101 to decimal, assuming this is a

Converting signed numbers to decimal • Convert 110101 to decimal, assuming this is a number in: (a) signed magnitude format (b) ones’ complement (c) two’s complement June 25, 2002 Arithmetic-logic units 8

Example solution • Convert 110101 to decimal, assuming this is a number in: Since

Example solution • Convert 110101 to decimal, assuming this is a number in: Since the sign bit is 1, this is a negative number. The easiest way to find the magnitude is to convert it to a positive number. (a) signed magnitude format Negating the original number, 110101, gives 010101, which is +21 in decimal. So 110101 must represent -21. (b) ones’ complement Negating 110101 in ones’ complement yields 001010 = +10 10, so the original number must have been -1010. (c) two’s complement • Negating 110101 in two’s complement gives 001011 = 1110, which means 110101 = -1110. The most important point here is that a binary number has different meanings depending on which representation is assumed. June 25, 2002 Arithmetic-logic units 9

Our four-bit unsigned adder circuit • Here is the four-bit unsigned addition circuit from

Our four-bit unsigned adder circuit • Here is the four-bit unsigned addition circuit from an earlier lecture. June 25, 2002 Arithmetic-logic units 10

Making a subtraction circuit • • We could build a subtraction circuit directly, similar

Making a subtraction circuit • • We could build a subtraction circuit directly, similar to the way we made unsigned adders yesterday. However, by using two’s complement we can convert any subtraction problem into an addition problem. Algebraically, A - B = A + (-B) • • So to subtract B from A, we can instead add the negation of B to A. This way we can re-use the unsigned adder hardware from last week. June 25, 2002 Arithmetic-logic units 11

A two’s complement subtraction circuit • • • To find A - B with

A two’s complement subtraction circuit • • • To find A - B with an adder, we’ll need to: – Complement each bit of B. – Set the adder’s carry in to 1. The net result is A + B’ + 1, where B’ + 1 is the two’s complement negation of B. Remember that A 3, B 3 and S 3 here actually sign bits. June 25, 2002 Arithmetic-logic units 12

Small differences • The only differences between the adder and subtractor circuits are: –

Small differences • The only differences between the adder and subtractor circuits are: – The subtractor has to negate B 3 B 2 B 1 B 0. – The subtractor sets the initial carry in to 1, instead of 0. • It’s not too hard to make one circuit that does both addition and subtraction. June 25, 2002 Arithmetic-logic units 13

An adder-subtractor circuit • XOR gates let us selectively complement the B input. X

An adder-subtractor circuit • XOR gates let us selectively complement the B input. X 0=X • • X 1 = X’ When Sub = 0, the XOR gates output B 3 B 2 B 1 B 0 and the carry in is 0. The adder output will be A + B + 0, or just A + B. When Sub = 1, the XOR gates output B 3’ B 2’ B 1’ B 0’ and the carry in is 1. Thus, the adder output will be a two’s complement subtraction, A - B. June 25, 2002 Arithmetic-logic units 14

Signed overflow • • With two’s complement and a 4 -bit adder, for example,

Signed overflow • • With two’s complement and a 4 -bit adder, for example, the largest representable decimal number is +7, and the smallest is -8. What if you try to compute 4 + 5, or (-4) + (-5)? 01 00 + 01 01 01 001 • • (+4) (+5) (-7) 1 1 00 + 1 01 1 1 (-4) (-5) (+7) We cannot just include the carry out to produce a five-digit result, as for unsigned addition. If we did, (-4) + (-5) would result in +23! Also, unlike the case with unsigned numbers, the carry out cannot be used to detect overflow, by itself – In the example on the left, the carry out is 0 but there is overflow. – Conversely, there are situations where the carry out is 1 but there is no overflow. June 25, 2002 Arithmetic-logic units 15

Detecting signed overflow • The easiest way to detect signed overflow is to look

Detecting signed overflow • The easiest way to detect signed overflow is to look at all the sign bits. 01 00 + 01 01 01 001 • • (+4) (+5) (-7) 1 1 00 + 1 01 1 1 (-4) (-5) (+7) Overflow occurs only in the two situations above: – If you add two positive numbers and get a negative result. – If you add two negative numbers and get a positive result. Overflow cannot occur if you add a positive number to a negative number. Do you see why? June 25, 2002 Arithmetic-logic units 16

Sign extension • • In everyday life, decimal numbers are assumed to have an

Sign extension • • In everyday life, decimal numbers are assumed to have an infinite number of 0 s in front of them. This helps in “lining up” numbers. To subtract 231 and 3, for instance, you can imagine: - • • 231 003 228 You need to be careful in extending signed binary numbers, because the leftmost bit is the sign and not part of the magnitude. If you just add 0 s in front, you might accidentally change a negative number into a positive one! For example, going from 4 -bit to 8 -bit numbers: – 0101 (+5) should become 0000 0101 (+5). – But 1100 (-4) should become 1111 1100 (-4). The proper way to extend a signed binary number is to replicate the sign bit, so the sign is preserved. June 25, 2002 Arithmetic-logic units 17

Subtraction summary • • A good representation for negative numbers makes subtraction hardware much

Subtraction summary • • A good representation for negative numbers makes subtraction hardware much easier to design. – Two’s complement is used most often (although signed magnitude shows up sometimes, such as in floating-point systems, which we’ll discuss on Wednesday). – Using two’s complement, we can build a subtractor with minor changes to the adder from last week. – We can also make a single circuit which can both add and subtract. Overflow is still a problem, but signed overflow is very different from the unsigned overflow we mentioned last time. Sign extension is needed to properly “lengthen” negative numbers. Tomorrow we’ll use most of the ideas we’ve seen so far to build an ALU – an important part of a processor. June 25, 2002 Arithmetic-logic units 18

Arithmetic-logic units • • • An arithmetic-logic unit, or ALU, performs many different arithmetic

Arithmetic-logic units • • • An arithmetic-logic unit, or ALU, performs many different arithmetic and logic operations. The ALU is the “heart” of a processor—you could say that everything else in the CPU is there to support the ALU. Here’s the plan: – We’ll show an arithmetic unit first, by building off ideas from the adder-subtractor circuit. – Then we’ll talk about logic operations a bit, and build a logic unit. – Finally, we put these pieces together using multiplexers. We show the same examples as from the book (pp. 360 -365), but things are re-labeled to be clearer in Logic. Works. Cin and S 0 are also treated a little differently. June 25, 2002 © 2000 -2002 Howard Huang 19

The four-bit adder • The basic four-bit adder always computes S = A +

The four-bit adder • The basic four-bit adder always computes S = A + B + CI. • But by changing what goes into the adder inputs A, B and CI, we can change the adder output S. This is also what we did to build the combined adder-subtractor circuit. • June 25, 2002 Arithmetic-logic units 20

It’s the adder-subtractor again! • Here the signal Sub and some XOR gates alter

It’s the adder-subtractor again! • Here the signal Sub and some XOR gates alter the adder inputs. – When Sub = 0, the adder inputs A, B, CI are Y, X, 0, so the adder produces G = X + Y + 0, or just X + Y. – When Sub = 1, the adder inputs are Y’, X and 1, so the adder output is G = X + Y’ + 1, or the two’s complement operation X - Y. June 25, 2002 Arithmetic-logic units 21

The multi-talented adder • • • So we have one adder performing two separate

The multi-talented adder • • • So we have one adder performing two separate functions. “Sub” acts like a function select input which determines whether the circuit performs addition or subtraction. Circuit-wise, all “Sub” does is modify the adder’s inputs A and CI. June 25, 2002 Arithmetic-logic units 22

Modifying the adder inputs • • By following the same approach, we can use

Modifying the adder inputs • • By following the same approach, we can use an adder to compute other functions as well. We just have to figure out which functions we want, and then put the right circuitry into the “Input Logic” box. June 25, 2002 Arithmetic-logic units 23

Some more possible functions • • We already saw how to set adder inputs

Some more possible functions • • We already saw how to set adder inputs A, B and CI to compute either X + Y or X - Y. How can we produce the increment function G = X + 1? One way: Set A = 0000, B = X, and CI = 1 • How about decrement: G = X - 1? A = 1111 (-1), B = X, CI = 0 • How about transfer: G = X? (This can be useful. ) A = 0000, B = X, CI = 0 This is almost the same as the increment function! June 25, 2002 Arithmetic-logic units 24

The role of CI • • • The transfer and increment operations have the

The role of CI • • • The transfer and increment operations have the same A and B inputs, and differ only in the CI input. In general we can get additional functions (not all of them useful) by using both CI = 0 and CI = 1. Another example: – Two’s-complement subtraction is obtained by setting A = Y’, B = X, and CI = 1, so G = X + Y’ + 1. – If we keep A = Y’ and B = X, but set CI to 0, we get G = X + Y’. This turns out to be a ones’ complement subtraction operation. June 25, 2002 Arithmetic-logic units 25

Table of arithmetic functions • • Here are some of the different possible arithmetic

Table of arithmetic functions • • Here are some of the different possible arithmetic operations. We’ll need some way to specify which function we’re interested in, so we’ve randomly assigned a selection code to each operation. June 25, 2002 Arithmetic-logic units 26

Mapping the table to an adder • • This second table shows what the

Mapping the table to an adder • • This second table shows what the adder’s inputs should be for each of our eight desired arithmetic operations. – Adder input CI is always the same as selection code bit S 0. – B is always set to X. – A depends only on S 2 and S 1. These equations depend on both the desired operations and the assignment of selection codes. June 25, 2002 Arithmetic-logic units 27

Building the input logic • • • All we need to do is compute

Building the input logic • • • All we need to do is compute the adder input A, given the arithmetic unit input Y and the function select code S (actually just S 2 and S 1). Here is an abbreviated truth table: We want to pick one of these four possible values for A, depending on S 2 and S 1. June 25, 2002 Arithmetic-logic units 28

Primitive gate-based input logic • • • We could build this circuit using primitive

Primitive gate-based input logic • • • We could build this circuit using primitive gates. If we want to use K-maps for simplification, then we should first expand out the abbreviated truth table. – The Y that appears in the output column (A) is actually an input. – We make that explicit in the table on the right. Remember A and Y are each 4 bits long! June 25, 2002 Arithmetic-logic units 29

Primitive gate implementation • From the truth table, we can find an MSP: A

Primitive gate implementation • From the truth table, we can find an MSP: A i = S 2 Y i ’ + S 1 Y i • Again, we have to repeat this once for each bit Y 3 -Y 0, connecting to the adder inputs A 3 -A 0. • This completes our arithmetic unit. June 25, 2002 Arithmetic-logic units 30

Bitwise operations • • Most computers also support logical operations like AND, OR and

Bitwise operations • • Most computers also support logical operations like AND, OR and NOT, but extended to multi-bit words instead of just single bits. To apply a logical operation to two words X and Y, apply the operation on each pair of bits Xi and Yi: 1 0 1 1 AND 1 1 1 0 1 0 • OR 1 0 1 1 1 1 1 0 1 1 XOR 1 1 1 0 01 01 We’ve already seen this informally in two’s-complement arithmetic, when we talked about “complementing” all the bits in a number. June 25, 2002 Arithmetic-logic units 31

Bitwise operations in programming • Languages like C, C++ and Java provide bitwise logical

Bitwise operations in programming • Languages like C, C++ and Java provide bitwise logical operations: & (AND)| (OR) • These operations treat each integer as a bunch of individual bits: 13 & 25 = 9 • because 01101 & 11001 = 01001 They are not the same as the operators &&, || and !, which treat each integer as a single logical value (0 is false, everything else is true): 13 && 25 = 1 • • ^ (XOR) ~ (NOT) because true && true = true Bitwise operators are often used in programs to set a bunch of Boolean options, or flags, with one argument. Easy to represent sets of fixed universe size with bits: – 1: is member, 0 not a member. Unions: OR, Intersections: AND June 25, 2002 Arithmetic-logic units 32

Bitwise operations in networking • • IP addresses are actually 32 -bit binary numbers,

Bitwise operations in networking • • IP addresses are actually 32 -bit binary numbers, and bitwise operations can be used to find network information. For example, you can bitwise-AND an address 192. 168. 10. 43 with a “subnet mask” to find the “network address, ” or which network the machine is connected to. 192. 168. 10. 43 = 11000000. 10101000. 0000101011 & 255. 224 = 11111111. 11100000 192. 168. 10. 32 = 11000000. 10101000. 00001010. 00100000 • You can use bitwise-OR to generate a “broadcast address, ” for sending data to all machines on the local network. 192. 168. 10. 43 = 11000000. 10101000. 0000101011 | 0. 0. 0. 31 = 00000000. 00011111 192. 168. 10. 63 = 11000000. 10101000. 00001010. 00111111 June 25, 2002 Arithmetic-logic units 33

Defining a logic unit • • A logic unit supports different logical functions on

Defining a logic unit • • A logic unit supports different logical functions on two multi-bit inputs X and Y, producing an output G. This abbreviated table shows four possible functions and assigns a selection code S to each. We’ll just use multiplexers and some primitive gates to implement this. Again, we need one multiplexer for each bit of X and Y. June 25, 2002 Arithmetic-logic units 34

Our simple logic unit • • Inputs: – X (4 bits) – Y (4

Our simple logic unit • • Inputs: – X (4 bits) – Y (4 bits) – S (2 bits) Outputs: – G (4 bits) June 25, 2002 Arithmetic-logic units 35

Combining the arithmetic and logic units • Now we have two pieces of the

Combining the arithmetic and logic units • Now we have two pieces of the puzzle: – An arithmetic unit that can compute eight functions on 4 -bit inputs. – A logic unit that can perform four functions on 4 -bit inputs. • We can combine these together into a single circuit, an arithmetic-logic unit (ALU). June 25, 2002 Arithmetic-logic units 36

Our ALU function table • • This table shows a sample function table for

Our ALU function table • • This table shows a sample function table for an ALU. All of the arithmetic operations have S 3=0, and all of the logical operations have S 3=1. These are the same functions we saw when we built our arithmetic and logic units a few minutes ago. Since our ALU only has 4 logical operations, we don’t need S 2. The operation done by the logic unit depends only on S 1 and S 0. June 25, 2002 Arithmetic-logic units 37

A complete ALU circuit The / and 4 on a line indicate that it’s

A complete ALU circuit The / and 4 on a line indicate that it’s actually four lines. 4 Cout should be ignored when logic operations are performed (when S 3=1). 4 4 G is the final ALU output. • When S 3 = 0, the final output comes from the arithmetic unit. • When S 3 = 1, the output comes from the logic unit. The arithmetic and logic units share the select inputs S 1 and S 0, but only the arithmetic unit uses S 2. June 25, 2002 Arithmetic-logic units 38

Comments on the multiplexer • • • Both the arithmetic unit and the logic

Comments on the multiplexer • • • Both the arithmetic unit and the logic unit are “active” and produce outputs. – The mux determines whether the final result comes from the arithmetic or logic unit. – The output of the other one is effectively ignored. Our hardware scheme may seem like wasted effort, but it’s not really. – “Deactivating” one or the other wouldn’t save that much time. – We have to build hardware for both units anyway, so we might as well run them together. This is a very common use of multiplexers in logic design. June 25, 2002 Arithmetic-logic units 39

The completed ALU • • This ALU is a good example of hierarchical design.

The completed ALU • • This ALU is a good example of hierarchical design. – With the 12 inputs, the truth table would have had 212 = 4096 lines. That’s an awful lot of paper. – Instead, we were able to use components that we’ve seen before to construct the entire circuit from a couple of easy-to-understand components. As always, we encapsulate the complete circuit in a “black box” so we can reuse it in fancier circuits. 4 4 June 25, 2002 Arithmetic-logic units 40

Arithmetic summary • • In the last few lectures we looked at: – Building

Arithmetic summary • • In the last few lectures we looked at: – Building adders hierarchically, starting with one-bit full adders. – Representations of negative numbers to simplify subtraction. – Using adders to implement a variety of arithmetic functions. – Logic functions applied to multi-bit quantities. – Combining all of these operations into one unit, the ALU. Where are we now? – We started at the very bottom, with primitive gates, and now we can understand a small but critical part of a CPU. – This all built upon our knowledge of Boolean algebra, Karnaugh maps, multiplexers, circuit analysis and design, and data representations. June 25, 2002 Arithmetic-logic units 41