Computer Organization & Assembly Language: The String Instructions
Computer Organization & Assembly Language: The String Instructions
Assembly Language
The String Instructions
The Direction Flag
2
Example
STRING1 DB ‘ABCDE’
String is stored in memory storing at offset 0200h
Offset Address Content ASCII Character
0200h 041h A
0201h 042h B
0202h 043h C
0203h 044h D
0204h 045h E
If DF = 0, SI and DI proceed in direction of increasing memory
addressed, from left to right across the string.
If DF = 1, SI and DI proceed in direction of decreasing memory
addressed
3
CLD and STD
0 Incremented Low-high
1 Decremented High-low
4
Moving a String
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.
Both SI and DI is incremented if DF =0 or decremented if DF = 1
MOVSW
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.
Both SI and DI is increased by 2 if DF =0 or decreased by 2 if DF = 1
MOVSB and MOVSW have no effect on the flags
5
MOVS (Move String Data).
Value to be automatically
Instruction
added/subtracted from SI and DI
MOVSB 1
MOVSW 2
MOVSD 4
Example
.DATA
STRING1 DB ‘HELLO’
STRING2 DB 5 DUP (?)
.CODE
MOV AX , @DATA
MOV DS , AX
MOV ES , AX
LEA SI , STRING1 ; SI points to source string
LEA DI , STRING2 ; DI points to destination string
CLD ; Clear DF
MOVSB; Move first byte
MOVSB; Move second byte
RET
7
The REP prefix
.CODE
MOV AX , @DATA
MOV DS , AX
MOV ES , AX
LEA SI , STRING1
LEA DI , STRING2
CLD
MOV CX , 5
REP MOVSB
RET
8
Example
Copy the contents of a string to other string in reverse
order
.CODE
MOV AX , @DATA
MOV DS , AX
MOV ES , AX
LEA SI , STRING1+4
LEA DI , STRING2
STD
MOV CX , 5
MOVE:
MOVSB
ADD DI , 2
LOOP MOVE
RET
9
Example (MOVSW)
.DATA
ARR DW 10,20,40,50,60,?
.CODE
STD
LEA SI,ARR+8H ;SI points to 60h
LEA DI,ARR+0AH ;DI points to ?
MOV CX,3 ; 3 elements to move
REP MOVSW ; move 40, 50, 60
MOV [DI],30H ; insert 30
RET
10
MOVS (Move String Data)
Value to be automatically
Instruction
added/subtracted from SI and DI
MOVSB 1
MOVSW 2
MOVSD 4
12
Example (STOSB)
.DATA
STRING1 DB 'HELLO'
.CODE
MOV AX,@DATA
MOV ES,AX
LEA DI,STRING1
CLD
MOV AL,'A‘ ;AL has character to store
STOSB ;store an ‘A’ in STRING1
STOSB ;store another ‘A’ in STRING1
RET
13
Reading And Storing A Character String
chars_read = 0
read a char
WHILE char is not a carriage return DO
IF char is backspace THEN
chars_read = chars_read – 1
remove previous char from string
ELSE
store char in string
chars_read = chars_read + 1
END_IF
read a char
END_WHILE
14
Reading And Storing A Character String
.DATA CMP AL,8H
STRING1 DB 50 DUP (?) JNE ELSE1
.CODE
DEC DI
MOV AX,@DATA
MOV ES,AX DEC BX
CLD JMP READ
XOR BX,BX ELSE1:
LEA DI,STRING1 STOSB
MOV AH,1
INC BX
INT 21H
WHILE1:
READ:
CMP AL,0DH INT 21H
JE END_WHILE JMP WHILE1
END_WHILE:
RET
15
Load String
LODSB
Move the byte addressed by DS:SI into AL, SI is incremented if
DF =0 or decremented if DF = 1
LODSW
Move 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
Equivalent Instruction of LODSW
16
Example
.DATA
STRING1 DB 'ABC'
.CODE
MOV AX,@DATA
MOV DS,AX
LEA SI,STRING1
CLD
LODSB ;load first byte into AL
LODSB ;load second byte into AL
RET
17
Scan String
Use to examine a string for a target byte/word
SCASB
Target byte is contained in AL
Subtract 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
SCASW
Target word is contained in AX
Subtract the string word addressed by ES:DI from AX and set the
flags.
DI is increased by 2 if DF =0 or decreased by 2 if DF = 1
All status flags are affected by SCASB and SCASW
18
Example
.DATA
STRING1 DB 'ABC'
.CODE
MOV AX,@DATA
MOV ES,AX
LEA DI,STRING1
CLD
MOV AL,'B‘ ;target character
SCASB ;scan first byte
SCASB ;scan second byte
RET
19
Compare String
CMPSB
Subtracts the byte with address ES:DI, from the byte addressed by DS:SI.
The result is not stored
Both SI and DI is incremented if DF =0 or decremented if DF = 1
CMPSW
Subtracts the word with address ES:DI, from the word addressed by DS:SI
Both SI and DI is increased by 2 if DF =0 or decreased by 2 if DF = 1
All status flags are affected by CMPSB and CMPSW
20
Example
.DATA
STRING1 DB 'ACD'
STRING2 DB 'ABC'
.CODE
MOV AX,@DATA
MOV DS,AX
MOV ES,AX
CLD ;left to right processing
LEA SI,STRING1
LEA DI,STRING2
CMPSB ;compares first byte
CMPSB ;compares second byte
RET
21
Chapter # 11, Question # 1
Suppose
SI contains 100h Byte 100h contains 10h
DI contains 200h Byte 101h contains 15h
AX contains 4142h Byte 200h contains 20h
DF = 0 Byte 201h contains 25h
Give the source, destination, and the value moved for each of the following
instructions. Also give the new contents of SI and DI.
1. MOVSB
2. MOVSW
3. STOSB
4. STOSW
5. LODSB
6. LODSW
22
Solution
No. Instructions Values (Contents in hex, of the source or Values of SI or DI which modified
destination which modified)
23
Table 1. String Primitive Instructions, Implied Operands.
Increment Value
General Specific Size Description for SI and DI