University of Technology
Department of Electrical Engineering
Microprocessor Engineering
Third Class
8086 Instruction Set
String Instruction
Assist. Prof. Dr. Hadeel Nasrat Abdullah
String Instructions
Flags
Mnem. Meaning Format Operation
Effected
((DS)*10+(SI)) ((ES)*10+ (DI))
MOVSB/
MOVS Move string (SI) 1 (SI); (DI) 1 (DI) [byte] none
MOVSW
(SI) 2 (SI); (DI) 2 (DI) [word]
((DS)*10+(SI)) - ((ES)*10+ (DI))
CMPSB/ O, S, Z, A, P,
CMPS Compare string (SI) 1 (SI); (DI) 1 (DI) [byte]
CMPSW C
(SI) 2 (SI); (DI) 2 (DI) [word]
(AL) or (AX) - ((ES)*10+ (DI))
SCASB/ O, S, Z, A, P,
SCAS Scan string (DI) 1 (DI) [byte]
SCASW C
(DI) 2 (DI) [word]
((DS)*10+ (SI)) (AL) or (AX)
LODSB/
LODS Load string (SI) 1 (SI) [byte] none
LODSW
(SI) 2 (SI) [word]
(AL) or (AX) ((ES)*10+ (DI))
STOSB/
STOS Store string (DI) 1 (DI) [byte] none
STOSW
(DI) 2 (DI) [word]
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 2
Auto-indexing of String Instructions
Execution of a string instruction causes the address indices in
SI and DI to be either automatically incremented or
decremented. The decision to increment or decrement is made
based on the status of the direction flag. The direction Flag:
Selects the auto increment (D=0) or the auto decrement (D=1)
operation for the DI and SI registers during string operations.
Mnemonic Meaning Format Operation Flags Effected
CLD Clear DF CLD 0 (DF) DF
STD Set DF STD 1 (DF) DF
Lecture 6: String Instruction 3
Assist. Prof. Dr. Hadeel N. Abdullah
Prefixes and the String Instructions
In most applications, the basic string operations must be
repeated in order to process arrays of data. Inserting a repeat
prefix before the instruction that is to be repeated does this,
the repeat prefixes of the 8086 are shown in table below.
Used
Mnemonic with Meaning
MOVS
Repeat while not end of string
REP STOS
CX ≠ 0
LODS
Repeat while not end of string and strings
CMPS
REPE/REPZ are equal
SCAS
CX ≠ 0 & ZF = 1
Repeat while not end of string and strings
REPNE/RE CMPS
are not equal
PNZ SCAS
CX ≠ 0 & ZF = 0
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 4
String Instruction Basics
Source DS:SI, Destination ES:DI
You must ensure DS and ES are correct
You must ensure SI and DI are offsets into DS and ES
respectively
Direction Flag (0 = Up, 1 = Down)
CLD - Increment addresses (left to right)
STD - Decrement addresses (right to left)
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 5
Example 1: Write an 8086 program loads the block of
memory locations from 0A000H through 0A00FH with
number 5H.
Solution:
Direction Flag=0, CLD
PA=0A000H
MOV AX, 0H PA=DS*10+DI
MOV DS, AX DS=0000H
MOV ES, AX ES=0000H
MOV AL, 05 SI=0000H
MOV DI, A000H DI=A000H
Counter=CX=0FH=16byte
MOV CX, 0FH
CLD
AGAIN: STOSB
LOOP AGAIN
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 6
Example 2: write an 8086 program to copy a block of 32
consecutive bytes from the block of memory locations starting at
address 2000H in the current Data Segment(DS) to a block of
locations starting at address 3000H in the current Extra Segment
(ES).
Solution: Direction Flag=0, CLD
CLD
MOV AX, data_seg DS=data_seg
MOV DS, AX ES=extra_seg
MOV AX, extra_seg
SI=2000H
MOV ES, AX DI=3000H
MOV CX, 20H Counter=CX=20H=32byte
MOV SI, 2000H
MOV DI, 3000H
MOVSB
REP
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 7
Example 3: Write an 8086 program that scans the 70 bytes
start at location D0H in the current Data
Solution:
MOV AX,data-seg
MOV DS, AX
CLD Direction Flag=0, CLD
MOV DI, 00D0H
MOV CX, 0046H DS=data_seg
MOV AL, 45H ES=extra_seg
REPNE
SI=0000H
SCASB DI=00D0H
DEC DI Counter=CX=46H=70byte
MOV [DI], 29H
HLT
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 8
Example 4: Write a program to move a block of 100 consecutive bytes of
data starting at offset address 400H in memory to another block of memory
locations starting at offset address 600H. Assume both block at the same
data segment F000H. Use loop instructions.
Solution:
MOV CX, 64H
MOV AX, F000H Direction Flag=0, CLD
MOV DS, AX
MOV ES, AX DS=F000H
MOV SI, 400H ES=F000H
MOV DI, 600H
SI=0400H
CLD DI=0600H
NXTPT: MOVSB Counter=CX=64H=100byte
LOOP NXTPT
HTL
Lecture 6: String Instruction 9
Assist. Prof. Dr. Hadeel N. Abdullah
Example 5: Explain the function of the following sequence of instructions
MOV DL, 05
MOV AX, 0A00H
MOV DS, AX
Direction Flag=0, CLD
MOV SI, 0
DS=0A00H
MOV CX, 0FH
ES=
AGAIN: INC SI
SI=0000H
CMP [SI], DL
DI=
LOOPNE AGAIN
Counter=CX=0FH=16byte
Solution: The first 5 instructions initialize internal registers and set up a
data segment the loop in the program searches the 15 memory locations
starting from Memory location A001H for the data stored in DL (05H). As
long as the value In DL is not found the zero flag is reset, otherwise it is set.
The LOOPNE Decrements CX and checks for CX=0 or ZF =1. If neither of
these conditions is met the loop is repeated. If either condition is satisfied
the loop is complete. Therefore, the loop is repeated until either 05 is found
or all locations in the address range A001H through A00F have been
checked and are found not to contain 5.
Lecture 6: String Instruction 10
Assist. Prof. Dr. Hadeel N. Abdullah
Example 6: Implement the previous example using SCAS
instruction.
Solution:
MOV AX, 0H Direction Flag=0, CLD
MOV DS, AX DS=0000H
MOV ES, AX ES=0000H
MOV AL, 05 SI=
DI=A001H
MOV DI, A001H
Counter=CX=0FH=16byte
MOV CX, 0FH
CLD
AGAIN: SCASB
LOOPNE AGAIN
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 11
Homework
Explain the function of the following sequence of
instructions
12
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah
Homework
1. Match the following
a) MOVSB/SW 1) loads AL/AX register by content of a string
b) CMPS 2) moves a string of bytes stored in source to destination
c) SCAS 3) compares two strings of bytes or words whose length is
stored in CX register
d) LODS 4) scans a string of bytes or words
Lecture 6: String Instruction
Assist. Prof. Dr. Hadeel N. Abdullah 13