Date post: | 29-May-2018 |
Category: |
Documents |
Upload: | dumbterminal |
View: | 217 times |
Download: | 0 times |
of 24
8/8/2019 CSE 204 Lec-9
1/24
The String Instruction
8/8/2019 CSE 204 Lec-9
2/24
Direction Flag
y Its purpose is to determine the direction in which stringoperations will proceed.
y These operations are implemented by the two index registers SI
and DI.y Suppose : STRING1 DB AB
y Offset address content ASCII char
y 0200h 041h A
y 0201h 042h B
y If DF =0, SI and DI proceed in the direction of increasingmemory location.
y If DF =1, SI and DI proceed in the direction of decreasingmemory location.
8/8/2019 CSE 204 Lec-9
3/24
CLD and STDy To make DF =0 use the CLD instruction:
y CLD ; clear direction flag
y To make DF =1 use the STD instruction:y STD ; set direction flag
y CLD and STD have no effect on the other flags.
8/8/2019 CSE 204 Lec-9
4/24
Using the DUP Operatory 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
8/8/2019 CSE 204 Lec-9
5/24
Moving a String
y In order to move a defined string, we use two instructions
in assembly.
y MOVSB
y MOVSW
8/8/2019 CSE 204 Lec-9
6/24
MOVSB
y The MOVSB instruction
y MOVSB
copies the contents of the byte addressed by DS:SI, to the byteaddressed by ES:DI
y The contents of the source byte are unchanged
y After the byte has been moved, both SI and DI are automatically
incremented if DF = 0 or decremented if DF = 1
8/8/2019 CSE 204 Lec-9
7/24
Suppose we have defined two strings as follows:
y
.DATA
y STRING1 DB HELLO
y STRING2 DB 5 DUP (?)
y To move the first two bytes of string1 to string2, we execute the following instructions:
y MOV AX, @DATA
y MOV DS, AX
y MOV ES, AX
y LEA SI, STRING1
y LEA DI, STRING2
y CLD ; clear DF
y MOVSB
y MOVSB
MOVSB
8/8/2019 CSE 204 Lec-9
8/24
y Before MOVSB
y STRING1
y STRING2
y After MOVSB
y STRING1
y STRING2
y After MOVSB
y STRING1
y STRING2
MOVSB
H E L L O
H
H E L L O
H E
H E L L O
SI
DI
SI
DI
SI
DI
8/8/2019 CSE 204 Lec-9
9/24
THE REPPrefixy MOVSB moves only a single byte from the source string to the destination
string
y To move the entire string, first initialize CX to the number N of bytes in the
source string and execute
y REP MOVSB
y The REP prefix causesMOVSB to be executed N times
y After eachMOVSB, CX is decremented until it becomes 0
y To copy STRING1 to STRING2, we execute
y CLD
y LEA SI, STRING1
y LEA DI, STRING2
y MOV CX, 5 ; no of characters in STRING1
y REP MOVSB
8/8/2019 CSE 204 Lec-9
10/24
MOVSW
y The MOVSW instruction
y MOVSW
Moves a word from the source string to the destination string
y It expects DS:SI to point to a source string word and ES:DI to point to a
destination string word
y 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
y MOVSB and MOVSW have no effect on the flags
8/8/2019 CSE 204 Lec-9
11/24
Store Stringy In order to store a string, we use two instructions in assembly.
y STOSB
y
Moves the contents of the AL
register to the byte addressed by
ES:DI.
y DI is incremented if DF = 0 or decremented if DF = 1
y STOSW
y 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
y STOSB and STOSW have no effect on the flags.
8/8/2019 CSE 204 Lec-9
12/24
y Example
y Storing two As in STRING1
y MOV AX,@DATA
MOV ES,AX
LEA DI, STRING1
CLD
MOV AL, A
STOSB
STOSB
Store String
8/8/2019 CSE 204 Lec-9
13/24
y Before STOSB
y STRING1
y
After STOSBy STRING1
y After STOSB
y STRING1
Store String
H E L L O
DI
A
A E L L O
DI
A
A
A
L L O
DI
A
AL
AL
AL
8/8/2019 CSE 204 Lec-9
14/24
Reading and storing a character Stringy Read and store characters in a string, until a carriage return is typed
y If the user makes a typing mistake and hits the backspace key, the previous
character is removed from the string
y Algorithm
y 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
ELSEStore char in string
Char_read = char_read + 1
END_IF
Read a char
END_WHILE
8/8/2019 CSE 204 Lec-9
15/24
y READ_STR PROC NEAR
; BX number of characters read
CLD
XOR BX, BX
MOV AH, 1
INT 21h
WHILE1:
CMP AL, 0DH
JE END_WHILE1
CMP AL, 8H ; backspace?
JNE ELSE1 ; no, store in stringDEC DI ; yes, mov str ptr
DEC BX ; back
JMP READ ;read again
Reading and storing a character String
ELSE1:
STOSB
INC BX
READ:INT 21h
JMP WHILE1
END_WHILE1:
RET
READ_STR ENDP
8/8/2019 CSE 204 Lec-9
16/24
Load String
y In order to load a string, we use two instructions in assembly.
y LODSB
y
Moves the byte addressed by DS:SI into AL
y SI is incremented if DF = 0 or decremented if DF = 1
y LODSW
y Moves the word addressed by DS:SI into AX
y SI is increased by 2 if DF = 0 or decreased by 2 if DF = 1
y LODSB and LODSW have no effect on the flags.
8/8/2019 CSE 204 Lec-9
17/24
y Example
y Load the first and second bytes ofSTRING1
STRING1 DB ABC
y Code
y MOV AX,@DATA
y MOV DS,AX
y LEA SI, STRING1
y CLD
y LODSB
y LODSB
Load String
8/8/2019 CSE 204 Lec-9
18/24
Load String
y Before LODSB
y STRING1
y
After LODSBy STRING1
y After LODSB
y STRING1
A B C
SI
A B C
SI
A
A
B C
SI
B
AL
AL
AL
8/8/2019 CSE 204 Lec-9
19/24
Displaying a character string
y Algorithm
y FOR count times DO
Load a string character into AL
Move it to DL
Output character
y END_FOR
8/8/2019 CSE 204 Lec-9
20/24
y DISP_STR PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH DIMOV CX, BX
JCXZ P_EXIT
CLD
MOV AH,2
TOP:
LODSB
MOV DL, AL
INT 21h
LOOP TOP
Displaying a character string
P_EXIT:
POP SI
POP DX
POP CX
POP BX
POP AX
RET
DISP_STR ENDP
8/8/2019 CSE 204 Lec-9
21/24
Scan String
y In order to scan a string, we use two instructions in assembly.
y SCASB
y Can be used to examine a string for a target byte
y The target byte is contained in AL
y SCASB subtracts the string byte pointed to by ES:DI from the contents of AL anduses the result to set the flags
y The result is not stored
y Afterward, DI is incremented if DF = 0 or decremented if DF = 1
y SCASW
y Can be used to examine a string for a target wordy The target byte is contained in AX
y SCASB subtracts the word pointed to by ES:DI from the contents of AX and usesthe result to set the flags
y The result is not stored
y Afterward, DI is increased by 2 if DF = 0 or decreased by 2 if DF = 1
y All the flags are affected by SCASB and SCASW
8/8/2019 CSE 204 Lec-9
22/24
8/8/2019 CSE 204 Lec-9
23/24
Scan Stringy Before SCASB
y STRING1
y
After SCASBy STRING1
y After SCASB
y STRING1
A B C
DI
B
A B C
DI
B
A
B C
DI
B
AL
AL
AL
ZF = 0
(not found)
ZF = 1
(found)
8/8/2019 CSE 204 Lec-9
24/24
Thank You