Professional Documents
Culture Documents
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.
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:
MOVSB
MOVSB
Before MOVSB SI
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
CLD
MOVSW
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
DI is incremented if DF = 0 or decremented if DF = 1
STOSW
MOV ES, AX
CLD
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
SI is incremented if DF = 0 or decremented if DF = 1
LODSW
SI is increased by 2 if DF = 0 or decreased by 2 if DF = 1
STRING1 DB ‘ABC’
Code
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
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