Вы находитесь на странице: 1из 6

STRING INSTRUCTIONS

The string instructions are summarized in figure 1.1 below. .The string instructions can operate
on only a single byte or word unless they are used with the REP prefix , hence they are often
referred to as string primitives.
Name

Mnemonic and Format*

Move string

MOVS DST, SRC

Move byte string


Move word string

MOVSB
MOVSW

Compare string

CMPS SRC, DST

Compare byte string


Compare word string

CMPSB
CMPSW

Scan string

SCAS DST

Scan byte string


Scan word string

SCASB
SCASW

Load string

LODS SRC

Load byte string


Load word string

LODSB
LODSW

Store string

STOS DST

Store byte string


Store word string

STOSB
STOSW

Description**

((DI)) <- ((SI))


Byte operands
(SI) <- (SI)1, (DI) <- (DI)1
Word operands
(SI) <- (SI)2, (DI) <- (DI)2

((SI)) <- ((DI))


Byte operands
(SI) <- (SI)1, (DI) <- (DI)1
Word operands
(SI) <- (SI)2, (DI) <- (DI)2

Byte operand
((AL)) - ((DI)), (DI) <- (DI)1
Word operand
((AX)) - ((DI)), (DI) <- (DI)2

Byte
(AL)
Word
(AX)

operand
<- (SI), (SI) <- (SI)1
operand
<- (SI), (SI) <- (SI)2

Byte operand
((DI)) <- ((AL)), (DI) <- (DI)1
Word operand
((DI)) <- ((AX)), (DI) <- (DI)2

*The B suffix indicates byte operands and the W suffix indicates.


**Incrementing (+) is used if DF=0 and decrementing (-) is used
if DF=1.
Flags: CMPS and SCAS affect all condition flags and
MOVS,LODS and STOS affect no flags.
Addresing modes: Operands are implied.

Figure 1-1 String primitives


When working with strings, the advantages of the MOVS and CMPS instructions over the MOV
and CMP instructions are:
1. They are only 1 byte long.
2. Both operands
3. Their auto-indexing obviates the need for separate incrementing or decrementing
instructions, thus decreasing overall processing time.
As an example consider the problem of moving the contents of a
block of memory to another area in memory. A solution that uses
only the MOV instruction, which cannot perform a memory-to-memory
transfer, is shown in Fig.1-2(a).A solution

MOVE:

MOV SI, OFFSET STRING1

;USE SI AS SOURCE INDEX

MOV DI, OFFSET STRING2

;USE DI AS DESTINATION INDEX

MOV CX, LENGTH STRING1

;PUT LENGTH IN CX

MOV AL, (SI)

;MOVE BYTE FROM SOURCE

MOV (DI), AL

;TO DESTINATION

INC SI

;INCREMENT SOURCE INDEX

INC DI

;INCREMENT DESTINATION INDEX

LOOP MOVE

(a)Uses the MOV instruction


MOV SI, OFFSET STRING1

;ASUME (DS)=(ES)

MOV DI, OFFSET STRING2


MOV CX, LENGTH STRING1
MOVE:

CLD

;CLEARALL FLAG

MOVS STRING2, STRING1

;FOR AUTO-INCREMENTING

LOOP MOVE

(B)Uses primitive MOVS


Figure 1-2 Program sequences for moving a block data
Note that the second program sequence may move either bytes or words, depending on the type
of STRING1 and STRING2. This task can be performed even more efficiently by applying the
REP prefix to eliminate the explicit loop.

The program sequence given in Fig.1-3 demonstrates the use of the DF flag by showing howdata
can be moved from an area to an overlapping area.
MOV SI, SRCADDR

;ASSUME (DS)=(ES)

MOV DI, DSTADDR


MOV CX, N

;PUT LENGTH IN CX

CLD

;CLEAR DF FLAG

CMP SI, DI

;IF ADDR OF STRG1 IS GREATER

