Search code examples
x86intel

How to understand "The XX flag is undefined/clear/set" in x86 manual?


According to the x86 instruction description of shld

If the count is 1 or greater, the CF flag is filled with the last bit shifted out of the destination operand and the SF, ZF, and PF flags are set according to the value of the result. For a 1-bit shift, the OF flag is set if a sign change occurred; otherwise, it is cleared. For shifts greater than 1 bit, the OF flag is undefined. If a shift occurs, the AF flag is undefined. If the count operand is 0, the flags are not affected. If the count is greater than the operand size, the flags are undefined.

The above description says how to process CF/OF/... flags. The manual uses "set/clear/undefined/not affected" to describe the manners.

My understand for these words is:

  • set: CPU will set the flag to 1
  • clear: CPU will set the flag to 0
  • undefined/not affected: CPU doesn't care about the flag

Is it correct?


Solution

  • Your understanding is correct regarding set and clear, but "undefined" means that the program can not rely on the CPU being in any particular state after the instruction is run with those particular operands.

    The flags may be set, they might not, they may have changed from their previous state, and then again they might not have. You can not even rely on the same behavior after multiple runs of the same program. The state of those flags after that instruction is run with those operands is simply not defined.