0

Let's say I have the number 0xFFFFFFFFFFFFFFFF in %rdi, and 0x7FFFFFFFFFFFFFFF in %rsi. Let's say I perform a subl %esi, %edi. Would the zero flag be set as 1, or 0? Or what if I perform an addl on the lower 32-bits, causing an overflow. So addl %esi, %edi. Would my CF/OF flag and zero flags be set to 1, or would the ZF flag stay zero?

  • 6
    In a 32 bit instruction, the flags are set according to the 32 bit result. What is in the other 32 bit of the register doesn't matter. – fuz Mar 17 '21 at 17:56
  • Okay, thank you! I had a feeling that was the case. – In Doing It Mar 17 '21 at 17:57
  • I had a brain fart and treated it as 64 bit instead of a 32 bit number. Hence why the question might not make sense. Allow me to fix it. – In Doing It Mar 17 '21 at 18:07
  • 3
    Not that it affects the FLAGS-setting, but writing the 32-bit result to the destination will also implicitly zero-extend to 64 bits. ([Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?](https://stackoverflow.com/q/11177137)). SF and so are still set according to the MSB of the 32-bit result, otherwise SF would be a lot less useful, and you wouldn't actually be able to fully do 8, 16, or 32-bit stuff that required FLAGS. – Peter Cordes Mar 17 '21 at 18:10
  • 2
    BTW, in your example, neither `sub %esi, %edi` nor `sub %rsi, %rdi` would carry or overflow, so that's not a great test case for CF and OF. (`-1 - LONG_MAX = LONG_MINX` for a 2's complement system like x86. Or `-1 - (-1) = 0` for 32-bit operand-size). Perhaps you meant the other order? Not that you need to test it; it works exactly like on a legacy 32-bit-only CPU where there are no upper bits, so porting some chunks of 32-bit asm may just need re-assembly. – Peter Cordes Mar 17 '21 at 18:14

0 Answers0