-
Notifications
You must be signed in to change notification settings - Fork 99
REPNE_REPNZ
REPNE / REPNZ — Repeat String Operation While Not Zero (Prefix)
| Opcode | Instruction | Op/ En | 64-Bit Mode | Compat/ Leg Mode | Description1 |
| F2 A6 | REPNE CMPS m8, m8 | ZO | Valid | Valid | Find matching bytes in (src1) and (src2). |
| F2 A7 | REPNE CMPS m16, m16 | ZO | Valid | Valid | Find matching words in (src1) and (src2). |
| F2 A7 | REPNE CMPS m32, m32 | ZO | Valid | Valid | Find matching doublewords in (src1) and (src2). |
| F2 REX.W A7 | REPNE CMPS m64, m64 | ZO | Valid | N.E. | Find matching quadwords in (src1) and (src2). |
| F2 AE | REPNE SCAS m8 | ZO | Valid | Valid | Find AL, starting at (src2). |
| F2 AF | REPNE SCAS m16 | ZO | Valid | Valid | Find AX, starting at (src2). |
| F2 AF | REPNE SCAS m32 | ZO | Valid | Valid | Find EAX, starting at (src2). |
| F2 REX.W AF | REPNE SCAS m64 | ZO | Valid | N.E. | Find RAX, starting at (src2). |
NOTES:
- See Description section for details on (src1) and (src2).
| Op/En | Operand 1 | Operand 2 | Operand 3 | Operand 4 |
| ZO | N/A | N/A | N/A | N/A |
Repeats a string instruction the number of times specified in the count register or until the ZF flag is set. The count register is CX, ECX, or RCX, depending on the instruction’s address size. The REPNE (repeat while not equal) and REPNZ (repeat while not zero) mnemonics are prefixes that can be added to the CMPS and SCAS instructions. (The REPNZ prefix is a synonymous form of the REPNE prefix.)
The REPNE/REPNZ 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 REPNE/REPNZ prefixes also check the state of the ZF flag after each iteration and terminate the repeat loop if the ZF flag is 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 it is checked only after each execution of CMPS and SCAS, and those instructions update 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 REPNE or REPNZ, 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.
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 CountReg ≠ 0
DO
Service pending interrupts (if any);
Execute associated string instruction;
CountReg ← (CountReg – 1);
IF CountReg = 0
THEN exit WHILE loop; FI;
IF ZF = 1
THEN exit WHILE loop; FI;
OD;None; however, the CMPS and SCAS instructions do set the status flags in the EFLAGS register.
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