# 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 ?

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

## 3 Answers

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  and overflow  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

Short answer:

Sign flag is only dependent on the bit D7 of the result you get in the accumulator and nothing else.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9081925,"math_prob":0.9115051,"size":1398,"snap":"2021-21-2021-25","text_gpt3_token_len":357,"char_repetition_ratio":0.14131995,"word_repetition_ratio":0.0,"special_character_ratio":0.28683835,"punctuation_ratio":0.16107382,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.952403,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-23T05:47:44Z\",\"WARC-Record-ID\":\"<urn:uuid:9c6fda1a-b034-4b30-b9cb-6b330e05969d>\",\"Content-Length\":\"178906\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b8d3a597-5bf4-407f-b6cc-3e7279d5fdd9>\",\"WARC-Concurrent-To\":\"<urn:uuid:d4547c5c-ee1d-4e30-ad7d-e8dbd3dd8e0a>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://electronics.stackexchange.com/questions/307873/overflow-results-into-wrong-sign-flag-bit-set-in-8085-microprocessor\",\"WARC-Payload-Digest\":\"sha1:Z5Q65BABFAFUDUNYY73QQAACE2FESIKK\",\"WARC-Block-Digest\":\"sha1:25HVC3HVFM4VMCUDYFU6NTPMLOC5QWQE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623488534413.81_warc_CC-MAIN-20210623042426-20210623072426-00000.warc.gz\"}"}