0

Im currently learning 8086 assembly at school, im using emu8086, i was playing around with some signed arithmetic and this occured.

if i use the SUB instruction to subtract 15(0FH) from -16(F0H) with the help of this code :

mov al,-16

sub al,15

i'll get-31 in 2s complement(E1H in hex), now the result isnt what im concerened about,im more concerned about the flags that were set after this operation, after this instruction the flags that were set were :

CF = 0

SF = 1

PF = 1

AF = 1

OF = 0

but if i do the subtraction operation with addition (ADD) instruction by typing the following code:

mov al,-16

add al,-15

i get the same result which is E1H but the flags that were set are different, the flags that were set after this arithmetic instruction are:

CF = 1

SF = 1

PF = 1

AF = 0

OF = 0(same with the sub instruction)

i understand why the flags are set because i did the arithmetic on paper myself,but what i dont understand is why the flags are different even though addition and subtraction are equivalent since if you want to subtract numbers, you can just add it this way : x-y= x+(-y) which in the end gives the same result.

blake
  • 107
  • 1
  • 7
  • 2
    In a nutshell, this is because the 8086 does not perform subtraction by the formula A = B + ¬B + 1 as e.g. an ARM chip does, but as true subtraction. – fuz Aug 16 '22 at 17:35
  • @fuz,oh i see,so if i may ask, when does the overflow flag get set when there is a SUB instruction? i know that for the ADD instruction, it gets set when there is unsigned overflow ,and for signed addition OF flag will be set when the the MSB of the two numbers are on and the MSB of the result is off or when the MSB of the two numbers are off and the MSB of the result is on. does this still apply somehow to subtraction aswell? – blake Aug 16 '22 at 17:56
  • 2
    OF gets set on signed overflow both for `SUB` and `ADD`. Unsigned overflow (carry/borrow) is indicated by CF. – fuz Aug 16 '22 at 18:01

0 Answers0