Remix.run Logo
Dwedit 11 hours ago

Because "sub eax,eax" looks stupid. (and also clears the carry flag, unlike "xor eax, eax")

tom_ 11 hours ago | parent | next [-]

xor clears the carry as well? In fact, looks like xor and sub affect the same set of flags!

xor:

> The OF and CF flags are cleared; the SF, ZF, and PF flags are set according to the result. The state of the AF flag is undefined.

sub:

> The OF, SF, ZF, AF, PF, and CF flags are set according to the result.

(I don't have an x64 system handy, but hopefully the reference manual can be trusted. I dimly remembered this, or something like it, tripping me up after coming from programming for the 6502.)

sfink 4 hours ago | parent | next [-]

Strangely, the only difference on the flags is that AF (auxiliary carry) is undefined for `xor eax, eax` but guaranteed to be zeroed for `sub eax, eax`. I don't know what that means in practice, though I'm guessing that at the very least the hardware would not treat it as a dependency on the previous value.

trollbridge 10 hours ago | parent | prev [-]

This is a good thing since the pipeline now doesn’t have to track the state of the flags since they all got zero’d.

HackerThemAll 10 hours ago | parent | prev [-]

If I remember correctly, sub used to be slower than xor on some ancient architectures.