Assembly is Too High-Level: BSWAPin 16-bit Registers 1



But what actually happens?

As it turns out, the Intel manual is correct in stating that you should use xchg instead of bswap. In practice, it’s hard to say the result of this 16-bit bswap is ‘undefined;’ as it is consistent with what it does each time. Instead of swapping the contents of ah and al 8-bit registers within ax, it actually just clears the register to 0x00. I tried a lot of different values to test this, and it always zero’s it out. I also tried fully loading up eax, and bswap’ing any value for ax just cleared the ax part, leaving the upper part of eax intact.

So in practice: bswap reg16 = xor reg16, reg16 = mov reg16, 0 (were both reg16’s are the same register in the xor)

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Assembly is Too High-Level: BSWAPin 16-bit Registers

  • Jilles Tjoelker

    The Intel manual’s alternative is suboptimal. A ROR or ROL by 8 bits is faster on most CPUs than an XCHG of the high and low 8-bit registers and also works on si, di, bp and r8w-r15w.