* better
[mascara-docs.git] / hw / i386.reference / REP.htm
blob3041d9f80a3d73b8bf345d176d349367a3f5c4ad
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
2 <HTML>
3 <HEAD>
4 <TITLE>80386 Programmer's Reference Manual -- Opcode REP</TITLE>
5 </HEAD>
6 <BODY>
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>
11 <P>
12 <HR>
13 <P>
14 <H1>REP/REPE/REPZ/REPNE/REPNZ -- Repeat Following String Operation</H1>
17 <PRE>
18 Opcode Instruction Clocks Description
20 F3 6C REP INS r/m8, DX 13+6*(E)CX,
21 pm=7+6*(E)CX
22 If CPL <= IOPL/
23 27+6*(E)CX
24 If CPL > IOPL or if in virtual 8086 mode Input (E)CX bytes from port
25 DX into ES:[(E)DI]
26 F3 6D REP INS r/m16,DX 13+6*(E)CX,
27 pm=7+6*(E)CX
28 If CPL <= IOPL/
29 27+6*(E)CX
30 If CPL > IOPL or if in virtual 8086 mode Input (E)CX words from port
31 DX into ES:[(E)DI]
32 F3 6D REP INS r/m32,DX 13+6*(E)CX,
33 pm=7+6*(E)CX
34 If CPL <= IOPL/
35 27+6*(E)CX
36 If CPL > IOPL or if in virtual 8086 mode Input (E)CX dwords from port
37 DX into ES:[(E)DI]
38 F3 A4 REP MOVS m8,m8 5+4*(E)CX Move (E)CX bytes from
39 [(E)SI] to ES:[(E)DI]
40 F3 A5 REP MOVS m16,m16 5+4*(E)CX Move (E)CX words from
41 [(E)SI] to ES:[(E)DI]
42 F3 A5 REP MOVS m32,m32 5+4*(E)CX Move (E)CX dwords from
43 [(E)SI] to ES:[(E)DI]
44 F3 6E REP OUTS DX,r/m8 5+12*(E)CX,
45 pm=6+5*(E)CX
46 If CPL <= IOPL/
47 26+5*(E)CX
48 If CPL > IOPL or if in virtual 8086 mode Output (E)CX bytes from
49 [(E)SI] to port DX
50 F3 6F REP OUTS DX,r/m16 5+12*(E)CX,
51 pm=6+5*(E)CX
52 If CPL <= IOPL/
53 26+5*(E)CX
54 If CPL > IOPL or if in virtual 8086 mode Output (E)CX words from
55 [(E)SI] to port DX
56 F3 6F REP OUTS DX,r/m32 5+12*(E)CX,
57 pm=6+5*(E)CX
58 If CPL <= IOPL/
59 26+5*(E)CX
60 If CPL > IOPL or if in virtual 8086 mode Output (E)CX dwords from
61 [(E)SI] to port DX
62 F3 AA REP STOS m8 5+5*(E)CX Fill (E)CX bytes at
63 ES:[(E)DI] with AL
64 F3 AB REP STOS m16 5+5*(E)CX Fill (E)CX words at
65 ES:[(E)DI] with AX
66 F3 AB REP STOS m32 5+5*(E)CX Fill (E)CX dwords at
67 ES:[(E)DI] with EAX
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
75 at ES:[(E)DI]
76 F3 AF REPE SCAS m16 5+8*N Find non-AX word starting
77 at ES:[(E)DI]
78 F3 AF REPE SCAS m32 5+8*N Find non-EAX dword starting
79 at ES:[(E)DI]
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
87 ES:[(E)DI]
88 F2 AF REPNE SCAS m16 5+8*N Find AX, starting at
89 ES:[(E)DI]
90 F2 AF REPNE SCAS m32 5+8*N Find EAX, starting at
91 ES:[(E)DI]
92 </PRE>
94 <H2>Operation</H2>
96 <PRE>
97 IF AddressSize = 16
98 THEN use CX for CountReg;
99 ELSE (* AddressSize = 32 *) use ECX for CountReg;
101 WHILE CountReg <> 0
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
107 THEN
108 IF (instruction is REP/REPE/REPZ) AND (ZF=1)
109 THEN exit WHILE loop
110 ELSE
111 IF (instruction is REPNZ or REPNE) AND (ZF=0)
112 THEN exit WHILE loop;
117 </PRE>
119 <H2>Description</H2>
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,
128 respectively.
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
133 construct.
135 The precise action for each iteration is as follows:
137 <OL>
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
140 count register.
142 <LI> Check CX. If it is zero, exit the iteration, and move to the next
143 instruction.
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.
159 </OL>
161 Repeated
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
187 themselves
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
195 <H2>Notes</H2>
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.
201 <HR>
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>
207 </BODY>