 This time we're going to be looking at how we can detect overflow. Overflow occurs when the results of an ALU operation can't be represented in the amount of space that we have available for them. So for example, if I add two 32-bit numbers together and I get something that would require 33 bits to actually represent, then I have overflow. This can happen either on the positive side or on the negative side, but I usually just get something that's too big to actually represent given our hardware. There are two ways to detect overflow for two's complement. Our overflow occurs when we have, say, two positive numbers that come in and the result ends up looking like a negative number. When we have two positive numbers, the most significant bit is zero. But if we add two really, really large positive numbers together, we might get... The most significant bit might get changed to a one, in which case the number would look like it's negative. So we could detect this by, say, looking at the two inputs coming in, seeing if they're both positive or both negative, and then checking to see if the result coming out of the adder is the same sign or not. If they're different signs, then we have overflow. Over here I've got a diagram of the last one-bit ALU in my stack. This is the one that's handling the most significant two bits of my inputs, say A31 and B31. So it produces the most significant output. You can also see that it's the one that computes the set line. This is the one that's routed back to the last line at the very top of the ALU, where the rest of the last lines are all zero, and not an interesting. So we're going to try to add in the hardware to actually implement overflow. So if I'm going to detect overflow by noticing that both of my inputs are, say, positive and my result is negative, then I first want to make sure that both of these inputs are the same. Because if one input is positive and the other one is negative, then even if I add them together, the results are going to be smaller in magnitude than at least one of them. So I'm only going to be worried about the case where both of them have the same sign. I'm going to use an XOR gate for this. XOR gates are great because they tell you if two inputs have different parity. I'm interested in whether or not they have the same parity, so I'm actually going to use a NOT gate after the XOR. Then I can take in route my A and my B over to my NOT XOR gate. Now I can tell whether my two inputs are the same or not. Now I want to know if, given that they have the same inputs, whether the outputs are the same. So I really only need to worry about A or B, and I'm interested in whether they have different parity this time. This is the result of my addition, so I'd have another XOR gate because I'm interested in whether they have different parity this time. I will just pick the B line to send over there to the other XOR gate. If they both have the same parity, then it doesn't matter whether I send A or B, and if they have different parities, then I don't have overflow anyway. So it won't matter whether I use A or B. So this XOR gate tells me if my inputs have the same parity, this XOR gate tells me if my input has a different parity than my output. If both of these things are true, then I have overflow. So in that case I want an AND gate. In this case I have three gates and they tell me if I've got overflow. I still have to make sure that the results of this are in fact meaningful. I have to make sure that I am doing say an addition operation NOT AND or OR, in which case I probably wouldn't care about overflow at all. But there's another way I can do this as well. It turns out that the carry bits for this ALU are also really, really interesting. As a really simple example that fits our one-bit ALU, let's consider adding one and one. We have two two-bit numbers. We'll say they're in two's complement. I'd like to add these together. It turns out these are the two largest positive numbers I can have with two bits. So 1 plus 1 gives me 0, carry a 1. 1 plus 0 plus 0 gives me 1, carry a 0. So that's interesting. It turns out that, in this case at least, my carry-in for the most significant adder was a 1, and the carry-out was a 0. If I have, say, the two largest negative numbers, the 1, 0, and 1, 0, and negative 2 each, add those together. 0 plus 0 gives me 0, so I'd carry a 0. 0 plus 1 plus 1 is 0, carry a 1. So I have something that looks like 0. But again, I have different carry bits. If I do something a little simpler, I add 0 and 1 together while I get 1. And I'm not carrying anything. Add negative 1 to negative 1. So it turns out another way we can detect overflow is by looking at these carry bits. If the carry bit to our most significant adder is different from the carry-out bit, then we also have overflow. So in this case, that's really easy to check because now we just have to check parity for two bits. Carry-in versus carry-out. Now my carry-in is all the way over here. And there's my carry-out. So if you do it that way, I only need one gate to detect overflow. Still the same problems as the other one that I still have to make sure it's meaningful. But this would allow me to detect overflow for two's complement.