x86 Assembly Language Reference Manual

String Instructions

All Intel string op mnemonics default to long.

Move Data from String to String (movs)

movs{bwl}
movs{bwl} m[8|16|32], reg[16|32] 
Operation

move {bwl} [(E)SI] -> ES: (E)DI]

move {bwl} DS: [(E)SI] -> ES: [(E)DI]

Description

Copies the byte, word, or long in [(E)SI] to the byte, word, or long in ES:[(E)DI}. Before executing the move instruction, load the index values into the SI source- and DI destination-index registers.

The destination operand must be addressable from the ES register; it cannot span segments. A source operand, however, can span segments; the default is DS.

After the data is moved, both the source- and destination-index registers are automatically incremented or decremented as determined by the value of the direction flag (DF). The index registers are incremented if DF = 0 (DF cleared by a cld instruction); they are decremented if DF = 1 (DF set by a std instruction). The increment/decrement count is 1 for a byte move, 2 for a word, and 4 for a long.

For a block move of CX bytes or words, precede a movs instruction with a rep prefix.

Example

Copy the 8-bit byte from the DS:[(E)SI] to the ES:[(E)DI] register.

movsb

Compare String Operands (cmps)

cmps{bwl}
Operation

compare DS:[(E)SI] with ES:[(E)DI]

Description

Compares the byte, word, or long in DS:[(E)SI] with the byte, word, or long in ES:[(E)DI}. Before executing the cmps instruction, load the index values into the SI source- and DI destination-index registers.

cmps subtracts the operand indexed by the destination-index from the operand indexed by the source-index register.

After the data is compared, both the source- and destination-index registers are automatically incremented or decremented as determined by the value of the direction flag (DF). The index registers are incremented if DF = 0 (DF cleared by a cld instruction); they are decremented if DF = 1 (DF set by a std instruction). The increment/decrement count is 1 for a byte move, 2 for a word, and 4 for a long.

For a block compare of CX or ECX bytes, words or longs, precede a cmps instruction with a repz or repnz prefix.

Example

Compare the 8-bit byte in the DS:[(E)SI] register to the ES:[(E)DI] register.

cmpsb

Compare the 16-bit word in the DS:[(E)SI] register to the ES:[(E)DI] register.

cmpsw

Compare the 32-bit word in the DS:[(E)SI] register to the ES:[(E)DI] register.

cmpsl

Store String Data (stos)

stos{bwl}

Operation

store [AL|AX|EAX] -> ES:[(E)DI]

Description

Transfers the contents of the AL, AX, or EAX register to the memory byte or word addressed in the destination register relative to the ES segment. Before executing the move instruction, load the index values into the DI destination-index register.

The destination operand must be addressable from the ES register; it cannot span segments.

After the data is transferred, the destination-index register is automatically incremented or decremented as determined by the value of the direction flag (DF). The index registers are incremented if DF = 0 (DF cleared by a cld instruction); they are decremented if DF = 1 (DF set by a std instruction). The increment/decrement count is 1 for a byte move, 2 for a word, and 4 for a long.

For a block transfer of CX bytes, words or longs, precede a stos instruction with a rep prefix.

Example

Transfer the contents of the AL register to the memory byte addressed in the destination register, relative to the ES segment.

stosb

Transfer the contents of the AX register to the memory word addressed in the destination register, relative to the ES segment

stosw

Transfer the contents of the EAX register to the memory double-word addressed in the destination register, relative to the ES segment

stosl

The Load String Operand (lods)

lods{bwl}
Operation

load ES:[(E)DI] -> [AL|AX|EAX]

Description

Loads the memory byte or word addressed in the destination register into the AL, AX, or EAX register. Before executing the lods instruction, load the index values into the SI source-index register.

After the data is loaded, the source-index register is automatically incremented or decremented as determined by the value of the direction flag (DF). The index register is incremented if DF = 0 (DF cleared by a cld instruction); it is decremented if DF = 1 (DF set by a std instruction). The increment/decrement count is 1 for a byte move, 2 for a word, and 4 for a long.

For a block transfer of CX bytes, words or longs, precede a lods instruction with a rep prefix; however, lods is used more typically within a loop construct where further processing of the data moved into AL, AX, or EAX is usually required.

Example

Load the memory byte addressed in the destination register, relative to the ES segment register, into the AL register.

lodsb

Load the memory word addressed in the destination register, relative to the ES segment register, into the AX register.

lodsw

Load the memory double-word addressed in the destination register, relative to the ES segment register, into the EAX register.

lodsl

Compare String Data (scas)

scas{bwl}
Operation

compare ES:[(E)DI] with [AL|AX|EAX]

Description

Compares the memory byte or word addressed in the destination register relative to the ES segment with the contents of the AL, AX, or EAX register. The result is discarded; only the flags are set.

Before executing the scas instruction, load the index values into the DI destination-index register. The destination operand must be addressable from the ES register; it cannot span segments.

After the data is transferred, the destination-index register is automatically incremented or decremented as determined by the value of the direction flag (DF). The index registers are incremented if DF = 0 (DF cleared by a cld instruction); they are decremented if DF = 1 (DF set by a std instruction). The increment/decrement count is 1 for a byte move, 2 for a word, and 4 for a long.

For a block search of CX or ECX bytes, words or longs, precede a scas instruction with a repz or repnz prefix.

Example

Compare the memory byte addressed in the destination register, relative to the ES segment, with the contents of the AL register.

scasb

Compare the memory word addressed in the destination register, relative to the ES segment, with the contents of the AX register

scasw

Compare the memory byte double-word addressed in the destination register, relative to the ES segment, with the contents of the EAX register

scasl

Look-Up Translation Table (xlat)

xlat

Operation

set AL to DS:[(E)BX + unsigned AL]

Description

Changes the AL register from the table index to the table entry. AL should be the unsigned index into a table addressed by DS:BX (16-bit address) or DS:EBX (32-bit address).

Example

Change the AL register from the table index to the table entry.

xlat

Repeat String Operation (rep, repnz, repz)

rep;
repnz;
repz;
Operation

repeat string-operation until tested-condition

Description

Use the rep (repeat while equal), repnz (repeat while nonzero) or repz (repeat while zero) prefixes in conjunction with string operations. Each prefix causes the associated string instruction to repeat until the count register (CX) or the zero flag (ZF) matches a tested condition.

Example

Repeat while equal: Copy the 8-bit byte from the DS:[(E)SI] to the ES:[(E)DI] register.

rep; movsb

Repeat while not zero: Compare the memory byte double-word addressed in the destination register EDL, relative to the ES segment, with the contents of the EAX register.

repnz; scasl

Repeat while zero:Transfer the contents of the EAX register to the memory double-word addressed in the destination register EDL, relative to the ES segment.

repz; stosl