* remove "\r" nonsense
[mascara-docs.git] / i386 / i386.reference / s03_06.htm
blob37ebb6afdb09fd46e8b8575c3538aa95a2a6a4ad
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
2 <HTML>
3 <HEAD>
4 <TITLE>80386 Programmer's Reference Manual -- Section 3.6</TITLE>
5 </HEAD>
6 <BODY>
7 <B>up:</B> <A HREF="c03.htm">
8 Chapter 3 -- Applications Instruction Set</A><BR>
9 <B>prev:</B> <A HREF="s03_05.htm">3.5 Control Transfer Instructions</A><BR>
10 <B>next:</B> <A HREF="s03_07.htm">3.7 Instructions for Block-Structured Languages</A>
11 <P>
12 <HR>
13 <P>
14 <H1>3.6 String and Character Translation Instructions</H1>
15 The instructions in this category operate on strings rather than on logical
16 or numeric values. Refer also to the section on I/O for information about
17 the string I/O instructions (also known as block I/O).
18 <P>
19 The power of 80386 string operations derives from the following features of
20 the architecture:
21 <OL>
22 <LI> A set of primitive string operations
23 <UL>
24 <LI><A HREF="MOVS.htm">MOVS</A> -- Move String
25 <LI><A HREF="CMPS.htm">CMPS</A> -- Compare string
26 <LI><A HREF="SCAS.htm">SCAS</A> -- Scan string
27 <LI><A HREF="LODS.htm">LODS</A> -- Load string
28 <LI><A HREF="STOS.htm">STOS</A> -- Store string
29 </UL>
30 <P>
31 <LI> Indirect, indexed addressing, with automatic incrementing or
32 decrementing of the indexes.
33 <DL>
34 <DT>
35 Indexes:
36 <DD>
37 ESI -- Source index register<BR>
38 EDI -- Destination index register
39 <DT>
40 Control flag:
41 <DD>
42 DF -- Direction flag
43 <DT>
44 Control flag instructions:
45 <DD>
46 <A HREF="CLD.htm">CLD</A> -- Clear direction flag instruction<BR>
47 <A HREF="STD.htm">STD</A> -- Set direction flag instruction
48 </DL>
49 <P>
50 <LI> Repeat prefixes
51 <UL>
52 <LI><A HREF="REP.htm">REP</A> -- Repeat while ECX not xero
53 <LI><A HREF="REP.htm">REPE/REPZ</A> -- Repeat while equal or zero
54 <LI><A HREF="REP.htm">REPNE/REPNZ</A> -- Repeat while not equal or not zero
55 </UL>
56 </OL>
57 <P>
58 The primitive string operations operate on one element of a string. A
59 string element may be a byte, a word, or a doubleword. The string elements
60 are addressed by the registers ESI and EDI. After every primitive operation
61 ESI and/or EDI are automatically updated to point to the next element of the
62 string. If the direction flag is zero, the index registers are incremented;
63 if one, they are decremented. The amount of the increment or decrement is
64 1, 2, or 4 depending on the size of the string element.
66 <H2>3.6.1 Repeat Prefixes</H2>
67 The repeat prefixes
68 <A HREF="REP.htm">REP</A> (Repeat While ECX Not Zero),
69 <A HREF="REP.htm">REPE/REPZ</A> (Repeat
70 While Equal/Zero), and
71 <A HREF="REP.htm">REPNE/REPNZ</A> (Repeat While Not Equal/Not Zero) specify
72 repeated operation of a string primitive. This form of iteration allows the
73 CPU to process strings much faster than would be possible with a regular
74 software loop.
75 <P>
76 When a primitive string operation has a repeat prefix, the operation is
77 executed repeatedly, each time using a different element of the string. The
78 repetition terminates when one of the conditions specified by the prefix is
79 satisfied.
80 <P>
81 At each repetition of the primitive instruction, the string operation may
82 be suspended temporarily in order to handle an exception or external
83 interrupt. After the interruption, the string operation can be restarted
84 again where it left off. This method of handling strings allows operations
85 on strings of arbitrary length, without affecting interrupt response.
86 <P>
87 All three prefixes causes the hardware to automatically repeat the
88 associated string primitive until ECX=0. The differences among the repeat
89 prefixes have to do with the second termination condition.
90 <A HREF="REP.htm">REPE/REPZ</A> and
91 <A HREF="REP.htm">REPNE/REPNZ</A> are used exclusively with the
92 <A HREF="SCAS.htm">SCAS</A> (Scan String) and
93 <A HREF="CMPS.htm">CMPS</A>
94 (Compare String) primitives. When these prefixes are used, repetition of the
95 next instruction depends on the zero flag (ZF) as well as the ECX register.
96 ZF does not require initialization before execution of a repeated string
97 instruction, because both
98 <A HREF="SCAS.htm">SCAS</A> and
99 <A HREF="CMPS.htm">CMPS</A> set ZF according to the results of
100 the comparisons they make. The differences are summarized in the
101 accompanying table.
102 <PRE>
103 Prefix Termination Termination
104 Condition 1 Condition 2
105 REP ECX = 0 (none)
106 REPE/REPZ ECX = 0 ZF = 0
107 REPNE/REPNZ ECX = 0 ZF = 1
108 </PRE>
110 <H2>3.6.2 Indexing and Direction Flag Control</H2>
111 The addresses of the operands of string primitives are determined by the
112 ESI and EDI registers. ESI points to source operands. By default, ESI refers
113 to a location in the segment indicated by the DS segment register. A
114 segment-override prefix may be used, however, to cause ESI to refer to CS,
115 SS, ES, FS, or GS. EDI points to destination operands in the segment
116 indicated by ES; no segment override is possible. The use of two different
117 segment registers in one instruction allows movement of strings between
118 different segments.
120 This use of ESI and DSI has led to the descriptive names source index and
121 destination index for the ESI and EDI registers, respectively. In all
122 cases other than string instructions, however, the ESI and EDI registers may
123 be used as general-purpose registers.
125 When ESI and EDI are used in string primitives, they are automatically
126 incremented or decremented after to operation. The direction flag determines
127 whether they are incremented or decremented. The instruction
128 <A HREF="CLD.htm">CLD</A> puts zero
129 in DF, causing the index registers to be incremented; the instruction
130 <A HREF="STD.htm">STD</A>
131 puts one in DF, causing the index registers to be decremented. Programmers
132 should always put a known value in DF before using string instructions in a
133 procedure.
135 <H2>3.6.3 String Instructions</H2>
136 <A HREF="MOVS.htm">MOVS</A> (Move String)
137 moves the string element pointed to by ESI to the
138 location pointed to by EDI.
139 <A HREF="MOVS.htm">MOVSB</A> operates on byte elements,
140 <A HREF="MOVS.htm">MOVSW</A> operates on word elements, and
141 <A HREF="MOVS.htm">MOVSD</A> operates on doublewords. The destination segment
142 register cannot be overridden by a segment override prefix, but the source
143 segment register can be overridden.
145 The <A HREF="MOVS.htm">MOVS</A> instruction, when accompanied by the
146 <A HREF="REP.htm">REP</A> prefix, operates as a
147 memory-to-memory block transfer. To set up for this operation, the program
148 must initialize ECX and the register pairs ESI and EDI. ECX specifies the
149 number of bytes, words, or doublewords in the block.
151 If DF=0, the program must point ESI to the first element of the source
152 string and point EDI to the destination address for the first element. If
153 DF=1, the program must point these two registers to the last element of the
154 source string and to the destination address for the last element,
155 respectively.
157 <A HREF="CMPS.htm">CMPS</A> (Compare Strings)
158 subtracts the destination string element (at ES:EDI)
159 from the source string element (at ESI) and updates the flags AF, SF, PF, CF
160 and OF. If the string elements are equal, ZF=1; otherwise, ZF=0. If DF=0,
161 the processor increments the memory pointers (ESI and EDI) for the two
162 strings.
163 <A HREF="CMPS.htm">CMPSB</A> compares bytes,
164 <A HREF="MOVS.htm">MOVSW</A> compares words, and
165 <A HREF="MOVS.htm">MOVSD</A> compares doublewords.
166 The segment register used for the source address can be changed
167 with a segment override prefix while the destination segment register
168 cannot be overridden.
170 <A HREF="SCAS.htm">SCAS</A> (Scan String)
171 subtracts the destination string element at ES:EDI from
172 EAX, AX, or AL and updates the flags AF, SF, ZF, PF, CF and OF. If the
173 values are equal, ZF=1; otherwise, ZF=0. If DF=0, the processor increments
174 the memory pointer (EDI) for the string.
175 <A HREF="SCAS.htm">SCASB</A> scans bytes;
176 <A HREF="SCAS.htm">SCASW</A> scans words;
177 <A HREF="SCAS.htm">SCASD</A> scans doublewords.
178 The destination segment register (ES) cannot
179 be overridden.
181 When either the
182 <A HREF="REP.htm">REPE</A> or
183 <A HREF="REP.htm">REPNE</A> prefix modifies either the
184 <A HREF="SCAS.htm">SCAS</A> or
185 <A HREF="CMPS.htm">CMPS</A>
186 primitives, the processor compares the value of the current string element
187 with the value in EAX for doubleword elements, in AX for word elements, or
188 in AL for byte elements. Termination of the repeated operation depends on
189 the resulting state of ZF as well as on the value in ECX.
191 <A HREF="LODS.htm">LODS</A> (Load String)
192 places the source string element at ESI into EAX for
193 doubleword strings, into AX for word strings, or into AL for byte strings.
194 <A HREF="LODS.htm">LODS</A> increments or decrements ESI according to DF.
196 <A HREF="STOS.htm">STOS</A> (Store String)
197 places the source string element from EAX, AX, or AL
198 into the string at ES:DSI.
199 <A HREF="STOS.htm">STOS</A> increments or decrements EDI according to
202 <HR>
204 <B>up:</B> <A HREF="c03.htm">
205 Chapter 3 -- Applications Instruction Set</A><BR>
206 <B>prev:</B> <A HREF="s03_05.htm">3.5 Control Transfer Instructions</A><BR>
207 <B>next:</B> <A HREF="s03_07.htm">3.7 Instructions for Block-Structured Languages</A>
208 </BODY>