Skip to content

REPE_REPZ

Henk-Jan Lebbink edited this page Jun 4, 2026 · 1 revision

REPE / REPZ — Repeat String Operation While Zero (Prefix)

Opcode Instruction Op/ En 64-Bit Mode Compat/ Leg Mode Description1
F3 A6 REPE CMPS m8, m8 ZO Valid Valid Find nonmatching bytes in (src1) and (src2).
F3 A7 REPE CMPS m16, m16 ZO Valid Valid Find nonmatching words in (src1) and (src2).
F3 A7 REPE CMPS m32, m32 ZO Valid Valid Find nonmatching doublewords in (src1) and (src2).
F3 REX.W A7 REPE CMPS m64, m64 ZO Valid N.E. Find nonmatching quadwords in (src1) and (src2).
F3 AE REPE SCAS m8 ZO Valid Valid Find non-AL byte starting at (src2).
F3 AF REPE SCAS m16 ZO Valid Valid Find non-AX word starting at (src2).
F3 AF REPE SCAS m32 ZO Valid Valid Find non-EAX doubleword starting at (src2).
F3 REX.W AF REPE SCAS m64 ZO Valid N.E. Find non-RAX quadword starting at (src2).

NOTES:

  1. See Description section for details on (src1) and (src2).

Instruction Operand Encoding

Op/En Operand 1 Operand 2 Operand 3 Operand 4
ZO N/A N/A N/A N/A

Description

Repeats a string instruction the number of times specified in the count register or until the ZF flag is clear. The count register is CX, ECX, or RCX, depending on the instruction’s address size. The REPE (repeat while equal) and REPZ (repeat while zero) mnemonics are prefixes that can be added to the CMPS and SCAS instructions. (The REPZ prefix is a synonymous form of the REPE prefix.)

The REPE/REPZ prefixes apply only to one string instruction at a time. To repeat a block of instructions, use the LOOP instruction or another looping construct. These repeat prefixes cause the associated instruction to be repeated until the count in register is decremented to 0.

The REPE/REPZ prefixes check the state of the ZF flag after each iteration and terminate the repeat loop if the ZF flag is not set. When both termination conditions are tested, the cause of a repeat termination can be determined either by testing the count register with a JECXZ instruction or by testing the ZF flag (with a JZ, JNZ, or JNE instruction).

The ZF flag does not require initialization because both the CMPS and SCAS instructions affect the ZF flag according to the results of the comparisons they make.

Each of the string instructions uses one or two source addresses. The first source address is DS:SI, DS:ESI, or DS:RSI, depending on the instruction’s address size; the DS segment may be overridden by an instruction prefix. The second source address is ES:DI, ES:EDI, or ES:RDI, depending on the instruction's address size; the ES segment may not be overridden. (Note that, in 64-bit mode, the base addresses of the CS, DS, ES, and SS segments are treated as zero.)

Similarly, the size of the count register is the instruction’s address size. Thus, the default count register in 64-bit mode is RCX; REX.W has no effect on the address size and the count register. If 67H is used to override the default address size, the size of the count register is also overridden.

A repeating string operation can be suspended by an exception or interrupt. When this happens, the state of the registers is preserved to allow the string operation to be resumed upon a return from the exception or interrupt handler. The source and destination registers point to the next string elements to be operated on, the EIP register points to the string instruction, and the ECX register has the value it held following the last successful iteration of the instruction. This mechanism allows long string operations to proceed without affecting the interrupt response time of the system. When a fault occurs during the execution of a CMPS or SCAS instruction that is prefixed with REPE or REPZ, the EFLAGS value is restored to the state prior to the execution of the instruction. Since the SCAS and CMPS instructions do not use EFLAGS as an input, the processor can resume the instruction after the page fault handler.

Operation

IF AddressSize = 16
THEN
Use CX for CountReg;
Implicit Source/Dest operand for memory use of SI/DI;
ELSE IF AddressSize = 64
THEN Use RCX for CountReg;
Implicit Source/Dest operand for memory use of RSI/RDI;
ELSE
Use ECX for CountReg;
Implicit Source/Dest operand for memory use of ESI/EDI;
FI;
WHILE CountReg0
    DO
        Service pending interrupts (if any);
        Execute associated string instruction;
        CountReg ← (CountReg1);
        IF ZF = 0
            THEN exit WHILE loop;
        FI;
    OD;

Flags Affected

None by the prefixes; however, the CMPS and SCAS instructions do set the status flags in the EFLAGS register.

Exceptions (All Operating Modes)

Exceptions may be generated by an instruction associated with the prefix.


Source: Intel® 64 and IA-32 Architectures Software Developer's Manual, Combined Volumes (Order Number 325462-091US, March 2026)
Generated: 7-6-2026

Clone this wiki locally