One instruction to clear PF (Parity Flag) — get od

2019-04-11 13:45发布

问题:

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.

回答1:

Not possible.

None of the PF-changing commands can unconditionally produce an odd-parity result when applied to two copies or 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.



回答2:

Try this:

foo:  cmp byte [foo],0x7F

Note: This cmp instruction's first byte is 0x80, and 0x80-0x7F = 0x01.



回答3:

I think the only way to do it besides mov (I smell interview question) is to find (miraculously, admittedly) a register or register pair that will satisfy TEST src, dst. See here, Operation.

At this moment, no such x86 register/register pair that could satisfy that condition spring to mind.