Search code examples
assemblyx86-16emu8086eflags

Why parity flag is 1 while number of ones is odd


After executing the following two instructions:

MOV BX, 0FD51H                                        
DEC BX

I get parity flag = 1 (indicating an even number of ones)

However, the binary representation of the decremented value is:

1111 1101 0101 0000

It has 9 ones (i.e, odd number of ones).

Also, executing NEG BX after that results in PF = 0. However, the 2's complement is:

0000 0010 1011 0000 has even number of ones. So I expect PF = 1.


Solution

  • From Intel's manual (section 3.4.3.1 Status Flags):

    Parity flag — Set if the least-significant byte of the result contains an even number of 1 bits; cleared otherwise.

    The least significant byte after DEC BX is 50h (i.e. 1010000b), which has an even number of ones. So you'll get PF=1.

    Similarly, after the NEG, the least significant byte is B0h (10110000b), which has an odd number of ones. So you get PF=0.