JA MOVE

;THAN ADDR OF STRG2, START FROM

STD

;THE FIRST ELEMENT;OTHERWISE,

ADD SI, CX

;START FROM THE LAST ELEMENT

DEC SI
ADD DI, CX
DEC DI
MOVE:

MOVSB
LOOP MOVE
.

Figure 1-3

Figure

Moving a block of data between two overlapping areas

1-4 Possible cases when moving data between overlapping areas

REP PREFIX
Because string operations inherently involve looping, the 8086 machine language includes a
prefix that considerably simplifies the use of string primitives with loops.This prefix has machine
code:
1111001Z
where, for the CMPS and SCAS primitives, the Z bit helps control the loop. By prefixing
MOVS,LODS and STOS, which donot affect the flags, with the REP prefix 11110011 they are
repeated the number of times indicated by the CX register according to the following steps:
1. If (CX)=0, exit the REP operation
2. Perform the specified primitive
3. Decrement CX by 1
4. Repeat steps 1 through 3
For the CMPS and SCAS primitives, which do affects the flags, the prefix causes them to be
repeated the numberof times indicated by the CX register or until the Z-bit does not match the
ZF flag, whichever occurs first.
In assembler language the prefix is invoked by placing the appropriate repeat (REP) mnemonic
before the primitive.
Figure 1-5 REP prefix
Name

Mnemonic and Format

Termination Condition

Repeat string operation


until CX=0

REP String Primitive*

(CX)=0

Repeat string operation


while equal or zero

REPE String primitive**


or
REPZ

(CX)=0 or (ZF)=0

Repeat string operation


while not equal or
not zero

REPNE String primitive**


or
REPNZ

(CX)=0 or (ZF)=0

*MOVS,LODS or STOS
**CMPS or SCAS
Note: In all cases (CX) <- (CX)-1 with each operation.

As an example of the use of the REP prefix let us reconsider the program sequence for moving a
string within memory given in Fig. 1-2(b).
By replacing the explicit loop
MOVE:

MOVS STRING2, STRING1


LOOP MOVE

with
REP

MOVS string2, STRING1

not only is the code simplified, but the execution time is reduced from
18 + 17 = 35 clock cycles per iteration
to

9 + 17 = 26 clock cycles

for the first iteration and 17 clock cycles for each subsequent iteration.
MOV
LES

BX, TABLE SIZE


DI, TABLEPT

MOV
LDS

DX, DI
SI, SYMBOLPT

CLD
LOOP1:
MOV
REPE
JE
ADD
MOV
MOV
DEC
JNE
NOT FOUND: .
.
FOUND:
.
.

Figure 1-6

CX, 8
CMPSB
FOUND
DX, 20
DI, DX
SI, OFFSET SYMBOL
BX
LOOP1

LOOP1:

MOV
MOV
MOV
CLD
REPNZ
JNZ
MOV
JMP

;LOAD NO.OF ENTRIES INTO BX


;LOAD OFFSET AND SEGMENT ADDRESS
;OF THE ARRAY TO BE SEARCHED
;SAVE OFFSET IN DX
;LOAD OFFSET AND SEGMENT ADDRESS
;OF THE SYMBOL TOBE SEARCHED FOR
;START FROM THE FIRST ENTRY
;COMPARE TWO CORRESPONDING
;CHARACTERS UNTIL NO MATCH
;POINT TO THE NEXT TABLE ENTRY
;POINT TO THE FIRST CHARACTER

Search a table for a given name with eigthcharacters


CX, LENGTH ASCII_STG ;INITIALIZE SEARCH
AL, '$'
DI, OFFSET ASCII_STG
SCAS ASCII_STG
DONE
BYTE PTR[DI-1],'_'
SHORT LOOP1

;SEARCH FOR '$' AND


;REPLACE WITH '_'

DONE:

.
.
.

Figure 1-7

Replace each "$" in a character string with a "_"