Other meanings of test
Computer Science
In x86 assembly language, the TEST instruction performs a bitwise AND operation between two operands and updates the flags register based on the result, without storing the result anywhere. It is primarily used to test whether specific bits are set or to compare a value against zero, and it is a fundamental building block for conditional branching in low-level programming.
The TEST instruction computes the bitwise AND of its two operands and discards the result, but sets the status flags according to that result.1 Specifically, the Zero Flag (ZF) is set if the AND result is zero, the Sign Flag (SF) reflects the most significant bit of the result, and the Parity Flag (PF) is set if the low byte of the result has an even number of set bits. The Carry Flag (CF) and Overflow Flag (OF) are always cleared to 0.2 The AF flag is undefined after TEST.
The most frequent use of TEST is to check whether a value is zero or whether a specific bit is set. For example, TEST EAX, EAX followed by JZ (jump if zero) branches if EAX equals zero. To test a particular bit, one uses an immediate mask: TEST AL, 01h tests the least significant bit. This pattern is ubiquitous in device drivers, operating system kernels, and embedded systems for checking status registers and flags.3
TEST is often contrasted with the CMP instruction, which performs a subtraction and updates flags. While CMP is used for equality and ordering comparisons, TEST is used for bitwise and zero checks. A key difference is that TEST does not distinguish between operands: TEST EAX, EBX is symmetric, whereas CMP EAX, EBX is not. This symmetry makes TEST useful for checking if two values have any common bits, a technique used in some hashing and collision-detection algorithms.4
The x86 instruction set provides several encodings for TEST. The opcode 84 /r is used for TEST r/m8, r8; 85 /r for TEST r/m16/32, r16/32; A8 ib for TEST AL, imm8; and A9 id for TEST AX/EAX, imm16/32.5 In 64-bit mode, the operand size can be 64 bits, and the instruction supports REX prefixes. The immediate operand is sign-extended in some forms, but for TEST the immediate is always used as-is in the AND operation.
One lesser-known fact is that TEST is often used as a peephole optimization by compilers to replace a CMP with a register that is known to be zero, saving an instruction. Another is that the instruction has a special form TEST r/m16, imm16 that is not available for 8-bit operands with a sign-extended immediate; instead, the 8-bit form uses a full 8-bit immediate. In the 8086, TEST was originally called 'AND' but was renamed to avoid confusion. Additionally, some assemblers allow TEST with a memory operand and an immediate, but the encoding is limited to specific forms. In the world of exploit development, TEST is sometimes used as a 'landing pad' for ROP chains because it is a harmless instruction that can be used to align the stack or as a NOP-like gadget.6
On modern x86 processors, TEST is a single-uop instruction with a latency of 1 cycle and a throughput of up to 4 per cycle on recent Intel and AMD cores. It is fully pipelined and does not require any memory access unless an operand is in memory. Because it does not write to a destination register, it does not create a dependency chain, making it ideal for use in tight loops and as a 'no-op' that updates flags without stalling the pipeline.7
The TEST instruction is a cornerstone of low-level programming, enabling efficient bit testing and zero checks in everything from bootloaders to modern operating systems.
Served from cache
Help improve the encyclopedia. Reports go straight to the site manager.