intel assembly TEST PF flag operation
Asked Answered
R

1

1

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:

enter image description here

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

Racquelracquet answered 21/11, 2017 at 3:6 Comment(6)
If you are trying to set or reset PF flag you can do that with lahf instruction have a look at my answer #41336153Barney
@Doda No, I want to do the formula manually, the problem it's that i'm not getting the same result than the program operation...Racquelracquet
You're reading the reference incorrectly. First, the parameter is TEMP[0:7] but you are using TEMP[15:8]. Second, BitwiseXNOR has only one parameter, so it's not clear why you are calculating 0000 0000 XNOR 0000 0000 (especially since the result has 8 bits, but PF is only one bit). What BitwiseXNOR means is to XOR all the bits together, and then negate the result.Sensation
I don't understand Are you trying to set/reset PF flag ?Barney
@Raymond Chen Can you post a example or link example please? i can't find a operation done like example, and you mean bit0 to bit7 xorged and then negated? something like this ~(0^0^0^0^0^0^0)?Racquelracquet
@Doda No. just i want to do the operation manually...Racquelracquet
S
5

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.

Scouring answered 21/11, 2017 at 4:23 Comment(1)
yes, i had a bad logic about BitwiseXNOR(Temporary[0:7]); because that i'm was doing wrong... thank you...Racquelracquet

© 2022 - 2024 — McMap. All rights reserved.