# `test`

[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/test

**Logical compare**

Category: Comparison

Sets the flags from a bitwise AND of the two operands, without storing the result.

8086 and later

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

Used in: [The flags register](https://asm-editor.specy.app/learn/courses/x86/introduction/the-flags-register.md) (x86-64 assembly), [Loops](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/loops.md) (x86-64 assembly), [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arithmetic-logic-and-bits.md) (x86-64 assembly), [Arrays, strings and the string instructions](https://asm-editor.specy.app/learn/courses/x86/think-in-assembly/arrays-strings-and-the-string-instructions.md) (x86-64 assembly), [syscall and the Linux x86-64 ABI](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/syscall-and-the-linux-abi.md) (x86-64 assembly), [Interrupts, exceptions and signals](https://asm-editor.specy.app/learn/courses/x86/talking-to-the-outside-world/interrupts-exceptions-and-signals.md) (x86-64 assembly), [Length of a string](https://asm-editor.specy.app/learn/courses/x86/examples/string-length.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), [Print an unsigned number in bases 2 through 36](https://asm-editor.specy.app/learn/courses/x86/examples/number-to-string.md) (x86-64 assembly), [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/x86/examples/drawing-on-the-screen.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

- `test r/m, reg` (8, 16, 32, 64)
  - `test r/m8, reg8`
  - `test r/m16, reg16`
  - `test r/m32, reg32`: 386 and later
  - `test r/m64, reg64`
- `test r/m, imm` (8, 16, 32, 64)
  - `test r/m8, imm8`
  - `test r/m16, imm16`
  - `test r/m32, imm32`: 386 and later
  - `test r/m64, imm32`
- `test acc, imm` (8, 16, 32, 64)
  - `test al, imm8`
  - `test ax, imm16`
  - `test eax, imm32`: 386 and later
  - `test 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

ZF reports whether all tested bits were zero; CF and OF are cleared.

For a 64-bit operand, an immediate uses a 32-bit field sign-extended to 64 bits. For example, `test rax, -1` tests all 64 bits. There is no full 64-bit immediate or sign-extended 8-bit immediate encoding for `test`; load a different 64-bit mask into a register if needed.

## Example

```x86
global _start

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

        test rax, rbx

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