3

这是一个非常幼稚的问题(我知道),但我认为这将是一个很好的起点,可以考虑如何实际执行 CPU 的基本指令集:

在二进制补码系统中,您不能反转您的实现可以表示的最大负数的符号。这样做的理论原因很明显,因为最大负数的取反将超出实现的范围(范围始终为
-128 到 127)。

但是,当您尝试对最负数执行否定运算时实际发生的情况非常奇怪。例如,在 8 位表示中,最大负数是 -128,或二进制的 1000 0000。通常,要否定一个数字,您会翻转所有位,然后加一个。但是,如果您尝试使用 -128 执行此操作,您最终会得到:

1000 0000 ->
0111 1111 ->
1000 0000

与您开始时使用的号码相同。出于这个原因,维基百科称其为“奇怪的数字”。

在同一篇维基百科文章中,它说上述否定

被检测为溢出条件,因为最高有效位进位但没有进位。

所以我的问题是:
A)这到底是什么意思?和
B) 似乎 CPU 每次执行基本算术运算时都需要执行额外的错误检查步骤,以避免与此否定相关的事故,从而产生大量开销。如果是这种情况,为什么不截断可以表示的数字范围以将奇怪的数字排除在外(即 8 位为 -127 到 127)?如果不是这样,您如何在不产生额外开销的情况下实施此类错误检查?

4

4 回答 4

4

来自 MSB 的进位位用作标志以指示我们需要更多位。没有它,我们将拥有一个模算术系统1,而无法检测何时回绕。

In modular arithmetic, you don’t deal with numbers but with equivalence classes of numbers that have the same remainder. In such a system, after adding 1 to 127, you would get −128, and you would conclude that +128 and −128 belong to the same equivalence class.

If you restricted yourself to numbers in the range −127 to +127, you would have to redefine addition, since 127 + 1 = −127 is nonsense.

Two’s-complement arithmetic, when presented to you by a computer, is essentially modular arithmetic with the ability to detect an overflow.

This is what a 4-bit adder would look like when adding 0001 to 0111. You can see that in the MSB the carry-in and carry-out are different:

     0        0        0        1
     | 0      | 1      | 1      | 1
     | |      | |      | |      | |
     v v      v v      v v      v v
0 <- ADD <-1- ADD <-1- ADD <-1- ADD <- 0
^     |    ^   |        |        |
      v        v        v        v
      1        0        0        0

It is this flag that the ALU uses to signal that an overflow occurred, without any extra steps.

1. Modular arithmetic goes from 0 to 255 instead of −127 to 128, but the basic idea is the same.

于 2010-10-28T20:16:28.433 回答
3

并不是 CPU 进行另一次检查,而是晶体管被安排在发生这种情况时注意到。它们是这样建造的,因为工程师在开始设计之前就选择了二补码。

结果是它发生在与返回非溢出结果相同的时钟周期内。


它是如何工作的?

“加 1”阶段实现了级联逻辑:从 LSB 开始,每个位依次接受真值表

old-bit  carry-in  new-bit  carry-out
-------------------------------------
   0        0        0         0
   0        1        1         0
   1        0        1         0
   1        1        0         1

(即new-bit = old-bit xor carry-incarry-out = old-bit and carry-in)。LSB 的“进位”是我们要添加的 1,而对于其余位,它是前一个位的“进位”(这就是为什么必须在级联中完成) .

这些电路中的最后一个只是为 增加了一个电路signed-overflow = (carry-in and not carry-out)

于 2010-10-28T19:29:23.577 回答
2

First off the wikipedia article states it cannot be negated from a negative signed number to a signed number. And what they mean is because it takes 9 bits to represent positive 128, which you cannot do with an 8 bit register. If you are going from negative signed to positive unsigned as a conversion, then you have enough bits. And the hardware should give you 0x80 when you negate 0x80 because that is the right answer.

For add, subtract, multiply, etc addition in twos complement is no different than decimal math from elementary school. You line up your binary numbers, add the columns, the result for that column is the least significant digit and the rest is carried over to the next column. So adding a 0b001 to 0b001 for example

 1
001
001
===
010

Add the two ones in the rightmost column, the result is 0b10 (2 decimal), write zero then carry the one, one plus zero plus zero is one, nothing to carry, zero plus zero is zero, the result is 0b010.

