# `and`

[x86-64 Documentation](https://asm-editor.specy.app/documentation/x86.md) › [Instructions](https://asm-editor.specy.app/documentation/x86/instruction.md)

Web page: https://asm-editor.specy.app/documentation/x86/instruction/and

**Logical AND**

Category: Logic

Combines the destination and source bit by bit with AND, storing the result in the destination.

8086 and later

[Intel's reference: AND](https://intel.github.io/SDM/instruction/AND.html)

Used in: [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arithmetic-logic-and-bits.md) (x86-64 assembly), [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/x86/examples/bit-tricks.md) (x86-64 assembly), [Stack arguments and a stack frame](https://asm-editor.specy.app/learn/courses/x86/examples/subroutine-with-stack-arguments.md) (x86-64 assembly)

## Flags

| Flag | Effect |
| --- | --- |
| CF | always 0 |
| PF | depends on the result |
| AF | undefined (do not rely on it) |
| ZF | depends on the result |
| SF | depends on the result |
| DF | not changed |
| OF | always 0 |

## Forms

- `and r/m, reg` (8, 16, 32, 64) · accepts the `lock` prefix with a memory operand
  - `and r/m8, reg8`
  - `and r/m16, reg16`
  - `and r/m32, reg32`: 386 and later
  - `and r/m64, reg64`
- `and reg, r/m` (8, 16, 32, 64)
  - `and reg8, r/m8`
  - `and reg16, r/m16`
  - `and reg32, r/m32`: 386 and later
  - `and reg64, r/m64`
- `and r/m, imm` (8, 16, 32, 64) · accepts the `lock` prefix with a memory operand
  - `and r/m16, imm8`
  - `and r/m32, imm8`: 386 and later
  - `and r/m64, imm8`
  - `and r/m8, imm8`
  - `and r/m16, imm16`
  - `and r/m32, imm32`: 386 and later
  - `and r/m64, imm32`
- `and acc, imm` (8, 16, 32, 64)
  - `and al, imm8`
  - `and ax, imm16`
  - `and eax, imm32`: 386 and later
  - `and rax, imm32`

## Operands

- `r/m`: A general-purpose register or memory location matching the operation’s size, for example `rax` or `qword [rbp-8]`.
- `reg`: A general-purpose register matching the operation’s size, for example `al`, `ax`, `eax` or `rax`.
- `imm`: An immediate value: a constant written in the instruction as a number, a character or a label, for example `10`, `0x1f`, `'a'` or `msg`. Open a row to see the size it has in each form, such as `imm8` or `imm32`.
- `acc`: The accumulator: `al`, `ax`, `eax` or `rax`, whichever matches the operation’s size. The form accepts no other register in this position.

## Description

A bit is set only when both input bits are set. CF and OF are cleared.

The destination is a register or memory operand; the source is a register, memory operand or immediate, with at most one memory operand. Register and memory operands have the same width. When the instruction writes a 32-bit register, that write clears the upper half of its 64-bit register.

An 8-bit immediate used with a wider operand is sign-extended from −128 through 127. For a 64-bit operation, the longer immediate field is only 32 bits and is also sign-extended; there is no full 64-bit immediate form. The encoded bits determine the value used by the operation. For example, a 32-bit field of `0xffffffff` becomes `0xffffffffffffffff` (−1), not positive 4294967295. An out-of-range constant can produce a warning and truncated bits; use a register for a value that cannot be represented by these sign extensions.

NASM normally chooses the short immediate encoding when it preserves the value; a `byte` qualifier is not required. `strict` with an immediate size can prevent that shortening.

For example, `and rax, 0xffffffff` does not clear the upper 32 bits: its immediate is sign-extended to all ones. `mov eax, eax` clears the upper half while preserving the low 32 bits.

## Example

```x86
global _start

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

        and rax, rbx

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