How does the OV(Overflow flag) of the 8051 work and how does it differentiate between signed and unsigned digits

8051binarymicrocontroller

If a number is 8 bit, the maximum value it can have is 2^8 or (1111 1111).
In the OV flag, the last digit or the MSB decides the sign, but how does the overflow flag know if it is the "signed MSB" or a normal MSB ?

eg. (1111 1111) is the number 256, but I read somewhere that as the MSB is set to 1, it is a negative number. I do not understand this as it is very confusing if the MSB is used as a sign bit or as an ordinary number with a place value.

Best Answer

The operations for signed and unsigned numbers are the same, but signed numbers overflow between bit 6 and 7, while unsigned numbers overflow between bit 7 and 8.

The OV flag represents the overflow into bit 7, while the CY flag represents the overflow into bit 8, so for a signed operation, you'd check the OV flag, while for an unsigned operation you'd check the CY flag after the operation.