BSR: Bit Scan Reverse

For information about interpreting this page, see the help page.
Opcode and MnemonicEncoding16 bit Mode32 bit Mode64 bit ModeDescription
0F BD /r
BSR r16, r/m16
RMValidValidValidStores the most significant set bit's position from r/m16 into r16.
0F BD /r
BSR r32, r/m32
RMValidValidValidStores the most significant set bit's position from r/m32 into r32.
REX.W 0F BD /r
BSR r64, r/m64
RMNot EncodableNot EncodableValidStores the most significant set bit's position from r/m64 into r64.

Encoding

EncodingOperand 1Operand 2
RMModRM.reg[w]ModRM.r/m[r]

Description

The BSR instruction searches the source operand for the most significant set bit. The zero-based index of this bit is stored in the destination operand.

The ZF flag is set if the source is 0 (no set bits), and cleared if a bit is found. If the ZF flag is set, the value in the destination operand is undefined.

Operation

This pseudo-code uses C# syntax. A list of the types used is available here.
public void BSR_16(ref ushort dest, ushort src)
{
  if (src == 0)
  {
    ZF = 1;
    return;
    // dest (undefined)
  }

  uint idx = 15;
  while (src.Bit[idx] == 0)
    idx++;
  dest = idx;
  ZF = 0;
}

public void BSR_32(ref uint dest, uint src)
{
  if (src == 0)
  {
    ZF = 1;
    return;
    // dest (undefined)
  }

  uint idx = 31;
  while (src.Bit[idx] == 0)
    idx--;
  dest = idx;
  ZF = 0;
}

public void BSR_64(ref ulong dest, ulong src)
{
  if (src == 0)
  {
    ZF = 1;
    return;
    // dest (undefined)
  }

  uint idx = 63;
  while (src.Bit[idx] == 0)
    idx--;
  dest = idx;
  ZF = 0;
}

Flags Affected

CF (carry flag)
Undefined.
PF (parity flag)
Undefined.
AF (auxiliary flag)
Undefined.
ZF (zero flag)
Set if the source is not zero. Cleared otherwise.
SF (sign flag)
Undefined.
OF (overflow flag)
Undefined.

Exceptions

Protected Mode

#UD
If the LOCK prefix is used.
#GP(0)
If the memory operand's effective address is outside the CS, DS, ES, FS, or GS segment limit.
If the DS, ES, FS, or GS segment register contains a NULL segment selector.
#SS(0)
If a memory operand's effective address is outside the SS segment limit.
#PF(fault-code)
If a page fault occurs.
#AC(0)
If alignment checking is enabled and an unaligned memory reference is made while the current privilege level is 3.

Real-Address Mode

#UD
If the LOCK prefix is used.
#GP
If the memory operand's effective address is outside the CS, DS, ES, FS, or GS segment limit.
#SS
If a memory operand's effective address is outside the SS segment limit.

Virtual-8086 Mode

#UD
If the LOCK prefix is used.
#GP(0)
If the memory operand's effective address is outside the CS, DS, ES, FS, or GS segment limit.
#SS(0)
If a memory operand's effective address is outside the SS segment limit.
#PF(fault-code)
If a page fault occurs.
#AC(0)
If alignment checking is enabled and an unaligned memory reference is made while the current privilege level is 3.

Compatibility Mode

Same exceptions as protected mode.

Long Mode

#UD
If the LOCK prefix is used.
#GP(0)
If the memory address is in a non-canonical form.
#SS(0)
If a memory address referencing the SS segment is in a non-canonical form.
#PF(fault-code)
If a page fault occurs.
#AC(0)
If alignment checking is enabled and an unaligned memory reference is made while the current privilege level is 3.