How does a processor without an overflow flag perform signed arithmetic?

I know that adding two unsigned integers larger than the bus size of a given processor can be achieved using the carry flag. And, as a rule, the same is true for signed integers using the overflow flag. However, the Intel 8085 has a Sign flag, not an Overflow flag, and how does it deal with signed integer arithmetic?

+4
source share
1 answer

As you know, the overflow flag is applicable only for signed integer arithmetic. On processors in which the ALU has both overflows and carry flags (for example, x86), both of these flags are set in accordance with the result of a binary arithmetic operation, but it is up to the programmer to decide how to interpret them. Signed arithmetic uses the overflow flag; unsigned arithmetic uses the carry flag. Looking at the wrong one gives you meaningless data.

There are two cases where the overflow flag is turned on during a binary arithmetic operation:

  • The inputs of both have sign bits that are off, and the result has a sign bit that is on.
  • The inputs both have signed bits that are turned on, while the result has a signed bit that is turned off.

, , . .

:

  • 0100 + 0001 = 0101 ( )
  • 0100 + 0100 = 1000 ( )
  • 0110 + 1001 = 1111 ( )
  • 1000 + 1000 = 0000 ( )
  • 1000 + 0001 = 1001 ( )
  • 1100 + 1100 = 1000 ( )

, ; , . . , , , . , , . , , , . , .

(, ).

, , ALU . , , , , , - . , .

C- :

// For the binary (two complement) addition of two signed integers,
// an overflow occurs if the inputs have the same sign AND ALSO the
// sign of the result is different from the signs of the inputs.
bool GetOverflowFlagForAddition(int op1, int op2, int result)
{
   return (~(op1 ^ op2) & (op1 ^ result)) < 0;
}

// For the binary (two complement) subtraction of two signed integers,
// an overflow occurs if the inputs have the same sign AND ALSO the
// sign of the result matches the signs of the inputs.
bool GetOverflowFlagForSubtraction(int op1, int op2, int result)
{
   return ((op1 ^ op2) & (op1 ^ result)) < 0;
}

( , , .)

, , Iwillnotexist Idonotexist : " XOR ." , ( ) .

, XOR . 8- : O = C 6 ^ C 7, O "" C "". , : , ( 7).

. , ( 6502, 8- ). 6502.


, ? . , :

  • , ( ).

  • , ( ).

. , :

  • 1111 + 0001 = 0000 ( )
  • 0111 + 0001 = 1000 ( )
  • 0000 - 0001 = 1111 ( )
  • 1000 - 0001 = 0111 ( )

, , , , , :

  • 0000 + 1111 = 1111 ( )
  • 1000 + 1111 = 0111 ( )

... , .


, . , ( ). , , , . OF == CF ^ SF, - , XORed , . , .

, , 8085 Ken Shirriff , , , XORing - C 6 ^ C 7, ALU. ( , , "K", "V" , , , .)

+7

Source: https://habr.com/ru/post/1682055/


All Articles