Course contentsShow
← Computer Science
Lesson 1098 of 2,87225. Computer Architecture FoundationsFree lesson

Comparison and Test Instructions

Using CMP and TEST instructions to set CPU flags for conditional execution.

Comparison and Test Instructions

You'll learn: How CPUs use special instructions to compare values and prepare for decision-making without changing the data itself.

What Are Comparison Instructions?

When a CPU needs to make decisions (like "if x equals y, jump to another part of code"), it first needs to examine values without modifying them. That's where CMP (compare) and TEST instructions come in.

These instructions perform operations similar to arithmetic or logical instructions you've already learned, but with one crucial difference: they don't store results in registers. Instead, they update CPU flags—tiny status indicators that other instructions can read to make decisions.

The CMP Instruction

CMP subtracts two values (just like SUB) but throws away the result, keeping only the flags:

CMP register1, register2

If the values are equal, the Zero Flag (ZF) is set to 1. If the first is smaller, the Carry Flag (CF) might be set. Think of it like checking "Are these boxes the same weight?" by lifting them—you learn the answer without actually moving anything.

The TEST Instruction

TEST performs a bitwise AND (just like the AND instruction) but discards the result:

TEST register, value

This is commonly used to check if specific bits are set or if a register is zero. Testing a register against itself (TEST AX, AX) is a fast way to check if it's zero.

Why This Matters

These flags feed into conditional jump instructions (coming soon in your learning), enabling the "if/then/else" logic that makes programs intelligent.

Key Takeaway: CMP and TEST instructions set CPU flags by performing operations without saving results, creating the foundation for conditional program flow.