如前所述,当和的符号相同而它们和的符号不同时,会发生有符号整数溢出。
示例(为简洁起见,使用 8 位数字):
0x7F (+127) + 0x00 (+0) = 0x7F (+127)
0x7F (+127) + 0x01 (+1) = 0x80 (-128) overflow
0x80 (-128) + 0x7F (+127) = 0xFF (-1)
0x80 (-128) + 0xFF (-1) = 0x7F (+127) overflow
爱荷华州,
overflow = (sign1 is equal to sign2) AND (sign of sum is NOT equal to sign1)
您可以使用 XOR 表示相等:
0 XOR 0 = 0 - equal
0 XOR 1 = 1 - not equal
1 XOR 0 = 1 - not equal
1 XOR 1 = 0 - equal
接着:
overflow = (NOT(sign1 XOR sign2)) AND (sign of sum XOR sign1)
您可以对这部分进行编码,如下所示:
addu $10,$8,$9 # bit 31 of r10 = sign of sum
xor $10,$10,$8 # bit 31 of r10 = sign of sum XOR sign1
xor $2,$8,$9 # bit 31 of r2 = sign1 XOR sign2
nor $2,$2,$2 # bit 31 of r2 = NOT(sign1 XOR sign2)
and $10,$10,$2 # bit 31 of r10 = (NOT(sign1 XOR sign2)) AND (sign of sum XOR sign1)
现在,如果您想将 r10 的第 31 位扩展到寄存器的所有其他位(这样您将在 r10 中获得全零或全一),您可以使用算术右移指令:
sra $10,$10,31