TEST

By Vesper Vei
2 minutes read

Table of Contents

  1. TEST (test)
    1. Basic function
    2. Instruction execution process
    3. Instruction format
    4. Behavioral characteristics
    5. Common uses
    6. Small example: test whether eax is even

TEST (test)

Basic function

The TEST instruction performs a bitwise AND on two operands, but does not store the result; it only updates the flags.
It is commonly used for conditional checks, such as testing whether a register is 0 or whether a certain bit is set. Logical behavior:

temp = op1 & op2 ; 结果丢弃
更新 EFLAGS ; 根据 temp 更新标志位

Instruction execution process

Instruction format

test r/m32, r32
test r/m64, r64
test r/m32, imm32
test r/m8, imm8

Behavioral characteristics

Difference from AND:

Equivalent behavior example (logically equivalent):

and temp, op1, op2 ; 假设 temp 是一个不存在的寄存器
根据 temp 更新 EFLAGS
; temp 被丢弃

Common uses

test eax, eax ; 等价于检查 eax 是否为 0
jz is_zero

Small example: test whether eax is even

test eax, 1
jz even

Principle: lowest bit is 0 → even number.



Relationship Graph

Loading graph...