The right most column where 1 plus 1 is 0b10 and we write 0 carry the one, well that carry the one is at the same time the carry out of the right most column and is the carry in of the second column. Also, with pencil and paper math we normally only talk about carry of something when it is non-zero but if you think about it you are always carrying a number like our second columns one plus zero is one carry the zero.

You can think of a twos complement negate as invert and add one, or walking the bits and inverting up to a point then not inverting, or taking the result of zero minus the number.

You can work subtract in binary using pencil and paper for what it is worth, makes your head hurt when borrowing compared to decimal, but works. For what you are asking though think of invert and add one.

It is easier to wrap your head around this if you take it down to even fewer bits than 8, three is a manageable number, it all scales from there.

So the first column below is the input, the second column is the inverted version and the third column is the second column plus one. The fourth column is the carry in to the msbit, the fifth column is the carry out of the msbit.

000 111 000 1 1
001 110 111 0 0
010 101 110 0 0
011 100 101 0 0
100 011 100 1 0
101 010 011 0 0
110 001 010 0 0
111 000 001 0 0

Real quick look at at adding a one to two bits:

00+1 = 001
01+1 = 010
10+1 = 011
11+1 = 100

For the case of adding one to a number, the only case where you carry out from the second bit into the third bit is when your bits are all ones, a single zero in there stops the cascading carry bits. So in the three bit inversion table above the only two cases where you have a carry into the msbit is 111 and 011 because those are the only two cases where those lower bits are all set. For the 111 case the msbit has a carry in and a carry out. for the 011 case the msbit has a carry in but not a carry out.

So as stated by someone else there are transistors wired up in the chip, if msbit carry in is set and msbit carry out is not set then set some flag somewhere, otherwise clear the flag.

So note that the three bit examples above scale. if after you invert and before you add one you have 0b01111111 then you are going to get a carry in without the carry out. If you have 0b11111111 then you get a carry in and a carry out. Note that zero is also a number where you get the same number back when you invert it, the difference is that when the bits are considered as signed, zero can be represented when negated, 1 with all zeros cannot.

The bottom line though is that this is not a crisis or end of the world thing there is a whole lot of math and other operations in the processor where carry bits and significant bits are falling off one side or the other and overflows and underflows are firing off, etc. Most of the time the programmers never check for such conditions and those bits just fall on the floor, sometimes causing the program to crash or sometimes the programmer has used 16 bit numbers for 8 bit math just to make sure nothing bad happens, or uses 8 bit numbers for 5 bit math for the same reason.

Note that the hardware doesnt know signed or unsigned for addition and subtraction. also the hardware doesnt know how to subtract. Hardware adders are three bit adders (two operands and carry in) with a result and carry out. Wire 8 of these up you have an 8 bit adder or subtractor, add without carry is the two operands wired directly with a zero wired in as the lsbit carry in. Add with carry is the two operands wired directly with the carry bit wired to the lsbit carry in. Subtract is add with the second operand inverted and a one on the carry in bit. At least from a high level perspective, that logic can all get optimized and implemented in ways often two hard to understand on casual inspection.

The really fun exercise is multiply, think about doing binary multiplication with pencil and paper, then realize it is much easier than decimal, because it is just a series of shifts and adds. given enough gates you can represent each result bit as a equation with the inputs to the equation being the operands. meaning you can do a single clock multiply if you wish, in the early days that was too many gates, so multi clock shift and adds were done, today we burn the gates and get single clock multiplies. Also note that understanding this also means that if you do say a 16 bit = 8 bit times 8 bit multiply, the lower 8 bit result is the same whether it is a signed multiply or unsigned. Since most folks do things like int = int * int; you really dont need a signed or unsigned multiply if all you care about is the result bits (no checking of flags, etc). fun stuff..

于 2010-10-28T21:53:41.293 回答
0

In the ARM Architecture Manual (DDI100E):

OverflowFrom
     Returns 1 if the addition or subtraction specified as its parameter 
     caused a 32-bit signed overflow. [...]
     Subtraction causes an overflow if the operands have different signs,
     and the first operand and the result have different signs.

NEG
     [...]
     V Flag = OverflowFrom(0 - Rm)

NEG is the instruction for computing the negation of a number, i.e. the twos complement.

The V flag signals signed overflow and can be used for conditional branching. It's fairly standard across different processor architectures, together with the three other flags Z (zero), C (carry) and N (negative).

For 0 - (-128) = 0 + 128 = -128 the first operand is 0 and the second operand as well as the result is -128, so the condition for overflow is satisfied, and the V flag is set.

于 2010-10-29T09:25:42.747 回答