Escolar Documentos
Profissional Documentos
Cultura Documentos
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
Move string
MOVSB
MOVSW
Compare string
CMPSB
CMPSW
Scan string
SCAS DST
SCASB
SCASW
Load string
LODS SRC
LODSB
LODSW
Store string
STOS DST
STOSB
STOSW
Description**
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
MOVE:
;PUT LENGTH IN CX
MOV (DI), AL
;TO DESTINATION
INC SI
INC DI
LOOP MOVE
;ASUME (DS)=(ES)
CLD
;CLEARALL FLAG
;FOR AUTO-INCREMENTING
LOOP MOVE
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)
;PUT LENGTH IN CX
CLD
;CLEAR DF FLAG
CMP SI, DI
JA MOVE
STD
ADD SI, CX
DEC SI
ADD DI, CX
DEC DI
MOVE:
MOVSB
LOOP MOVE
.
Figure 1-3
Figure
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
Termination Condition
(CX)=0
(CX)=0 or (ZF)=0
(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:
with
REP
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
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
DONE:
.
.
.
Figure 1-7