BSWAP: Byte Swap

For information about interpreting this page, see the help page.
Opcode and MnemonicEncoding16 bit Mode32 bit Mode64 bit ModeDescription
0F C8+rd
BSWAP r32
OValidValid*ValidReverses the byte order (endianness) of r32.
REX.W 0F C8+rd
BSWAP r64
ONot EncodableNot EncodableValidReverses the byte order (endianness) of r64.
Notes:

* The BSWAP instruction is not supported on processors earlier than the 80486.

Encoding

EncodingOperand
Oopcode[rw]

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 visa versa).

This instruction is only valid on 32 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

This pseudo-code uses C# syntax. A list of the types used is available here.
public void BSWAP_32(ref uint arg)
{
  uint temp = arg;
  arg.Bit[0..8] = temp.Bit[24..32];
  arg.Bit[8..16] = temp.Bit[16..24];
  arg.Bit[16..24] = temp.Bit[8..16];
  arg.Bit[24..32] = temp.Bit[0..8];
}

public void BSWAP_64(ref ulong arg)
{
  ulong temp = arg;
  arg.Bit[0..8] = temp.Bit[56..64];
  arg.Bit[8..16] = temp.Bit[48..56];
  arg.Bit[16..24] = temp.Bit[40..48];
  arg.Bit[24..32] = temp.Bit[32..40];
  arg.Bit[32..40] = temp.Bit[24..32];
  arg.Bit[40..48] = temp.Bit[16..24];
  arg.Bit[48..56] = temp.Bit[8..16];
  arg.Bit[56..64] = temp.Bit[0..8];
}

Exceptions

Protected Mode

#UD
If the LOCK prefix is used.

Real-Address Mode

Same as protected mode.

Virtual-8086 Mode

Same as protected mode.

Compatibility Mode

Same as protected mode.

Long Mode

Same as protected mode.