问题
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 withpopcnt eax, eax
/not eax
/and eax, 1
. (Or use BMIandn eax, eax, ecx
withecx=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