String Instructions
String instructions were designed to
operate on large data structures.
The SI and DI registers are used as pointers
to the data structures being accessed or
manipulated.
The operation of the dedicated registers
stated above are used to simplify code
and minimize its size.
String Instructions
The registers(DI,SI) are automatically
incremented or decremented depending
on the value of the direction flag:
DF=0, increment SI, DI.
DF=1, decrement SI, DI.
To set or clear the direction flag one
should use the following instructions:
CLD to clear the DF.
STD to set the DF.
String Instructions
String instructions we will discuss:
LODSX
STOSX
MOVSX
CMPSX
SCASX
The REP/REPZ/REPNZ prefixes are used to
repeat the operation it precedes.
LODSB/LODSW
Loads the AL/AX registers with the content of
the memory byte/word pointed to by SI
relative to DS.
After the transfer is made, the SI register is
automatically updated as follows:
SI is incremented if DF=0.
SI is decremented if DF=1.
LODSB/LODSW
Examples:
LODSB
AL=DS:[SI]; SI=SI 1
LODSW
AX=DS:[SI]; SI=SI 2
LODSB/LODSW
Example
Assume:
Location Content
Re giste r SI 500H
Me mory loca tion 500H 'A'
Re giste r AL '2'
After execution of LODSB
If DF=0 then:
Location Content
Re giste r SI 501H
Me mory loca tion 500H 'A'
Re giste r AL 'A'
Else if DF=1 then:
Location Content
Re giste r SI 4FFH
Me mory loca tion 500H 'A'
Re giste r AL 'A'
STOSB/STOSW
Transfers the contents of the AL/AX registers
to the memory byte/word pointed to by DI
relative to ES.
After the transfer is made, the DI register is
automatically updated as follows:
DI is incremented if DF=0.
DI is decremented if DF=1.
STOSB/STOSW
Examples:
STOSB
ES:[DI]=AL; DI=DI 1
STOSW
ES:[DI]=AX; DI=DI 2
STOSB/STOSW
Example
Assume:
Location Content
Re giste r DI 500H
Me mory loca tion 500H 'A'
Re giste r AL '2'
After execution of STOSB
If DF=0 then:
Location Content
Re giste r DI 501H
Me mory loca tion 500H '2'
Re giste r AL '2'
Else if DF=1 then:
Location Content
Re giste r DI 4FFH
Me mory loca tion 500H '2'
Re giste r AL '2'
MOVSB/MOVSW
Transfers the contents of the memory
byte/word pointed to by SI relative to DS
to the memory byte/word pointed to by
DI relative to ES.
After the transfer is made, the DI register is
automatically updated as follows:
DI is incremented if DF=0.
DI is decremented if DF=1.
MOVSB/MOVSW
Examples:
MOVSB
ES:[DI]=DS:[SI]; DI=DI 1;SI=SI 1
MOVSW
ES:[DI]= DS:[SI]; DI=DI 2; SI=SI 2
MOVSB/MOVSW
Example
Assume:
Location Content
Re giste r SI 500H
Re giste r DI 600H
Me mory loca tion 500H '2'
Me mory loca tion 600H 'W '
After execution of MOVSB
If DF=0 then:
Location Content
Re giste r SI 501H
Re giste r DI 601H
Me mory loca tion 500H '2'
Me mory loca tion 600H '2'
Else if DF=1 then:
Location Content
Re giste r SI 4FFH
Re giste r DI 5FFH
Me mory loca tion 500H '2'
Me mory loca tion 600H '2'
CMPSB/CMPSW
Compares the contents of the memory
byte/word pointed to by SI relative to DS to
the memory byte/word pointed to by DI
relative to ES and changes the flags
accordingly.
After the comparison is made, the DI and SI
registers are automatically updated as
follows:
DI and SI are incremented if DF=0.
DI and SI are decremented if DF=1.
SCASB/SCASW
Compares the contents of the AL/AX
register with the memory byte/word pointed
to by DI relative to ES and changes the flags
accordingly.
After the comparison is made, the DI
register is automatically updated as follows:
DI is incremented if DF=0.
DI is decremented if DF=1.
REP/REPZ/REPNZ
These prefixes cause the string instruction
that follows them to be repeated the
number of times in the count register CX or
until:
ZF=0 in the case of REPZ (repeat while equal).
ZF=1 in the case of REPNZ (repeat while not
equal).