Bit Scan Forward

Encoding

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

Description

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

ZF is set (due to the source being zero), the result in the destination operand is undefined.

Operation

public void BSF(ref U16 dest, U16 src)
{
    if (src == 0)
        return; // `dest` is undefined

    U16 idx = 0;
    while (src.Bit[idx] == 0)
        idx++;
    dest = idx;
}

public void BSF(ref U32 dest, U32 src)
{
    if (src == 0)
        return; // `dest` is undefined

    U32 idx = 0;
    while (src.Bit[idx] == 0)
        idx++;
    dest = idx;
}

public void BSF(ref U64 dest, U64 src)
{
    if (src == 0)
        return; // `dest` is undefined

    U64 idx = 0;
    while (src.Bit[idx] == 0)
        idx++;
    dest = idx;
}

Flags Affected

CF (carry flag)
Undefined.
PF (parity flag)
Undefined.
AF (auxiliary flag)
Undefined.
ZF (zero flag)
Set according to the source.
SF (sign flag)
Undefined.
OF (overflow flag)
Undefined.

Exceptions

Real-Address Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.

Virtual-8086 Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand (using a segment other than
  • SS
  • ) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Protected Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Compatibility Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the
  • SS
  • segment has an effective address that is outside the
  • SS
  • segment's limit.
#GP(0)
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.

Long Mode

#UD
  • If the
  • LOCK
  • prefix is used.
#SS(0)
  • If a memory operand using the SS segment is in non-canonical form.
  • If a memory operand using the SS segment has an effective address that is outside the SS segment's limit.
#GP(0)
  • If a memory operand (using a segment other than SS) is in non-canonical form.
  • If a memory operand uses a segment containing a NULL selector.
  • If a memory operand (using a segment other than SS) has an effective address that is outside the segment's limit.
#PF(fc)
  • If a page fault occurs.
#AC(0)
  • If alignment checking is enabled while the current privilege level is 3 and an unaligned memory access is made.