Section 10
Section 10
Strings
DF
Direction Flag:
CLD -> DF = 0
STD -> DF = 1
String Instructions
MOVSB/MOVSW
• MOVSB
The MOVSB (move string, byte) instruction fetches
the byte at address DS:SI, stores it at address ES:DI
and then increments or decrements the SI and DI
registers by one.
• MOVSW
The MOVSW (move string, word) instruction fetches
the word at address DS:SI, stores it at address ES:DI
and then increments or decrements SI and DI by
two.
MOVSB
DS:SI
ES:DI
MOVSB
DS:SI
ES:DI
.model small
.stack 128
.data MOV CX, 10
CLD
Array1 db 10 dup('m') next: MOVSB
Array2 db 10 dup(?) LOOP next
mov ah, 4ch.
.code int 21h
main proc far main endp
; set segment registers: end main
mov ax, data
mov ds, ax
mov es, ax
mov DI, offset Array2
mov SI, offset Array1
REP
.model small
.stack 128
.data MOV CX, 10
DS:SI
CMP
ES:DI
CMPSB
DS:SI
ES:DI
.model small
.stack 128
.data MOV CX, 10
CLD
Array1 db 10 dup('m')
Array2 db 10 dup('m') Next: CMPSB
Equal db 0 jne skip
loop Next
.code
main proc far Skip:
; set segment registers: cmp cl,0
mov ax, data jne end
mov ds, ax mov Equal ,1
mov es, ax end:
mov DI, offset Array2 mov ah, 4ch
mov SI, offset Array1 int 21h
main endp
end main
REPE/REPZ
REPNE/REPNZ
REPE/REPZ : The string instruction will repeat
execution as long as CX is not equal to 0 and
also the result of comparison sets the ZF to 1,
i.e. the two compared operands are equal.
.model small
.stack 128
.data MOV CX, 10
CLD
Array1 db 10 dup('m')
Array2 db 10 dup('m') repe CMPSB
Equal db 0
cmp cl,0
.code jne end
main proc far mov Equal ,1
; set segment registers: end:
mov ax, data mov ah, 4ch
mov ds, ax int 21h
mov es, ax main endp
mov DI, offset Array2 end main
mov SI, offset Array1
SCASB/ SCASW
SCASB/SCASW :
The scan string instructions are useful in
searching for a particular value or a character in
a string. These instructions are used to compare
arrays to a byte or word. Instructions only
require a destination string (pointed at by ES:DI).
The source operand is the value in the AL
(SCASB), AX (SCASW). Then increments (or
decrements) DI by one or two.
SCASB
AL
CMP
ES:DI
SCASB
ES:DI
.code Skip:
main proc far cmp cx,0
; set segment registers: je end
mov ax, data mov found , 1
mov ds, ax
mov es, ax end:
mov DI, offset Array1
mov al , Letter mov ah, 4ch
int 21h
main endp
end main
REPE/REPZ
REPNE/REPNZ
REPE/REPZ : The string instruction will repeat
execution as long as CX is not equal to 0 and
also the result of comparison sets the ZF to 1,
i.e. the two compared operands are equal.
ES:DI
STOSB
ES:DI
.model small
.stack 128
.data MOV CX, 5
CLD
Array1 db “HELLO”
Letter db ‘K’ loop1: stosb
loop loop1
.model small
.stack 128
.data MOV CX, 5
CLD
Array1 db “HELLO”
Letter db ‘K’ REP stosb
AL
LODSB
DS:SI