Pregunta

I need a concise way to express the x86 Overflow Flag as a function of the two operands. I understand that the core sets OF when both operands have the same sign but the result has a different sign.

For example,

ADD SRC[31..0], DEST[31..0]

Can OF be reasonably expressed as a "one-liner" boolean function of the bits of SRC and DEST?

¿Fue útil?

Solución

This is how you can express the signed overflow flag value for addition of two signed (2's complement) 32-bit integers:

OV = (((SRC XOR DST) XOR 80000000H) AND ((SRC + DST) XOR SRC) AND 80000000H) ≠ 0

SRC and DST are the entire 32-bit integer values.

The expression essentially compares the signs of the sum and of the addends.

Expressing the overflow solely in terms of individual bits isn't very practical because you'd need to effectively replicate a full 32-bit adder. And that's not a good candidate for one-lining.

If you want a similar expression for subtraction, try to derive it or just express subtraction in terms of addition as is done in the Sbb() function in this answer. It shouldn't be hard.

Using a test to make sure your code works is a good idea, too. You can even write a small assembly routine for addition/subtraction that would return you the overflow flag and use that to test correctness of your one-liner.

Otros consejos

As the overflow flag is the xor of the carry out of the sign bit with the carry in to the sign bit, it could be expressed as:

((SRC[31..0] + DEST[31..0]) shr 32) xor ((SRC[30..0] + DEST[30..0]) shr 31)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top