1

Could you please explain how the INC x86 assembly instruction is affecting the Parity Flag (PF)?

I have the following code:

.text:00401413 mov     edi, offset user_id ; user_id at memory location 0x40D020
...
.text:00401421 inc     edi                 ; edi = 0x40D021
...
.text:0040142D inc     edi
.text:0040142E jnp     short no_parity

With user_id defined as follows:

.bss:0040D020 user_id         db 21h dup(?)
.bss:0040D042                 db    ? ;
.bss:0040D043                 db    ? ;
.bss:0040D044                 db    ? ;
...

At offset 0x40142D:

0:000> r edi
edi=0040d021
0:000> dd 40d021
0040d021  4f4f4f4f 4f4f4f4f 4f4f4f4f 4f4f4f4f
0040d031  4f4f4f4f 4f4f4f4f 4f4f4f4f 004f4f4f
0040d041  00000000 00000000 00000000 00000000
0040d051  00000000 00000000 00000000 00000000
0040d061  00000000 00000000 00000000 00000000
0040d071  00000000 00000000 00000000 00000000
0040d081  00000000 ff000000 01000000 01000000
0040d091  02000000 00000000 01000000 00000000

If my understanding of the parity flag is correct, it should apply to the low 8 bits: 01001111 because:

edi = 0x4f4f4f4f = 0b1001111010011110100111101001111

The number of 1 in 01001111 is odd (five 1), which should set the PF to 1. But this is not the case:

0:000> r pf
pf=1

...which leads to the jump not to be taken at offset 0x40142E.

Thank you for your help.

1

The parity flags is "Set if the least-significant byte of the result contains an even number of 1 bits; cleared otherwise."

Source: section 3.4.3.1 of http://www.intel.com/content/dam/www/public/us/en/documents/manuals/64-ia-32-architectures-software-developer-manual-325462.pdf

So edi = 0x0040d021 => least significant byte = 0x21

0x21 = 0b00100001 = 2 bits set = even number of bits sets => PF set

(It's not clear if the value you give for edi is before or after the inc. If it's before then after the inc edi will equal 0x0040d022, which still has 2 bits set.)

|improve this answer|||||
  • So this is the value of EDI itself that is checked (0x40D021) instead of the value at the memory location it points to (0x4F4F4F4F), right? – Sebastien Damaye Nov 21 '15 at 7:52
  • Exactly. The inc instruction acts on edi and it is the resulting value of edi that is checked. – Ian Cook Nov 21 '15 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.