83 lines
12 KiB
HTML
83 lines
12 KiB
HTML
<html><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
|
|
<html>
|
|
|
|
<head>
|
|
<title>80386 Programmer's Reference Manual -- Section 3.6</title>
|
|
</head>
|
|
|
|
<body>
|
|
<b>up:</b> <a href="C03.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/C03.HTM">Chapter 3 -- Applications Instruction Set</a><br>
|
|
<b>prev:</b> <a href="S03_05.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/S03_05.HTM">3.5 Control Transfer Instructions</a><br>
|
|
<b>next:</b> <a href="S03_07.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/S03_07.HTM">3.7 Instructions for Block-Structured Languages</a>
|
|
<p>
|
|
<hr>
|
|
<p>
|
|
<h1>3.6 String and Character Translation Instructions</h1>
|
|
The instructions in this category operate on strings rather than on logical or numeric values. Refer also to the section on I/O for information about the string I/O instructions (also known as block I/O).
|
|
<p>The power of 80386 string operations derives from the following features of the architecture:
|
|
<ol>
|
|
<li>A set of primitive string operations
|
|
<ul>
|
|
<li><a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVS</a> -- Move String
|
|
<li><a href="CMPS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CMPS.HTM">CMPS</a> -- Compare string
|
|
<li><a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCAS</a> -- Scan string
|
|
<li><a href="LODS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/LODS.HTM">LODS</a> -- Load string
|
|
<li><a href="STOS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/STOS.HTM">STOS</a> -- Store string
|
|
</ul>
|
|
<p>
|
|
<li>Indirect, indexed addressing, with automatic incrementing or decrementing of the indexes.
|
|
<dl>
|
|
<dt>Indexes:
|
|
<dd>ESI -- Source index register<br>
|
|
EDI -- Destination index register
|
|
<dt>Control flag:
|
|
<dd>DF -- Direction flag
|
|
<dt>Control flag instructions:
|
|
<dd><a href="CLD.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CLD.HTM">CLD</a> -- Clear direction flag instruction<br>
|
|
<a href="STD.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/STD.HTM">STD</a> -- Set direction flag instruction
|
|
</dl>
|
|
<p>
|
|
<li>Repeat prefixes
|
|
<ul>
|
|
<li><a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REP</a> -- Repeat while ECX not xero
|
|
<li><a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPE/REPZ</a> -- Repeat while equal or zero
|
|
<li><a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPNE/REPNZ</a> -- Repeat while not equal or not zero
|
|
</ul>
|
|
</ol>
|
|
<p>The primitive string operations operate on one element of a string. A string element may be a byte, a word, or a doubleword. The string elements are addressed by the registers ESI and EDI. After every primitive operation ESI and/or EDI are automatically updated to point to the next element of the string. If the direction flag is zero, the index registers are incremented; if one, they are decremented. The amount of the increment or decrement is 1, 2, or 4 depending on the size of the string element.
|
|
<h2>3.6.1 Repeat Prefixes</h2>
|
|
The repeat prefixes <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REP</a> (Repeat While ECX Not Zero), <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPE/REPZ</a> (Repeat While Equal/Zero), and <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPNE/REPNZ</a> (Repeat While Not Equal/Not Zero) specify repeated operation of a string primitive. This form of iteration allows the CPU to process strings much faster than would be possible with a regular software loop.
|
|
<p>When a primitive string operation has a repeat prefix, the operation is executed repeatedly, each time using a different element of the string. The repetition terminates when one of the conditions specified by the prefix is satisfied.
|
|
<p>At each repetition of the primitive instruction, the string operation may be suspended temporarily in order to handle an exception or external interrupt. After the interruption, the string operation can be restarted again where it left off. This method of handling strings allows operations on strings of arbitrary length, without affecting interrupt response.
|
|
<p>All three prefixes causes the hardware to automatically repeat the associated string primitive until ECX=0. The differences among the repeat prefixes have to do with the second termination condition. <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPE/REPZ</a> and <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPNE/REPNZ</a> are used exclusively with the <a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCAS</a> (Scan String) and <a href="CMPS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CMPS.HTM">CMPS</a> (Compare String) primitives. When these prefixes are used, repetition of the next instruction depends on the zero flag (ZF) as well as the ECX register. ZF does not require initialization before execution of a repeated string instruction, because both <a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCAS</a> and <a href="CMPS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CMPS.HTM">CMPS</a> set ZF according to the results of the comparisons they make. The differences are summarized in the accompanying table.
|
|
<pre>
|
|
|
|
|
|
|
|
Prefix Termination Termination
|
|
Condition 1 Condition 2
|
|
REP ECX = 0 (none)
|
|
REPE/REPZ ECX = 0 ZF = 0
|
|
REPNE/REPNZ ECX = 0 ZF = 1
|
|
</pre>
|
|
<h2>3.6.2 Indexing and Direction Flag Control</h2>
|
|
The addresses of the operands of string primitives are determined by the ESI and EDI registers. ESI points to source operands. By default, ESI refers to a location in the segment indicated by the DS segment register. A segment-override prefix may be used, however, to cause ESI to refer to CS, SS, ES, FS, or GS. EDI points to destination operands in the segment indicated by ES; no segment override is possible. The use of two different segment registers in one instruction allows movement of strings between different segments.
|
|
<p>This use of ESI and DSI has led to the descriptive names source index and destination index for the ESI and EDI registers, respectively. In all cases other than string instructions, however, the ESI and EDI registers may be used as general-purpose registers.
|
|
<p>When ESI and EDI are used in string primitives, they are automatically incremented or decremented after to operation. The direction flag determines whether they are incremented or decremented. The instruction <a href="CLD.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CLD.HTM">CLD</a> puts zero in DF, causing the index registers to be incremented; the instruction <a href="STD.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/STD.HTM">STD</a> puts one in DF, causing the index registers to be decremented. Programmers should always put a known value in DF before using string instructions in a procedure.
|
|
<h2>3.6.3 String Instructions</h2>
|
|
<a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVS</a> (Move String) moves the string element pointed to by ESI to the location pointed to by EDI. <a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVSB</a> operates on byte elements, <a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVSW</a> operates on word elements, and <a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVSD</a> operates on doublewords. The destination segment register cannot be overridden by a segment override prefix, but the source segment register can be overridden.
|
|
<p>The <a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVS</a> instruction, when accompanied by the <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REP</a> prefix, operates as a memory-to-memory block transfer. To set up for this operation, the program must initialize ECX and the register pairs ESI and EDI. ECX specifies the number of bytes, words, or doublewords in the block.
|
|
<p>If DF=0, the program must point ESI to the first element of the source string and point EDI to the destination address for the first element. If DF=1, the program must point these two registers to the last element of the source string and to the destination address for the last element, respectively.
|
|
<p><a href="CMPS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CMPS.HTM">CMPS</a> (Compare Strings) subtracts the destination string element (at ES:EDI) from the source string element (at ESI) and updates the flags AF, SF, PF, CF and OF. If the string elements are equal, ZF=1; otherwise, ZF=0. If DF=0, the processor increments the memory pointers (ESI and EDI) for the two strings. <a href="CMPS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CMPS.HTM">CMPSB</a> compares bytes, <a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVSW</a> compares words, and <a href="MOVS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/MOVS.HTM">MOVSD</a> compares doublewords. The segment register used for the source address can be changed with a segment override prefix while the destination segment register cannot be overridden.
|
|
<p><a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCAS</a> (Scan String) subtracts the destination string element at ES:EDI from EAX, AX, or AL and updates the flags AF, SF, ZF, PF, CF and OF. If the values are equal, ZF=1; otherwise, ZF=0. If DF=0, the processor increments the memory pointer (EDI) for the string. <a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCASB</a> scans bytes; <a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCASW</a> scans words; <a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCASD</a> scans doublewords. The destination segment register (ES) cannot be overridden.
|
|
<p>When either the <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPE</a> or <a href="REP.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/REP.HTM">REPNE</a> prefix modifies either the <a href="SCAS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/SCAS.HTM">SCAS</a> or <a href="CMPS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/CMPS.HTM">CMPS</a> primitives, the processor compares the value of the current string element with the value in EAX for doubleword elements, in AX for word elements, or in AL for byte elements. Termination of the repeated operation depends on the resulting state of ZF as well as on the value in ECX.
|
|
<p><a href="LODS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/LODS.HTM">LODS</a> (Load String) places the source string element at ESI into EAX for doubleword strings, into AX for word strings, or into AL for byte strings. <a href="LODS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/LODS.HTM">LODS</a> increments or decrements ESI according to DF.
|
|
<p><a href="STOS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/STOS.HTM">STOS</a> (Store String) places the source string element from EAX, AX, or AL into the string at ES:DSI. <a href="STOS.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/STOS.HTM">STOS</a> increments or decrements EDI according to DF.
|
|
<p>
|
|
<hr>
|
|
<p><b>up:</b> <a href="C03.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/C03.HTM">Chapter 3 -- Applications Instruction Set</a><br>
|
|
<b>prev:</b> <a href="S03_05.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/S03_05.HTM">3.5 Control Transfer Instructions</a><br>
|
|
<b>next:</b> <a href="S03_07.HTM" tppabs="http://webster.cs.ucr.edu/Page_TechDocs/Doc386/S03_07.HTM">3.7 Instructions for Block-Structured Languages</a>
|
|
</body>
|
|
|