Byte Swap

  1. This instruction is not supported on processors earlier than the 80486.

Encoding

EncodingOperand
oopcode(0..2)

Description

The BSWAP instruction reverses the byte order of the operand. This instruction can be used to change the endianness of the operand from little to big (or vice versa).

This instruction is only valid on 32 bit and 64 bit operands. To operate on a 16 bit operand, use the XCHG instruction. Use of this instruction on a 16 bit operand is undefined.

Operation

public void BSWAP(ref U32 arg)
{
    U32 temp = arg;
    arg.Bit[0..7] = temp.Bit[24..31];
    arg.Bit[8..15] = temp.Bit[16..23];
    arg.Bit[16..23] = temp.Bit[8..15];
    arg.Bit[24..31] = temp.Bit[0..7];
}

public void BSWAP(ref U64 arg)
{
    U64 temp = arg;
    arg.Bit[0..7] = temp.Bit[56..63];
    arg.Bit[8..15] = temp.Bit[48..55];
    arg.Bit[16..23] = temp.Bit[40..47];
    arg.Bit[24..31] = temp.Bit[32..39];
    arg.Bit[32..39] = temp.Bit[24..31];
    arg.Bit[40..47] = temp.Bit[16..23];
    arg.Bit[48..55] = temp.Bit[8..15];
    arg.Bit[56..63] = temp.Bit[0..7];
}

Flags Affected

None.

Exceptions

Real-Address Mode

#UD
  • If the LOCK prefix is used.

Virtual-8086 Mode

#UD
  • If the LOCK prefix is used.

Protected Mode

#UD
  • If the LOCK prefix is used.

Compatibility Mode

#UD
  • If the LOCK prefix is used.

Long Mode

#UD
  • If the LOCK prefix is used.