0% found this document useful (0 votes)
26 views33 pages

Section 10

Uploaded by

adhamkassem898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views33 pages

Section 10

Uploaded by

adhamkassem898
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Section 10

Strings
DF
Direction Flag:

0 -> operation for the DI and SI (increment)


1 -> operation for the DI and SI (Decrement )
DF

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

Then increments SI and DI (IF DF==0)

ES:DI

N.B. SI and DI are decremented if DF==1


Example MOVSB
Copy all elements in array1 to array2

.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

REP: Allows the execution to be repeated CX


times.
Example MOVSB using REP
Copy all elements in array1 to array2

.model small
.stack 128
.data MOV CX, 10

Array1 db 10 dup('m') CLD


Array2 db 10 dup(?) REP MOVSB

.code mov ah, 4ch.


main proc far int 21h
; set segment registers: main endp
mov ax, data end main
mov ds, ax
mov es, ax
mov DI, offset Array2
mov SI, offset Array1
CMPSB/CMPSW
• CMPSB
This instruction is used to compare arrays of bytes. It
compares the byte addressed by DS:[SI] to the byte
addressed by ES:[DI]. Then, SI and DI are either
incremented or decremented by 1 depending on the
Direction Flag.
• CMPSW
This instruction is used to compare arrays of words. It
compares the word addressed by DS:[SI] to the word
addressed by ES:[DI]. Then, SI and DI are either
incremented or decremented by 2 depending on the
Direction Flag.
CMPSB

DS:SI

CMP
ES:DI
CMPSB

DS:SI

Then increments SI and DI (IF DF==0)

ES:DI

N.B. SI and DI are decremented if DF==1


Example CMPSB
Compare all elements in array1 to array2; if the 2 arrays are identical
The Equal variable should be one, if not Equal variable should be 0

.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.

REPNE/REPNZ : 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 0,
i.e. the two compared operands are not equal.
Example CMPSB and REPE
Compare all elements in array1 to array2; if the 2 arrays are identical
The Equal variable should be one, if not Equal variable should be 0

.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

Then increments SI and DI (IF DF==0)

ES:DI

N.B. SI and DI are decremented if DF==1


Example SCASB
Search of Letter in array1; if found the Found variable should be one,
if not Found variable should be 0

.model small MOV CX, 5


.stack 128 CLD
.data
Loop1: scasb
Array1 db "HELLO" je skip
Letter db 'z' loop loop1
Found db 0

.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.

REPNE/REPNZ : 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 0,
i.e. the two compared operands are not equal.
Example SCASB and REPNE
Search of Letter in array1; if found the Found variable should be one,
if not Found variable should be 0

.model small MOV CX, 5


.stack 128 CLD
.data
repne scasb
Array1 db "HELLO"
Letter db 'z'
Found db 0 Skip:
cmp cx,0
.code je end
main proc far mov found , 1
; set segment registers:
mov ax, data end:
mov ds, ax
mov es, ax mov ah, 4ch
mov DI, offset Array1 int 21h
mov al , Letter main endp
end main
STOSB/STOSW
STOSB/STOSW instructions store the value in the
accumulator (AL/AX) into the corresponding
locations pointed by ES:DI and then increments
(or decrements) DI by one or two.
STOSB
AL

ES:DI
STOSB

Then increments SI and DI (IF DF==0)

ES:DI

N.B. SI and DI are decremented if DF==1


Example STOSB
Copy the letter in Letter variable into all elements of array1

.model small
.stack 128
.data MOV CX, 5
CLD
Array1 db “HELLO”
Letter db ‘K’ loop1: stosb
loop loop1

.code mov ah, 4ch


main proc far int 21h
; set segment registers: main endp
mov ax, data end main
mov ds, ax
mov es, ax
mov DI, offset Array1
mov al , Letter
Example STOSB and REP
Copy the letter in Letter variable into all elements of array1

.model small
.stack 128
.data MOV CX, 5
CLD
Array1 db “HELLO”
Letter db ‘K’ REP stosb

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 Array1
mov al , Letter
LODSB/LODSW
LODSB/LODSW instructions load the value from
the corresponding locations pointed by DS:SI
into the accumulator (AL/AX) and then
increments (or decrements) SI by one or two.
LODSB
DS:SI

AL
LODSB

DS:SI

Then increments SI and DI (IF DF==0)

N.B. SI and DI are decremented if DF==1


Uppercase to Lowercase using LODSB
and STOSB
.model small CLD
.stack 128
.data loop1: LODSB
cmp al,'A'
Array1 db "HELLO" jb loop2
Array2 db 5 dup(?) cmp al,'Z'
ja loop2
.code add al,20H
main proc far loop2: stosb
mov ax, data loop loop1
mov ds, ax
mov es, ax
mov SI,offset Array1 mov ah, 4ch
mov DI,offset Array2 int 21h
MOV CX, 5 main endp
end main
Repeat compatibility
REP -> MOVS, STOS

REPE/REPZ, REPNE/REPNZ -> CMPS, SCAS


Thanks

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy