0% found this document useful (0 votes)
14 views18 pages

Assembly Language 6

The document provides an overview of string instructions in assembly language programming, focusing on the Direction Flag (DF) and its role in determining the direction of string operations. It details various instructions such as MOVSB, STOSB, LODSB, SCASB, and CMPSB, explaining their syntax and functionality in moving, storing, loading, scanning, and comparing strings. Additionally, it introduces the REP prefix for repeated operations and illustrates examples to clarify the concepts.

Uploaded by

afifmostakim
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)
14 views18 pages

Assembly Language 6

The document provides an overview of string instructions in assembly language programming, focusing on the Direction Flag (DF) and its role in determining the direction of string operations. It details various instructions such as MOVSB, STOSB, LODSB, SCASB, and CMPSB, explaining their syntax and functionality in moving, storing, loading, scanning, and comparing strings. Additionally, it introduces the REP prefix for repeated operations and illustrates examples to clarify the concepts.

Uploaded by

afifmostakim
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/ 18

Assembly

Language Programming:
The String Instruction

Reference: Yu . Marut
Chapter 11

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
The Direction Flag

The direction flag (DF) is a control flag.


The control flags are used to control the
processor’s operations.
DF determines the direction in which
string operations will proceed.
These operation are implemented using
two index registers: SI and DI.
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
The Direction Flag
 String1 DB ‘ABC’
OFFSET Content ASCII character
0200H 041H A
0201H 042H B
0202H 043H C

 If DF=0, SI and DI proceed in the direction of


increasing memory addresses: from left to right
across the string. Conversely, if DF=1, SI and DI
will proceed in the direction of decreasing
memory addresses: from right to left.

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
CLD & STD Instruction
Clear Direction Flag (CLD)
To make DF=0, the CLD instruction is used
Syntax: CLD
Set Direction Flag (STD)
To make DF=1, the STD instruction is used
Syntax: STD
CLD and STD have no effect on the other
flags
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Moving a String
Syntax: MOVSB
Copies the contents of the byte
addressed by DS:SI to the byte
addressed by ES:DI. The contents of the
source byte are unchanged.
After the byte has been moved, both SI
and DI are automatically incremented if
DF=0, or decremented if DF=1.
Permits memory to memory operation
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Moving a String:
Example
String1 DB ‘HELLO$’
String2 DB 5 dup (?)
Before
After MOVSB
MOVSB
MOV AX, @DATA SI SI
MOV DS, AX String1 ‘H’ ‘E’ ‘L’ ‘L’ ‘O’
MOV ES, AX DI DI
LEA SI, String1 String2 ‘H’
LEA DI, String2
CLD
MOVSB

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
The REP Prefix
MOVSB moves only a single byte from
the source string to the destination
string. To move the entire string, first
initialize CX to the number N of bytes in
the source string and execute
Syntax: REP MOVSB
The REP prefix causes MOVSB to be
executed N times. After each MOVSB, CX
is decremented until it becomes 0.
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
MOVSW Instruction
Syntax: MOVSW; word form of MOVSB
Copies the contents of the word
addressed by DS:SI to the word
addressed by ES:DI. The contents of the
source word are unchanged.
After the word has been moved, both SI
and DI are automatically incremented by
2 if DF=0, or decremented by 2 if DF=1.
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Storing String
 Syntax: STOSB; store string byte
 Moves the contents of AL register to the
byte addressed by ES:DI. DI is incremented
if DF=0 or decremented if DF=1.
 Syntax: STOSW; store string word
 Moves the contents of AX register to the
word addressed by ES:DI. DI is incremented
by 2 if DF=0 or decremented if DF=1.

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Storing String
MOV AX, @DATA
MOV ES, AX Before STOVSB
After STOVSB
DI DI DI
LEA DI, string1 String1
String1 ‘H’
‘A’ ‘E’
‘E’
‘A’ ‘L’
‘L’ ‘L’
‘L’ ‘O’
‘O’

CLD
MOV AL, ‘A’
STOSB
STOSB

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Load String
Syntax: LODSB; load string byte
Moves the byte addressed by DS:SI into
AL. SI is incremented if DF=0 or
decremented if DF=1.
Syntax: LODSW; load string word
Moves the word addressed by DS:SI into
AX. SI is increased by 2 if DF=0 or
decreased by 2 if DF=1.
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Load String

String1 DB ‘HELLO$’
MOV AX, @DATA
MOV DS, AX
LEA SI, String1
CLD Before LODSB
After LODSB
SI
LODSB String1
SI SI
String1 ‘H’
‘H’ ‘E’
‘E’ ‘L’
‘L’ ‘L’
‘L’ ‘O’
‘O’ AL=‘H’
AL=‘E’
LODSB
Md. Shafiqul Islam
EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Scan String
 Syntax: SCASB; scan string byte
 Can be used to examine a string for a target
byte. The target byte is contained in AL.
 SCASB subtracts the string byte pointed to by
ES:DI from the contents of AL and uses the result
to set the flags. DI is incremented if DF=0 or
decremented if DF=1.
 Syntax: SCASW; scan string word
 The target word is in AX.
 All the flags are affected by SCASB and SCASW

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Scan String

String1 DB ‘HELLO$’
MOV AX, @DATA
MOV ES, AX
LEA DI, String1
CLD
MOV AL, ‘E’ Before SCASB
After SCASB
SCASB DI DI DI ZF=0
SCASB String1
String1 ‘H’
‘H’ ‘E’
‘E’ ‘L’
‘L’ ‘L’
‘L’ ‘O’
‘O’ ZF=1

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Scan String
If CX is initialized to the number of bytes
in the string
Syntax: REPNE SCASB ; Repeat SCASB
while not equal to target
Repeatedly subtract each string byte
from AL, update DI and decrements CX
until the target is found or CX=0.

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Compare String
Syntax: CMPSB; compare string bytes
Subtracts the byte with address ES:DI
from the byte with address DS:SI and sets
the flags. Then both SI and DI are
incremented or decremented depending
on DF.
Syntax: CMPSW; compare string words

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
String1 DB ‘ACD’
String2 DB ‘ABC’
MOV AX, DATA Before CMPSB
MOV DS, AX SI
After CMPSB

MOV ES, AX String1 A’


SI
‘C’
SI
‘D’
ZF=1
ZF=0

LEA SI, String1 String1


DI
A’ ‘C’ ‘D’

LEA DI, String2 String2 ‘A’


DI
‘B’
DI
‘C’
String2 ‘A’ ‘B’ ‘C’
CLD
CMPSB
CMPSB

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET
Thank You

Md. Shafiqul Islam


EEE 315: MICROPROCESSOR & INTERFACING
10/9/2024 Lecturer, Dept of EEE, BUET

You might also like