You are on page 1of 24

The String Instruction

Direction Flag
Its purpose is to determine the direction in which string
operations will proceed.
These operations are implemented by the two index registers SI
and DI.
Suppose : STRING1 DB ‘AB’
Offset address content ASCII char
 0200h 041h A
 0201h 042h B
If DF =0, SI and DI proceed in the direction of increasing
memory location.
If DF =1, SI and DI proceed in the direction of decreasing
memory location.
CLD and STD
To make DF =0 use the CLD instruction:
CLD ; clear direction flag
To make DF =1 use the STD instruction:
STD ; set direction flag
CLD and STD have no effect on the other flags.
Using the DUP Operator
Use DUP (duplicate) to allocate (create space for) an
array or string.

var1 db 20 DUP(0) ; 20 bytes, all equal to zero


var2 db 20 DUP(?) ; 20 bytes, uninitialized
Moving a String
In order to move a defined string, we use two instructions in

assembly.
MOVSB

MOVSW
MOVSB
 The MOVSB instruction

 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
MOVSB
Suppose we have defined two strings as follows:
 .DATA

 STRING1 DB ‘HELLO’
 STRING2 DB 5 DUP (?)
 To move the first two bytes of string1 to string2, we execute the following instructions:

 MOV AX, @DATA


 MOV DS, AX
 MOV ES, AX
 LEA SI, STRING1
 LEA DI, STRING2
 CLD ; clear DF
 MOVSB

 MOVSB
MOVSB
 Before MOVSB SI

 STRING1 ‘H’ ‘E’ ‘L’ ‘L’ ‘O’


DI
 STRING2

 After MOVSB
SI
 STRING1 ‘H’ ‘E’ ‘L’ ‘L’ ‘O’
 STRING2 DI
‘H’
 After MOVSB
SI
 STRING1 ‘H’ ‘E’ ‘L’ ‘L’ ‘O’
 STRING2
DI
‘H’ ‘E’
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


 REP MOVSB
 The REP prefix causes MOVSB to be executed N times

 After each MOVSB, CX is decremented until it becomes 0

 To copy STRING1 to STRING2, we execute

 CLD

 LEA SI, STRING1


 LEA DI, STRING2
 MOV CX, 5 ; no of characters in STRING1
 REP MOVSB
MOVSW
 The MOVSW instruction

 MOVSW

Moves a word from the source string to the destination string


 It expects DS:SI to point to a source string word and ES:DI to point to a

destination string word


 After a string word has been moved, both SI and DI are automatically

increased by 2 if DF = 0 or decreased by 2 if DF = 1
 MOVSB and MOVSW have no effect on the flags
Store String
 In order to store a string, we use two instructions in assembly.

 STOSB

 Moves the contents of the AL register to the byte addressed by ES:DI.

 DI is incremented if DF = 0 or decremented if DF = 1

 STOSW

 Moves the contents of the AX register to the byte addressed by ES:DI

and updates DI by 2, according to the direction flag setting


 STOSB and STOSW have no effect on the flags.
Store String
 Example

 Storing two “A”s in STRING1


 MOV AX, @DATA

MOV ES, AX

LEA DI, STRING1

CLD

MOV AL, ‘A’

STOSB

STOSB
Store String
Before STOSB DI
 STRING1
‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘A’
AL
After STOSB DI
 STRING1 ‘A’ ‘E’ ‘L’ ‘L’ ‘O’ ‘A’

AL
After STOSB DI
 STRING1 ‘A’ ‘A’ ‘L’ ‘L’ ‘O’ ‘A’

AL
Reading and storing a character String
 Read and store characters in a string, until a carriage return is typed
 If the user makes a typing mistake and hits the backspace key, the previous character is
removed from the string
 Algorithm
 char_read = 0

Read a char
WHILE char is not a carriage return DO
IF char is a backspace
THEN
char_read = char_read – 1
Remove previous char from string
ELSE
Store char in string
Char_read = char_read + 1
END_IF
Read a char
END_WHILE
Reading and storing a character String
 READ_STR PROC NEAR
; BX number of characters read ELSE1:
CLD STOSB
XOR BX, BX INC BX
MOV AH, 1 READ:
INT 21h INT 21h
WHILE1:
JMP WHILE1
CMP AL, 0DH
END_WHILE1:
JE END_WHILE1
RET
CMP AL, 8H ; backspace?
READ_STR ENDP
JNE ELSE1 ; no, store in string
DEC DI ; yes, mov str ptr
DEC BX ; back
JMP READ ;read again
Load String
 In order to load a string, we use two instructions in assembly.

 LODSB

 Moves the byte addressed by DS:SI into AL

 SI is incremented if DF = 0 or decremented if DF = 1

 LODSW

 Moves the word addressed by DS:SI into AX

 SI is increased by 2 if DF = 0 or decreased by 2 if DF = 1

 LODSB and LODSW have no effect on the flags.


Load String
 Example

 Load the first and second bytes of STRING1

STRING1 DB ‘ABC’
 Code

 MOV AX, @DATA


 MOV DS, AX
 LEA SI, STRING1
 CLD

 LODSB

 LODSB
Load String
Before LODSB SI
 STRING1
‘A’ ‘B’ ‘C’
AL
After LODSB SI
 STRING1 ‘A’ ‘B’ ‘C’ ‘A’

AL
After LODSB SI
 STRING1 ‘A’ ‘B’ ‘C’ ‘B’

AL
Displaying a character string
 Algorithm

 FOR count times DO

Load a string character into AL

Move it to DL

Output character

 END_FOR
Displaying a character string
 DISP_STR PROC
PUSH AX
PUSH BX P_EXIT:
PUSH CX
POP SI
PUSH DX
PUSH DI POP DX
MOV CX, BX POP CX
JCXZ P_EXIT
POP BX
CLD
MOV AH,2 POP AX
TOP: RET
LODSB DISP_STR ENDP
MOV DL, AL
INT 21h
LOOP TOP
Scan String
 In order to scan a string, we use two instructions in assembly.

 SCASB
 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
 The result is not stored
 Afterward, DI is incremented if DF = 0 or decremented if DF = 1

 SCASW
 Can be used to examine a string for a target word
 The target byte is contained in AX
 SCASB subtracts the word pointed to by ES:DI from the contents of AX and uses the result
to set the flags
 The result is not stored
 Afterward, DI is increased by 2 if DF = 0 or decreased by 2 if DF = 1

 All the flags are affected by SCASB and SCASW


Scan String
 Example
 STRING1 DB ‘ABC’
Scan the first two bytes of STRING1 for “B”
 MOV AX, @DATA
 MOV ES, AX
 CLD
 LEA DI, STRING1
 MOV AL, ‘B’
 SCASB
 SCASB
Scan String
Before SCASB DI
 STRING1
‘A’ ‘B’ ‘C’ ‘B’
AL
After SCASB DI
 STRING1 ‘A’ ‘B’ ‘C’ ZF = 0 ‘B’
(not found)
AL
After SCASB DI
 STRING1 ‘A’ ‘B’ ‘C’ ZF = 1 ‘B’
(found)
AL
Thank You

You might also like