1 <!DOCTYPE HTML PUBLIC
"-//IETF//DTD HTML 2.0//EN">
4 <TITLE>80386 Programmer's Reference Manual -- Opcode REP
</TITLE>
7 <B>up:
</B> <A HREF=
"c17.htm">
8 Chapter
17 --
80386 Instruction Set
</A><BR>
9 <B>prev:
</B><A HREF=
"RCL.htm"> RCL/RCR/ROL/ROR Rotate
</A><BR>
10 <B>next:
</B><A HREF=
"RET.htm"> RET Return from Procedure
</A>
14 <H1>REP/REPE/REPZ/REPNE/REPNZ -- Repeat Following String Operation
</H1>
18 Opcode Instruction Clocks Description
20 F3
6C REP INS r/m8, DX
13+
6*(E)CX,
24 If CPL
> IOPL or if in virtual
8086 mode Input (E)CX bytes from port
26 F3
6D REP INS r/m16,DX
13+
6*(E)CX,
30 If CPL
> IOPL or if in virtual
8086 mode Input (E)CX words from port
32 F3
6D REP INS r/m32,DX
13+
6*(E)CX,
36 If CPL
> IOPL or if in virtual
8086 mode Input (E)CX dwords from port
38 F3 A4 REP MOVS m8,m8
5+
4*(E)CX Move (E)CX bytes from
40 F3 A5 REP MOVS m16,m16
5+
4*(E)CX Move (E)CX words from
42 F3 A5 REP MOVS m32,m32
5+
4*(E)CX Move (E)CX dwords from
44 F3
6E REP OUTS DX,r/m8
5+
12*(E)CX,
48 If CPL
> IOPL or if in virtual
8086 mode Output (E)CX bytes from
50 F3
6F REP OUTS DX,r/m16
5+
12*(E)CX,
54 If CPL
> IOPL or if in virtual
8086 mode Output (E)CX words from
56 F3
6F REP OUTS DX,r/m32
5+
12*(E)CX,
60 If CPL
> IOPL or if in virtual
8086 mode Output (E)CX dwords from
62 F3 AA REP STOS m8
5+
5*(E)CX Fill (E)CX bytes at
64 F3 AB REP STOS m16
5+
5*(E)CX Fill (E)CX words at
66 F3 AB REP STOS m32
5+
5*(E)CX Fill (E)CX dwords at
68 F3 A6 REPE CMPS m8,m8
5+
9*N Find nonmatching bytes in
69 ES:[(E)DI] and [(E)SI]
70 F3 A7 REPE CMPS m16,m16
5+
9*N Find nonmatching words in
71 ES:[(E)DI] and [(E)SI]
72 F3 A7 REPE CMPS m32,m32
5+
9*N Find nonmatching dwords in
73 ES:[(E)DI] and [(E)SI]
74 F3 AE REPE SCAS m8
5+
8*N Find non-AL byte starting
76 F3 AF REPE SCAS m16
5+
8*N Find non-AX word starting
78 F3 AF REPE SCAS m32
5+
8*N Find non-EAX dword starting
80 F2 A6 REPNE CMPS m8,m8
5+
9*N Find matching bytes in
81 ES:[(E)DI] and [(E)SI]
82 F2 A7 REPNE CMPS m16,m16
5+
9*N Find matching words in
83 ES:[(E)DI] and [(E)SI]
84 F2 A7 REPNE CMPS m32,m32
5+
9*N Find matching dwords in
85 ES:[(E)DI] and [(E)SI]
86 F2 AE REPNE SCAS m8
5+
8*N Find AL, starting at
88 F2 AF REPNE SCAS m16
5+
8*N Find AX, starting at
90 F2 AF REPNE SCAS m32
5+
8*N Find EAX, starting at
98 THEN use CX for CountReg;
99 ELSE (* AddressSize =
32 *) use ECX for CountReg;
103 service pending interrupts (if any);
104 perform primitive string instruction;
105 CountReg := CountReg -
1;
106 IF primitive operation is CMPB, CMPW, SCAB, or SCAW
108 IF (instruction is REP/REPE/REPZ) AND (ZF=
1)
111 IF (instruction is REPNZ or REPNE) AND (ZF=
0)
112 THEN exit WHILE loop;
121 REP, REPE (repeat while equal), and REPNE (repeat while not equal)
122 are prefix that are applied to string operation. Each prefix cause the
123 string instruction that follows to be repeated the number of times
124 indicated in the count register or (for REPE and REPNE) until the
125 indicated condition in the zero flag is no longer met.
127 Synonymous forms of REPE and REPNE are REPZ and REPNZ,
130 The REP prefixes apply only to one string instruction at a time. To repeat
131 a block of instructions, use the
132 <A HREF=
"LOOP.htm">LOOP
</A> instruction or another looping
135 The precise action for each iteration is as follows:
138 <LI> If the address-size attribute is
16 bits, use CX for the count
139 register; if the address-size attribute is
32 bits, use ECX for the
142 <LI> Check CX. If it is zero, exit the iteration, and move to the next
145 <LI> Acknowledge any pending interrupts.
147 <LI> Perform the string operation once.
149 <LI> Decrement CX or ECX by one; no flags are modified.
151 <LI> Check the zero flag if the string operation is
152 <A HREF=
"SCAS.htm">SCAS
</A> or
<A HREF=
"CMPS.htm">CMPS
</A>. If
153 the repeat condition does not hold, exit the iteration and move to
154 the next instruction. Exit the iteration if the prefix is REPE and ZF
155 is
0 (the last comparison was not equal), or if the prefix is REPNE
156 and ZF is one (the last comparison was equal).
158 <LI> Return to step
1 for the next iteration.
162 <A HREF=
"CMPS.htm">CMPS
</A> and
<A HREF=
"SCAS.htm">SCAS
</A>
163 instructions can be exited if the count is
164 exhausted or if the zero flag fails the repeat condition. These two cases
165 can be distinguished by using either the
166 <A HREF=
"Jcc.htm">JCXZ
</A> instruction, or by using
167 the conditional jumps that test the zero flag (
<A HREF=
"Jcc.htm">JZ
</A>,
168 <A HREF=
"Jcc.htm">JNZ
</A>, and
169 <A HREF=
"Jcc.htm">JNE
</A>).
171 <H2>Flags Affected
</H2>
173 ZF by REP
<A HREF=
"CMPS.htm">CMPS
</A> and REP
174 <A HREF=
"SCAS.htm">SCAS
</A> as described above
176 <H2>Protected Mode Exceptions
</H2>
178 #UD if a repeat prefix is used before an instruction that is not in the
179 list above; further exceptions can be generated when the string operation is
180 executed; refer to the descriptions of the string instructions themselves
182 <H2>Real Address Mode Exceptions
</H2>
184 Interrupt
6 if a repeat prefix is used before an instruction that is not in
185 the list above; further exceptions can be generated when the string
186 operation is executed; refer to the descriptions of the string instructions
189 <H2>Virtual
8086 Mode Exceptions
</H2>
191 #UD if a repeat prefix is used before an instruction that is not in the
192 list above; further exceptions can be generated when the string operation is
193 executed; refer to the descriptions of the string instructions themselves
197 Not all input/output ports can handle the rate at which the REP
198 <A HREF=
"INS.htm">INS
</A>
199 and REP
<A HREF=
"OUTS.htm">OUTS
</A> instructions execute.
203 <B>up:
</B> <A HREF=
"c17.htm">
204 Chapter
17 --
80386 Instruction Set
</A><BR>
205 <B>prev:
</B><A HREF=
"RCL.htm"> RCL/RCR/ROL/ROR Rotate
</A><BR>
206 <B>next:
</B><A HREF=
"RET.htm"> RET Return from Procedure
</A>