Introduction 

A string is a sequence of characters. String processing is an important aspect of any programming language. String handling instructions allow the programmer to deal with strings in an efficient way. String instructions operate on blocks, or contiguous linear arrays, of memory.

There are five classes of string instructions:

Move string instructions: MOVS, MOVSB, MOVSW, MOVSD. These instructions are used to copy a block of memory to another block of memory.
Compare string instructions: CMPS, CMPSB, CMPSW, CMPSD. These instructions are used for comparing two blocks of memory.
Scan string instructions: SCAS, SCASB, SCASW, SCASD. These instructions are used for scanning a block of memory for a particular value.
Store string instructions: STOS, STOSB, STOSW, STOSD. These instructions are used for storing values into a block of memory.
Load string instructions: LODS, LODSB, LODSW, LODSD. These instructions are used for loading values from a block of memory.

All the string instructions assume the following:

The address of the source string is stored in the SI register.
The address of the destination string is stored in the DI register.
The offset stored in SI is relative to the Data Segment (DS).
The offset stored in DI is relative to the Extra Segment (ES).
All string instructions do not have explicit operands except the general forms of string instructions: MOVS, CMPS, SCAS, STOS, LODS.
The operands used in the general forms of string instructions are just used to determine the size of the operands.
Depending on the size of the operands:
the MOVS instruction will be replaced by MOVSB, MOVSW, or MOVSD.
the CMPS instruction will be replaced by CMPSB, CMPSW, or CMPSD.
the SCAS instruction will be replaced by SCASB, SCASW, or SCASD.
the STOS instruction will be replaced by STOSB, STOSW, or STOSD.
the LODS instruction will be replaced by LODSB, LODSW, or LODSD.
Depending on the direction flag, the SI and/or DI registers are either incremented or decremented to point at the next address in the string.
If the Direction Flag is 0, the SI and/or DI are incremented.
If the Direction Flag is 1, the SI and/or DI are decremented.
The SI and/or DI registers are either incremented or decremented by 1, 2, or 4 depending on the string instruction executed.
The instruction CLD clears the Direction Flag.
The instruction STD sets the Direction Flag.

Since the offset of the SI is relative to the DS and the offset of the DI is relative to the ES, we need to make ES equal to DS if both strings are in the DS. This can be easily accomplished by the following instructions:

	PUSH DS
	POP ES