Search code examples
assemblyx86cpu-registersinteger-overflowctf

Where do registers AL and AX overflow to?


Please tell me what will be the result of the following assembly (pseudo)code?
We have set all bits of the register EAX to 0, so in turn we also have AH, AL, and AX as 0. But when I subtract the lowest bit of AL, we will take the two's complement and add, giving us a bunch of 1s followed by a 0.

XOR eax, eax
SUB al 0x01

So in summary, my question is that on executing the code above, will just AL be full of 1s, or will it also carry over to AX and EAX.


Solution

  • Carry-out from add/sub goes into CF, the carry flag in EFLAGS.

    The operand of sub al, 1 is AL, not EAX. The high 24 bits of EAX can't be affected by an instruction with 8-bit operand size.

    If you wanted to flip the whole EAX to all-ones, you'd need to use sub eax, 1. (or not eax, or dec eax). sub al, 1 is a different instruction, and the difference is that it only affects AL (and EFLAGS), not any bits outside AL.


    giving us a bunch of 1s followed by a 0.

    No. In 2's complement, -1 is represented by a number with all its bits set. For example, in 8-bit it's 0xFF, in 32-bit it's 0xFFFFFFFF. With a 0 bit at the bottom, it would represent -2.

    will just AL be full of 1s, or will it also carry over to AX and EAX.

    Just AL. You could have just tried it yourself (and then asked for an explanation of the result if you were still puzzled).