btr

Bit test and reset

Copies the selected bit into CF, then clears that bit in the operand.

386 and latertakes lock

Intel's reference: BTR

CFPFAFZFSFDFOF ●??–?–?
  • ● set from the result
  • ? undefined afterwards
  • – not changed

Forms

Operands

r/m
A general-purpose register or memory location matching the operation’s size, for example rax or qword [rbp-8]. The byte registers ah, bh, ch and dh cannot be used with a REX prefix, including when the memory address uses r8–r15.
reg
A general-purpose register matching the operation’s size, for example al, ax, eax or rax. The byte registers ah, bh, ch and dh cannot be used with a REX prefix, which is required by spl, bpl, sil, dil and r8–r15.
imm8
An 8-bit immediate, -128 to 255. Instructions that sign-extend it to a wider operand interpret the byte as -128 to 127; shift counts do not use this sign extension.

bt leaves the operand unchanged; btc complements that bit, btr clears it and bts sets it.

For a register operand, the index wraps modulo its width: 16, 32 or 64 bits, so bt rax, 63 tests the top bit of rax. For a memory operand, a register index is a signed bit offset into a bit string and can select a bit outside the first word, doubleword or quadword. An immediate index is one byte, and the processor takes it modulo the operand width even for a memory operand. NASM encodes the number as written, so bt dword [rax], 40 tests bit 8 of the doubleword at rax. To reach a bit beyond the operand, move the address instead (bt dword [rax + 4], 8 tests bit 40 of the string) or put the index in a register.

global _start

section .text
_start:
        mov rax, 12
        mov rbx, 5
        mov rcx, 3
        mov rsi, 9

        btr rax, rbx

        mov rax, 60         ; syscall 60: exit
        xor rdi, rdi
        syscall