Store String Instructions 

The store string instructions are useful in initializing a block of memory with a particular value. They have one of the following forms:

STOS dest_string
STOSB
STOSW
STOSD

The STOS instruction only requires a destination string (pointed at by ES:DI). The source operand is the value in the AL (STOS), AX (STOSW), or EAX (STOSD) register. The STOS instruction stores the value in the accumulator (AL, AX, or EAX) into the corresponding locations pointed by ES:DI and then increments (or decrements) DI by one, two, or four.

When the first form, STOS des_string, is used the assembler will replace it by STOSB, STOSW, or STOSD depending on the size of the operand dest_string.

The semantics of the instructions STOSB, STOSW, and STOSD are illustrated below:

InstructionDescription
STOSB ES:[DI]<-AL ;
IF (DF=0)
DI=DI+1
ELSE
DI=DI-1
STOSW ES:[DI+1:DI]<-AX ;
IF (DF=0)
DI=DI+2
ELSE
DI=DI-2
STOSD ES:[DI+3:DI]<-EAX ;
IF (DF=0)
DI=DI+4
ELSE
DI=DI-4

The STOSB instruction

The STOSB stores the value in the AL register into the byte addressed by ES:[DI]. DI is then incremented (if DF=0) or decremented (if DF=1) by 1.

The STOSW instruction

The STOSW instruction stores the AX register into the word addressed by ES:[DI]. DI is then incremented (if DF=0) or decremented (if DF=1) by 2.

The STOSD instruction

The STOSD instruction stores the EAX register into the double word addressed by ES:[DI]. DI is then incremented (if DF=0) or decremented (if DF=1) by 4.

Note that it is possible to use only the unconditional repeat prefix REP with store string instructions.

The next example illustrates the use of the store string instructions in initializing a block of memory by a particular value.

Example: Initialize an array of 100 bytes by the value -1.
	MOV DI, offset Array
	MOV CX, 100
	MOV AX, DS
	MOV ES, AX
	CLD
	MOV	AL, -1
REP 	STOSB
	...	

Note that we can initialize the Array using the dup operator by using the following declaration in the data segment:

Array DB 100 DUP(-1)

However, in many programming instances it is required to initialize an array repeatedly and in this case the store string instructions can be used.