Instruction
CMPS
Compares the element at [RSI] with the one at [RDI], setting the flags as CMP would, and steps both pointers; REPE and REPNE repeat it.
- Extensions
- Mnemonics
- CMPS, CMPSB, CMPSW, CMPSD, CMPSQ
- Forms
- 12 (12 Legacy)
- Data
- Intel XED v2026.08.23
CMPS compares the element at [RSI] with the one at [RDI], setting the
flags as a CMP of the first with the second would, and
moves both pointers to the next element. With a REPE prefix it walks
two buffers until they differ, the core of memcmp; with REPNE it
walks them until a pair is equal. The doubleword form shares its name
with SSE2’s CMPSD, a comparison of doubles.
REPE CMPS as memcmp
With the buffers in RSI and RDI, the length in RCX and the direction
flag clear, repe cmpsb stops after the first pair of bytes that
differ, or when RCX reaches 0. Afterwards:
- ZF set means every byte compared was equal.
- ZF clear means the bytes at RSI - 1 and RDI - 1 differ, and CF gives
their order as unsigned numbers, as
memcmpneeds: JB when the byte of the first buffer is the smaller. - RCX holds the number of bytes left uncompared.
The trap is a length of 0. The instruction then compares nothing and
changes no flag, so ZF still holds whatever an earlier instruction left
there; the code must test RCX first, or set ZF itself, for instance
with cmp rcx, rcx.
Speed
Intel’s optimization manual advises against REP CMPS and REP SCAS for performance and points to SIMD code instead (Section 3.7.5). Its processors from Golden Cove on report faster REPE and REPNE CMPSB and SCASB, for 1 to 128 bytes held in the first-level cache, in CPUID.(EAX=7,ECX=1):EAX[12], and the manual gives them a flat 15 cycles in that range (Section 3.8.3). The instruction table of AMD’s Zen 5 guide lists CMPSB as microcoded.
GCC 14 and Clang 23 at -O2 call memcmp for comparisons of unknown
length, and inline those of a small known size without CMPS:
memcmp(a, b, 16) == 0 becomes two quadword XORs and an OR in GCC,
and PCMPEQB with PMOVMSKB in
Clang.
A fault in the middle of a REPE or REPNE CMPS, such as a page fault on a later element, leaves RSI, RDI and RCX at the element that faulted, and the flags as they were before the instruction, so it resumes cleanly; see MOVS for the direction flag and the 67 prefix.
In the optimization guide
What the optimization guide says about CMPS, a line per claim. Each links to the claim in the guide, with its sources and where they differ.
Memory
- REP MOVS and STOS: Intel’s cores made
rep movsbandrep stosbfast from Ivy Bridge, and short strings fast from Ice Lake and Golden Cove, as CPUID reports.
Golden Cove and Raptor Cove
- Loads, stores and caches: Short
rep movsb,rep stosb,rep cmpsbandrep scasbare fast: with the data in the L1 cache, any length up to 128 bytes takes a flat 9, 12, 15 and 15 cycles, zero included except for the compares.
Forms
| Instruction | Encoding | Requires |
|---|---|---|
CMPSB
CMPSB | A6 | Base instruction set |
CMPSW
CMPSW | 66 A7 | Base instruction set |
CMPSD
CMPSD | A7 | Base instruction set |
CMPSQ 64-bit mode only
CMPSQ | REX.W A7 | LM |
REPE CMPSB
REPE_CMPSB | F3 A6 | Base instruction set |
REPNE CMPSB
REPNE_CMPSB | F2 A6 | Base instruction set |
REPE CMPSW
REPE_CMPSW | 66 F3 A7 | Base instruction set |
REPNE CMPSW
REPNE_CMPSW | 66 F2 A7 | Base instruction set |
REPE CMPSD
REPE_CMPSD | F3 A7 | Base instruction set |
REPNE CMPSD
REPNE_CMPSD | F2 A7 | Base instruction set |
REPE CMPSQ 64-bit mode only
REPE_CMPSQ | F3 REX.W A7 | LM |
REPNE CMPSQ 64-bit mode only
REPNE_CMPSQ | F2 REX.W A7 | LM |
Latency and throughput
| Model | CMPSBCMPSWCMPSDCMPSQCMPSBCMPSBCMPSWCMPSWCMPSDCMPSDCMPSQCMPSQ |
|---|---|
| Sandy Bridge | 8 / 1p0 p1 p5 p23 |
| Haswell | 4 / 1p012356 |
| Broadwell, Skylake, Skylake-SP, Ice Lake | 100 / 0.25p0156 |
| Alder Lake, Sapphire Rapids | 6 / 1p0 p1 p2 p3 p5 p6 p10 p11 |
| Lunar Lake | 6 / 0.83p0 p1 p2 p3 p4 p5 p20 p21 p22 |
| Bonnell | 3 / 1.5p01 |
| Silvermont | 100 / 1FPC0 |
| Piledriver | 100 / 0.5EX0 EX1 |
| Zen, Zen 2 | 100 / 0.25 |
| Zen 3, Zen 4 | 100 / 25ALU0 ALU1 ALU2 ALU3 |
| Jaguar | 100 / 0.5ALU0 ALU1 |
Operation
What the forms do, in pseudocode that our tests run against results recorded on real processors. How to read.
step = -(OSIZE // 8) if df else OSIZE // 8
a = load(aSI, OSIZE)
b = load(aDI, OSIZE)
flags = sub_flags(a, b) # [rSI] - [rDI]
aSI = low(aSI + step, ASIZE)
aDI = low(aDI + step, ASIZE)Run on AMD Zen 5 (EPYC 9645): 16 cases.
REPE CMPSB REPE CMPSW REPE CMPSD REPE CMPSQ 64-bit mode onlystep = -(OSIZE // 8) if df else OSIZE // 8
s = aSI
d = aDI
c = aCX
for i in range(aCX): # no flag changes when aCX is 0
a = load(s, OSIZE)
b = load(d, OSIZE)
flags = sub_flags(a, b)
s = low(s + step, ASIZE)
d = low(d + step, ASIZE)
c -= 1
if a != b:
break # ZF clear: the first difference
aSI = s
aDI = d
aCX = cRun on AMD Zen 5 (EPYC 9645): 24 cases.
REPNE CMPSB REPNE CMPSW REPNE CMPSD REPNE CMPSQ 64-bit mode onlystep = -(OSIZE // 8) if df else OSIZE // 8
s = aSI
d = aDI
c = aCX
for i in range(aCX):
a = load(s, OSIZE)
b = load(d, OSIZE)
flags = sub_flags(a, b)
s = low(s + step, ASIZE)
d = low(d + step, ASIZE)
c -= 1
if a == b:
break # ZF set: the first match
aSI = s
aDI = d
aCX = cRun on AMD Zen 5 (EPYC 9645): 24 cases.
Operands
| Operand | Kind | Action |
|---|---|---|
CMPSB CMPSW CMPSD CMPSQ 64-bit mode only | ||
m8/16/32/64 at [rSI] | implicit | read |
rSI | implicit | read, write |
m8/16/32/64 at ES:[rDI] | implicit | read |
rDI | implicit | read, write |
RFLAGS | implicit | read, write |
REPE CMPSB REPNE CMPSB REPE CMPSW REPNE CMPSW REPE CMPSD REPNE CMPSD REPE CMPSQ 64-bit mode only REPNE CMPSQ 64-bit mode only | ||
m8/16/32/64 at [rSI] | implicit | conditional read |
rSI | implicit | read, conditional write |
m8/16/32/64 at ES:[rDI] | implicit | conditional read |
rDI | implicit | read, conditional write |
rCX | implicit | read, conditional write |
RFLAGS | implicit | read, conditional write |
Flags
| When | OF | SF | ZF | AF | PF | CF | DF |
|---|---|---|---|---|---|---|---|
| REP forms | |||||||
| in some cases | M | M | TM | M | M | M | T |
| Other forms | |||||||
| M | M | M | M | M | M | T | |
Attributes
What else XED records about the forms. How to read.
- REP forms
- repeated: count in rCX
CPUID and processors
Each form belongs to an XED ISA set. A form can be used when the processor reports every CPUID bit of one of its ISA set's alternatives.
| ISA set | CPUID | Processors |
|---|---|---|
I86 | No CPUID bit: part of the base instruction set. | Every x86-64 processor in XED’s model and Zen 3 to Zen 6 |
I386 | No CPUID bit: part of the base instruction set. | Every x86-64 processor in XED’s model and Zen 3 to Zen 6 |
LONGMODE | LM | Every x86-64 processor in XED’s model and Zen 3 to Zen 6 |
More General-purpose instructions
Sources
- Intel XED v2026.08.23 (commit
0bcb6237345c): forms, encodings, operands, flags, exception classes, attributes, ISA sets, CPUID bits and chip model. - The CPU data of x86-64.net: Zen 3 to Zen 6, from GCC’s
-marchfeature sets and AMD’s Processor Programming References, which the page of each processor cites. - LLVM 23.1.2's x86 scheduling models (commit
85ac56026243): the latency, reciprocal throughput and ports of each form, as llvm-mca reports them. - Intel 64 and IA-32 Architectures Software Developer's Manual, Volume 2 (325383-093, September 2026): Volume 2A, page 3-181 (CMPS/
CMPSB/ CMPSW/ CMPSD/ CMPSQ) - AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions (24594, rev. 3.39, October 2026)
- Intel 64 and IA-32 Architectures Optimization Reference Manual, Volume 1 (248966-051, October 2026)
- Software Optimization Guide for the AMD Zen5 Microarchitecture (58455, rev. 1.00, August 2024), Wayback Machine copy of AMD's ZIP
The tables are derived from Intel XED, Copyright Intel Corporation, licensed under the Apache License 2.0; x86-64.net converted and reformatted them. The timings come from LLVM's scheduling models, part of the LLVM Project, licensed under the Apache License 2.0 with LLVM Exceptions. The text is our own.