You already know that you can set or clear bits using bit operations such as and, xor, or, and not. But there are other ways to modify individual bits: bts for setting bits to 1, btr for resetting bits to 0, and bt for testing if a bit is set to 1.
Other Ways to Modify Bits
bits3.asm
We again use the printb.c program here; make sure to adapt your makefile or SASM build settings accordingly.
The variable bitflags is the object of study here; we will be manipulating bits in this variable.
The bitflags Variable
Remember that the bit count (the index) starts at 0. This means that in a byte, which has 8 bits, the first bit is at position 0, and the last bit is at position 7. Setting bits to 1 with the instruction bts and resetting bits to 0 with btr is simple: just specify the index of the bit to be changed as the second operand.
Testing a bit is a bit more complicated. Put the index of the bit to be tested in rax and use the instruction bt. If the bit is 1, the carry flag, CF, will be set to 1; otherwise, CF will be 0. Based on the value of the flag, you can direct your program to execute certain instructions or not. In this case, we use a special instruction setc, a conditional set. In this case, the instruction sets dil to 1 if the carry flag is 1. dil is the lower part of rdi; be careful to set rdi to 0 before using setc to set dil. It might well be that the higher bits of rdx are set during the execution of a previous instruction.
The setc instruction is an example of setCC. setCC sets a byte in the operand if the condition in CC is met, where CC is a flag, such as CF (abbreviated as c), ZF CF (abbreviated as z), SF CF (abbreviated as s), and so on. Take a look in the Intel manuals for more details.
Summary
Setting bits, resetting bits, and examining bits, with btr, bts, and bt
The setCC instruction