The String Instructions
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
In8086assemblylanguage,amemorystringorstringissimplyabyte
orwordarray.
String Operations:
Copy a string into another string.
Search a string for a particular byte or word.
Store characters in a string.
Compare strings of characters alphabetically.
String operation provide automatic updating of pointer registers and
allow memory to memory operations.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
•TheFLAGSregistercontainssixstatusflagsandthreecontrolflags.
•Thecontrolflagsareusedtocontroltheprocessor’soperations.
•ThepurposeofDFistodeterminethedirectioninwhichstring
operationswillproceed.
•TheseoperationsareimplementedbythetwoindexregistersSI&
DI.
•IfDF=0,SIandDIproceedinthedirectionofincreasingmemory
addresses:fromlefttorightacrossthestring.
•Conversely,ifDF=1,SIandDIproceedinthedirectionof
decreasingmemoryaddresses:fromrighttoleft.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example:
STRING1DB'ABCDE‘
If the string is stored in memory starting at offset 0200h:
Offset address ContentASCII character
0200h 041h A
0201h 042h B
0202h 043h C
0203h 044h D
0204h 045h E
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
CLD and STD
To make DF = 0, use the CLDInstruction
CLD ;clear directional flag
To make OF= 1, use the STDinstruction:
STD ; set directional flag
CLD and STD have no effect on the other flags.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Moving a String
MOVSB ; move string byte
•ThisinstructioncopiesthecontentsofthebyteaddressedbyDS:SI,tothebyte
addressedbyES:DI.
•Thecontentsofthesourcebyteareunchanged.
•Afterthebytehasbeenmoved,bothSIandDIareautomaticallyincrementedif
DF=0,ordecrementedifDF=1.
MOVSW ; move string word
•Thisinstructionmovesawordfromthesourcestringtothedestinationstring.
•LikeMOVSB,itexpectsDS:SItopointtoasourcestringword,andES:DItopoint
toadestinationstringword.
•Afterastringwordhasbeenmoved,bothSIandDIareincreasedby2ifDF=0,or
aredecreasedby2ifDF=1.
The REP prefix causes MOVSB/MOVSW to be executed N times.
MOVSB and MOVSW have no effect on the flags.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example01:Definetwostringsas
followsandmovethecontentsof
STRINGI(thesourcestring)into
STRING2(thedestinationstring).
.DATA
STRING1 DB‘HELLO’
STRING2 DB5 DUP (?)
Solution:
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
LEA SI, STRING1
LEA DI, STRING2
CLD
MOV CX, 5
REP MOVSB
Before MOVSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’
Offset01234
DI
STRING2
Offset01234
After 1
st
MOVSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’
Offset01234
DI
STRING2‘H’
Offset01234
After 2
nd
MOVSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’
Offset01234
DI
STRING2‘H’‘E’
Offset01234
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example 02: Write instructions to
copy STRING1 into STRING2 in
reverse order.
Solution:
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
LEA SI, STRING1+4
LEA DI, STRING2
STD
MOV CX, 5
MOVE:
MOVSB
ADD DI, 2
LOOP MOVE
Before MOVSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’
Offset01234
DI
STRING2
Offset01234
After 2
nd
MOVSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’
Offset01234
DI
STRING2‘O’‘L’
Offset01234
After 1
st
MOVSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’
Offset01234
DI
STRING2‘O’
Offset01234
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example03:Writeinstructionstoinsert30
between20and40ofARR.
ARR DW 10, 20, 40, 50, 60, ?
Solution:
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
STD
LEA SI, ARR+8h
LEA DI, ARR+Ah
MOV CX, 3
REP MOVSW
MOV WORD PTR [DI], 30
Before MOVSW
SI
ARR 1020405060?
Offset0246810
DI
ARR
Offset0246810
After 1
st
MOVSW
SI
ARR 1020405060?
Offset0246810
DI
ARR 102040506060
Offset0246810
After 2
nd
MOVSW
SI
ARR 1020405060?
Offset0246810
DI
ARR 102040505060
Offset0246810
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Store String
STOSB ; store string byte
moves the contents of ALregister to the byte addressed by ES:DI. DI Is
incremented if DF =0 or decremented if DF = 1.
STOSW ; store string word
moves the contents of AXto the word at address ES:DIand updates DI
by 2 according to the DF setting.
STOSB and STOSW have no effect on the flags.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example 04: Write instructions to
store two ‘A’ in STRING1.
Solution:
MOV AX, @DATA
MOV ES, AX
LEA DI, STRING1
CLD
MOV AL, ’A’
STOSB
STOSB
Before STOSB
DI
STRING1‘H’‘E’‘L’‘L’‘O’ ‘A’
Offset01234 AL
After1
st
STOSB
DI
STRING1‘A’‘E’‘L’‘L’‘O’ ‘A’
Offset01234 AL
After2
nd
STOSB
DI
STRING1‘A’‘A’‘L’‘L’‘O’ ‘A’
Offset01234 AL
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Load String
LODSB ;load string byte
moves the byte addressed by DS:SIinto AL. SI is then incremented if DF
=0 or decremented if DF= 1.
LODSW ;load string word
moves the word addressed by DS:SIinto AX; SI is Increased by 2 if DF =
0 or decreased by 2 if DI= = 1.
LODSB and LODSW have no effect on the flags.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example 05: Write instructions to
successively load 1
st
and 2
nd
bytes
of STRING1 into AL.
Solution:
MOV AX, @DATA
MOV DS, AX
LEA SI, STRING1
CLD
LODSB
LODSB
Before LODSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’ ‘A’
Offset01234 AL
After1
st
LODSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’ ‘H’
Offset01234 AL
After2
nd
LODSB
SI
STRING1‘H’‘E’‘L’‘L’‘O’ ‘E’
Offset01234 AL
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Exercise01:Writeaprogramthatreadsastring,storeituntil
acarriagereturnistypedanddisplaysitonthenextline.Ifthe
usermakesatypingmistakeandhitsthebackspacekey,the
previouscharacterisremovedfromthestring.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Scan String
SCASB;scan string byte
usedtoexamineastringforatargetbyte.ThetargetbyteiscontainedinAL.SCASB
subtractsthestringbytepointedtobyES:DIfromthecontentsofALandusestheresultto
settheflags.Theresultisnotstored.Afterward,DIisincrementedifDF=0ordecremented
ifDF=1.
SCASW;scan string word
ThetargetwordisinAX.SCASWsubtractsthewordaddressed
byES:DlfromAXandsetstheflags.DIisincreasedby2ifDF=0ordecreasedby2ifDF=
1.
All the status flags are affected by SCASB and SCASW.
REPNE SCASB ; repeat SCASB while not equal (to target)
will repeatedly subtract each string byte from AL, update DJ, and decrement CX until there
is a zero result (the target is found) or CX = 0 (the string ends).
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Before SCASB
DI
STRING1‘A’‘B’‘C’ ‘B’
Offset012 AL
After1
st
SCASB
DI
STRING1‘A’‘B’‘C’ ‘B’
Offset012 AL
(ZF=0)
After2
nd
SCASB
DI
STRING1‘A’‘B’‘C’ ‘B’
Offset012 AL
(ZF=1)
STRING1 DB 'ABC'
MOV AX, @DATA
MOV ES, AX
CLD
LEA DI, STRING1
MOV AL, 'B‘
SCASB
SCASB
# Examine the first two bytes of
STRING1, looking for "B"
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Compare String
CMPSB ;compare string byte
subtractsthebytewithaddressES:DIfromthebytewithaddressDS:SI,
andsetstheflags.Theresultisnotstored.Afterward,bothSIandDI
areincrementedifDF=0,ordecrementedifDF=1.
CMPSW ;comparestringword
ItsubtractsthewordwithaddressES:DIfromthewordwhoseaddress
isDS:SI,andsetstheflags.IfDI=0,SIandDIareincreasedby2;ifDF=
1,theyaredecreasedby2.CMPSWisusefulIncomparingwordarrays
ofnumbers.
All the status flags arc affected by CMPSB and CMPSW.
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Before CMPSB
SI
STRING1‘A’‘C’‘D’
Offset012
DI
STRING2‘A’‘B’‘C’
Offset012
After 2
nd
CMPSB
SI
STRING1‘A’‘C’‘D’
Offset012
DI
STRING2‘A’‘B’‘C’
Offset012
After 1
st
CMPSB
SI
STRING1‘A’‘C’‘D’
Offset012
DI
STRING2‘A’‘B’‘C’
Offset012
Result=41h-41h=0
(not stored)
ZF=1, SF=0
Result=43h-42h=1
(not stored)
ZF=0, SF=0
REPE and REPZ
String comparison may be done by attaching the prefix REPE(repeat while equal)
or REPZ (repeat while zero) to CMPSB or CMPSW. CX is initialized to the number of
bytes in the shorter string, then
REPE CMPSB ;compare string bytes while equal
REPE CMPSW ;compare string words while equal
repeatedlyexecutesCMPSBorCMPSWanddecrementsCXuntil(1)thereisa
mismatchbetweencorrespondingstringbytesorwords,or(2)CX=0.
Theflagsaresetaccordingtotheresultofthelastcomparison
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET
Example06:Writean8086ALPtocompareasourcestringof50wordspointedto
byanoffsetof2000hinthesegmentwithadestinationstringpointedtobyan
offset3000hinanothersegment.Theprogramshouldbehaltedassoonasa
matchisfoundortheendofstringisreached.
Solution:
MOV AX, @DATA
MOV DS, AX
MOV ES, AX
MOV SI, 2000H
MOV DI, 3000H
MOV CX, 50
CLD
REPNE CMPSW
HLT
A. S. M. Badrudduza, Asst. Prof., Dept. of ETE, RUET