Intel 64 and IA-32 Architectures Software Developers Manual Volume 1, Basic Architecture

Vol. 1 7-19
PROGRAMMING WITH GENERAL-PURPOSE INSTRUCTIONS
7.3.7 Bit and Byte Instructions
These instructions operate on bit or byte strings. For the purpose of this discussion,
they are further divided subordinate subgroups that:
Test and modify a single bit
Scan a bit string
Set a byte given conditions
Test operands and report results
7.3.7.1 Bit Test and Modify Instructions
The bit test and modify instructions (see Table 7-3) operate on a single bit, which can
be in an operand. The location of the bit is specified as an offset from the least signif-
icant bit of the operand. When the processor identifies the bit to be tested and modi-
fied, it first loads the CF flag with the current value of the bit. Then it assigns a new
value to the selected bit, as determined by the modify operation for the instruction.
7.3.7.2 Bit Scan Instructions
The BSF (bit scan forward) and BSR (bit scan reverse) instructions scan a bit string in
a source operand for a set bit and store the bit index of the first set bit found in a
destination register. The bit index is the offset from the least significant bit (bit 0) in
the bit string to the first set bit. The BSF instruction scans the source operand low-to-
high (from bit 0 of the source operand toward the most significant bit); the BSR
instruction scans high-to-low (from the most significant bit toward the least signifi-
cant bit).
7.3.7.3 Byte Set on Condition Instructions
The SETcc (set byte on condition) instructions set a destination-operand byte to 0 or
1, depending on the state of selected status flags (CF, OF, SF, ZF, and PF) in the
EFLAGS register. The suffix (cc) added to the SET mnemonic determines the condi-
tion being tested for.
For example, the SETO instruction tests for overflow. If the OF flag is set, the desti-
nation byte is set to 1; if OF is clear, the destination byte is cleared to 0. Appendix B,
Table 7-3. Bit Test and Modify Instructions
Instruction Effect on CF Flag Effect on Selected Bit
BT (Bit Test) CF flag Selected Bit No effect
BTS (Bit Test and Set) CF flag Selected Bit Selected Bit 1
BTR (Bit Test and Reset) CF flag Selected Bit Selected Bit 0
BTC (Bit Test and
Complement)
CF flag Selected Bit Selected Bit NOT (Selected Bit)