| Opcode | Encoding | 16-bit | 32-bit | 64-bit | Description |
|---|---|---|---|---|---|
0F BC /rBSF r16, r/m16 | rm | Valid | Valid | Valid | Stores the bit position of the least significant set bit from r/m16 into r16. |
0F BC /rBSF r32, r/m32 | rm | Valid | Valid | Valid | Stores the bit position of the least significant set bit from r/m32 into r32. |
REX.W 0F BC /rBSF r64, r/m64 | rm | N/E | N/E | Valid | Stores the bit position of the least significant set bit from r/m64 into r64. |
Encoding
| Encoding | Operand 1 | Operand 2 |
|---|---|---|
rm | ModRM.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
NULLselector. - 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
NULLselector. - 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
SSsegment is in non-canonical form. - If a memory operand using the
SSsegment has an effective address that is outside theSSsegment'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
NULLselector. - 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.