In x86 assembly, is it possible to clear the Parity Flag in one and only one instruction, working under any initial register configuration?
This is equivalent to creating a result register with an odd number of bits, with any operation that sets flags (expressly excluding mov
).
For contrast, setting the parity flag can be done in one instruction:
cmp bl, bl
And there are many ways to clear the parity flag with two instructions:
and bl, 0
or bl, 1
However, the one-instruction method remains elusive.
See other answers for tricks like using memory contents, or newer instructions that always clear PF regardless of the bit-pattern in any registers.
Clearing PF is possible in one instruction, but getting an odd number of bits in a result register from the same instruction appears not to be, without known register contents to start with. (mov al, 1
could of course do just the latter part of the question without affecting FLAGS).
The rest of this answer was written considering only the way proposed in the question: using traditional ALU instructions on registers that set PF according to the parity of the low byte of the result.
None of the PF-changing instruction can unconditionally produce an odd-parity result when applied to two copies of a register (like or al, al
). Likewise, none of the arithmetic commands produces an odd-parity result when applied to a register and a constant that completely defines the result (like and al, 0
or or al, ffh
). As for commands where the second operand is any other constant, the result would depend on the initial value of the register, and we have no control over that.
If we knew some details of the execution environment, it could be possible to use the contents of memory at a well-known address. On PC compatibles in real mode, you can rely on BIOS data structures. In MS-DOS, ditto for executable header. In Windows, there's the TEB as FS:0
.