intel assembly TEST PF flag operation

最后都变了- 提交于 2019-12-11 07:16:08

问题


I'm was doing a manual operation with TEST (Parity flag operation), the problem it's that i can't get the right result, consider this:

ax = 256 = 0000 0001 0000 0000

so if i do:

test ah, 0x44

the PF flag operation should be:

0000 0000 = 0000 0001 & 0100 0100

PF = 0000 0000 XNOR 0000 0000

PF = 1111 1111?

I've followed the intel reference, according to this:

the question it's what i'm doing wrong?


回答1:


BitwiseXNOR is a horizontal XNOR of the bits, producing a single bit. Remember that PF is only 1 bit wide (a flag in EFLAGS), so it makes no sense to write PF=1111 1111.


It's the same parity calculation as always for instructions that "set flags according to the result", except that it's done on test's internal temporary result. (And as always, on the low 8 bits of it, regardless of operand size).

PF = 0 if the number of set bits is odd, PF = 1 if the number of set bits is even. So yes, the expression you posted in comments, ~(0^0^0^0^0^0^0^0) is correct.

See also:

  • https://en.wikipedia.org/wiki/Parity_flag
  • https://en.wikipedia.org/wiki/Parity_bit points out that parity is also the inverted (sum mod 2) of all the bits. (Because XOR is add-without-carry). Parity of an integer wider than 8 bits can be computed with popcnt eax, eax / not eax / and eax, 1. (Or use BMI andn eax, eax, ecx with ecx=1 set outside a loop to do the not-and part in one instruction.) Or just use the inverted parity value, where 1 = odd parity.

This answer has several examples of how PF is set from the horizontal-XOR of the bits in a result. In your case, the 8 bits are the AND result that test internally generates.



来源:https://stackoverflow.com/questions/47404539/intel-assembly-test-pf-flag-operation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!