Electrical – Overflow results into wrong sign-flag bit set in 8085 microprocessor .

8085flag

In case of 8085 microprocessor when the MSB bit of accumulator is 1 then sign flag becomes 1 ( simply copy the result's msb) , my question is that :

When we add two positive numbers say 44H(01000100) and 43H(01000011) , the result would be 10000111 , and thus the sign flag becomes 1 , but the both numbers are positive the how sign becomes 1 ?

How microprocessor is able to handle this , please Help me out ?

Best Answer

You must study binary arithmetic. Sign flag is not enough to identify the result of the signed addition. There should be another flag called overflow which will identify if resulting value is correct or not.

8085 does not have overflow bit, unlike Z80's P/V bit, and you will not be able to identify correctness of the result using S flag only. It will be your task of programmer to ensure the overflow condition either by:

  1. examining signs of input data. In your example, both are positive, but output is negative meaning there's an overflow. In general, values of differing signs will never trigger overflow; if signs of operands are the same, but result's sign is different, the result has overflown.

  2. expanding size of the values to hold overflow bit. For this, in case of 8-bit registers, you may only have 6 significant bits, with two MSBs being sign [7] and overflow [6] respectively. If, after operation, these bits differ, it means overflow condition.

Your example extended to 9 bits

0.0.1000100 + 0.0.1000011 = 0.1.0000111

44h is positive 7-bit value, 43h is positive 7-bit value; result is having its sign and overflow bits set differently, 0 and 1, and it is overflow condition. Another example -

1.1.1100000 + 0.0.1110010 = 1.0.0.1010010 => 0.0.1010010

with carry discarded we see that sign and overflow bits are the same, thus result is correct, and in decimal it is -32 + 114 = 82.

Related Topic