4 Add an additional output to your addersubtractor to indica
4) Add an additional output to your adder/subtractor to indicate overflow. Remember, when adding two positive numbers, obtaining a negative result indicates overflow. Similarly, when adding two negative numbers, a positive result indicates overflow.
Solution
adder/subtractor to indicate overflow: and adding two negative numbers,a positive result indicates overflow: With two operands and one result, we have three sign bits (each 1 or 0) to consider, so we have exactly 2**3=8 possible combinations of the three bits. Only two of those 8 possible cases are considered overflow. Below are just the sign bits of the two addition operands and result: ADDITION SIGN BITS num1sign num2sign sumsign --------------------------- 0 0 0 *OVER* 0 0 1 (adding two positives should be positive) 0 1 0 0 1 1 1 0 0 1 0 1 *OVER* 1 1 0 (adding two negatives should be negative) 1 1 1 We can repeat the same table for subtraction. Note that subtracting a positive number is the same as adding a negative, so the conditions that trigger the overflow flag are: SUBTRACTION SIGN BITS num1sign num2sign sumsign --------------------------- 0 0 0 0 0 1 0 1 0 *OVER* 0 1 1 (subtracting a negative is the same as adding a positive) *OVER* 1 0 0 (subtracting a positive is the same as adding a negative) 1 0 1 1 1 0 1 1 1 A computer might contain a small logic gate array that sets the overflow flag to \"1\" iff any one of the above four OV conditions is met. A human need only remember that, when doing signed math, adding two numbers of the same sign must produce a result of the same sign, otherwise overflow happened. Calculating Overflow Flag: Method 2 ----------------------------------- When adding two binary values, consider the binary carry coming into the leftmost place (into the sign bit) and the binary carry going out of that leftmost place. (Carry going out of the leftmost [sign] bit becomes the CARRY flag in the ALU.) Overflow in two\'s complement may occur, not when a bit is carried out out of the left column, but when one is carried into it and no matching carry out occurs. That is, overflow happens when there is a carry into the sign bit but no carry out of the sign bit. The OVERFLOW flag is the XOR of the carry coming into the sign bit (if any) with the carry going out of the sign bit (if any). Overflow happens if the carry in does not equal the carry out. Examples (2-bit signed 2\'s complement binary numbers): 11 +01 === 00 - carry in is 1 - carry out is 1 - 1 XOR 1 = NO OVERFLOW 01 +01 === 10 - carry in is 1 - carry out is 0 - 1 XOR 0 = OVERFLOW! 11 +10 === 01 - carry in is 0 - carry out is 1 - 0 XOR 1 = OVERFLOW! 10 +01 === 11 - carry in is 0 - carry out is 0 - 0 XOR 0 = NO OVERFLOW Note that this XOR method only works with the *binary* carry that goes into the sign *bit*. If you are working with hexadecimal numbers, or decimal numbers, or octal numbers, you also have carry; but, the carry doesn\'t go into the sign *bit* and you can\'t XOR that non-binary carry with the outgoing carry. Hexadecimal addition example (showing that XOR doesn\'t work for hex carry): 8Ah +8Ah ==== 14h The hexadecimal carry of 1 resulting from A+A does not affect the sign bit. If you do the math in binary, you\'ll see that there is *no* carry *into* the sign bit; but, there is carry out of the sign bit. Therefore, the above example sets OVERFLOW on. (The example adds two negative numbers and gets a positive number.)