[译]The 6502 overflow flag explai

2016-11-08  本文已影响35人  Elinx

** 这篇文章是翻译,也是笔记,只做重点摘抄,详情参考原文**

Unsigned binary addition of 80 + 44 yielding 224.Unsigned binary addition of 80 + 44 yielding 224.
M - N = M - N + 256 = M + (256 - N) = M + N的2s补码

也就是减法能用加法算了。但是这里的256会影响carry flag,怎么解决?

Signed addition of 80 and -48 yields a carry, which is discarded.Signed addition of 80 and -48 yields a carry, which is discarded.

80 - 48 = 80 + (256 - 48) = 256 + 32,这里carry flag置位,结果是32

Signed addition of 80 + 80 yields overflow.Signed addition of 80 + 80 yields overflow.

这个时候carry flag没有置位,但是结果却是负数,因为160超出了有符号数可以表示的范围。从此引入了overflow bit

Binary addition, demonstrating the bits that affect the 6502 overflow flag.Binary addition, demonstrating the bits that affect the 6502 overflow flag.
// common difinition,也就是说carry into C7的bit和carry out C7的bit不一致的时候发生溢出
OV = C6 XOR C7

// M7 and N7 are both 0 and C6 is 1 or
// M7 and N7 are both 1 and C6 is 0
V = (!M7&!N7&C6) | (M7&N7&!C6)

// hardware implemation
V = not (((m7 nor n7) and c6) nor ((M7 nand N7) nor c6))

// high level language,也就是说两个输入的符号和输出的符号都不想等的时候溢出
(M^result)&(N^result)&0x80
上一篇 下一篇

猜你喜欢

热点阅读