REP              Repeat                              Flags: not altered

REP  string-instruction

Logic:    while CX <> 0                      ;for MOVS, LODS or STOS
execute string instruction
CX   CX - 1
while CX <> 0
execute string instruction     ;for CMPS or SCAS
CX   CX - 1
if ZF = 0 terminate loop

REP is a prefix that may be specified before any string instruction
(CMPS, LODS, MOVS, SCAS, and STOS). REP causes the string instruction
following it to be repeated, as long as CX does not equal 0; CX is
decremented after each execution of the string instruction.  (For CMPS
and SCAS, REP will also terminate the loop if the Zero Flag is clear
after the string instruction executes.)

Operands                  Clocks   Transfers  Bytes   Example
-                        2          -        1     REP MOVS TO,FROM

Notes:         If CX is initially 0, the REPeated instruction is
skipped entirely.

The test for CX equal to 0 is performed before the
instruction is executed.  The test for the Zero Flag
clear  done only for CMPS and SCAS  is performed
after the instruction is executed.

REP, REPE (Repeat While Equal), and REPZ (Repeat
While Zero) are all synonyms for the same

REPNZ (Repeat Not Zero) is similar to REP, but when
used with CMPS and SCAS, will terminate with the
Zero Flag set, rather than cleared.

REP is generally used with the MOVS (Move String)
and STOS (Store String) instructions; it can be
thought of as "repeat while not end of string."

You do not need to initialize ZF before using
repeated string instructions.

A REPeated instruction that is interrupted between
repeats will correctly resume processing upon return
from the interrupt. However, if other prefixes are
used on a single instruction (for example, segment
override or LOCK) in addition to the REP, all
prefixes except the one that immediately preceded
the string instruction will be lost. Therefore, if
you must use more than one prefix on a single
instruction, you should disable interrupts before
the instruction, and enable them afterwards. Note
that even this precaution will not prevent a non-
maskable interrupt, and that lengthy string
operations may cause large delays in interrupt


The following example moves 100 bytes from BUFFER1 to BUFFER2:

CLD                     ;Move in the forward direction
LEA     SI, BUFFER1     ;Source pointer to SI
LEA     DI, BUFFER2     ;  ...and destination to DI
MOV     CX,100          ;REP uses CX as the counter
REP     MOVSB           ;  ...and do it

See also: REPNE
See also: MOVS
See also: STOS
See also: CMPS
See also: SCAS
See also: LODS
See also: CLD
See also: STD

REP Repeat