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

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 ?

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.

• The 8085 does have the needed flags. it's undocumented because it's part of the series of instructions Intel added to the 8085 for high level languages and then for what appears to have been business reasons never documented. See righto.com/2013/02/looking-at-silicon-to-understanding.html. The answer above is still excellent and required knowledge for many other processors. – Alan Cox Feb 16 '19 at 10:58

Adding signed integers which yield a sum outside of the range of representation is formally invalid.

To make it practically work, you would need software logic to figure out what happened and create a correct result in some viable representation, for example you could test that both operands were positive and re-interpret the result as a positive value in a wider (or simply unsigned) format.

However the most common higher-level language scheme (for example in C) is to require that you first change the representation of the arguments to one wide enough to hold the result, so for example you would store your 8-bit values in 16-bit registers and add them to produce a 16-bit result. While in one view inefficient (if this is wider than your ALU and thus requiring multiple additions), from another perspective this actually is efficient, since the operations are always the same irrespective of the data, and in (later) pipelined and otherwise accelerated designs, making program-flow decisions can cost more time than performing arithmetic.

• can 8085 microprocessor handle this ? – Minion May 28 '17 at 21:27
• The meaningful question is if the programmer has handled it. The processor does precisely what the manual (+/- errata) say it should, it is up to the programmer to combine those operations to produce a meaningful result, for example multiple 8-bit operations to produce a 16-bit result. – Chris Stratton May 28 '17 at 21:29