0% found this document useful (0 votes)
343 views25 pages

Computer Organization & Assembly Language: The String Instructions

This document discusses string instructions in assembly language. It covers the direction flag (DF), which determines the direction of string operations using the SI and DI registers. It also covers instructions for moving, comparing, scanning, storing, and loading strings, such as MOVSB, CMPSB, SCASB, STOSB, and LODSB. The REP and CLD/STD instructions are also discussed for their effects on string operations. Examples are provided to demonstrate the use of various string instructions. [/SUMMARY]

Uploaded by

Abdul Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views25 pages

Computer Organization & Assembly Language: The String Instructions

This document discusses string instructions in assembly language. It covers the direction flag (DF), which determines the direction of string operations using the SI and DI registers. It also covers instructions for moving, comparing, scanning, storing, and loading strings, such as MOVSB, CMPSB, SCASB, STOSB, and LODSB. The REP and CLD/STD instructions are also discussed for their effects on string operations. Examples are provided to demonstrate the use of various string instructions. [/SUMMARY]

Uploaded by

Abdul Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Computer Organization &

Assembly Language
The String Instructions
The Direction Flag

 The control flags are used to control the processor


operations
 Direction Flag (DF)
 Purpose is to determine in which string operations will
determined
 These operations are implemented by the two index
registers SI and DI

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

 To clear DF = 0 use CLD instruction


 To set DF = 1, use STD instruction

CLD and STD have no effect on the other flags

Value of the Direction Flag Effect on SI and DI Address Sequence

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

 Equivalent Instruction of MOVSW


MOV AX, DS: [SI]
MOV ES: [DI], AX
 Equivalent Instruction of MOVSB
MOV AL, DS: [SI]
MOV ES: [DI], AL
Store String
 STOSB
 Move the contents of AL register to the byte addressed by ES:DI,
DI is incremented if DF =0 or decremented if DF = 1
 STOSW
 Move the contents of AX register to the word addressed by
ES:DI, DI is increased by 2 if DF =0
 STOSB and STOSW have no effect on the flags
 Equivalent Instruction of STOSW

MOV ES: [DI], AX


 Equivalent Instruction of STOSB

MOV ES: [DI], AL

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

MOV AX, DS: [SI]


 Equivalent Instruction of LODSB

MOV AL,DS: [SI]

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)

1. MOVSB [0200]=10h SI=101h, DI=201h

2. MOVSW [0200]=1015h SI=102h, DI=202h

3. STOSB [0200]= 42h DI=201h

4.  STOSW [0200]=4142h DI=202h

5.  LODSB AL=10h SI=101h

6.  LODSW AX=1510h SI=102h

23
Table 1. String Primitive Instructions, Implied Operands.

Instruction Description Implied Operands

Move String Data: Copy a memory


MOVS byte, word, or doubleword from one MOVS [DI],[SI]
memory location to another.

Compare String: Compare a memory


CMPS byte, word, or doubleword to CMPS [SI],[DI]
memory.

Scan String: Compare AL, AX, or SCAS [DI],AL


SCAS EAX to the contents of memory, SCAS [DI],AX
affecting the Flags register. SCAS [DI],EAX

Store String Data: Store AL, AX, or STOS [DI],AL


STOS STOS [DI],AX
EAX into memory.
STOS [DI],EAX

Load Accumulator from String: LODS AL,[SI]


LODS Load a byte, word, or doubleword LODS AX,[SI]
into AL, AX, or EAX from memory. LODS EAX,[SI]
Table 2. Overview of String Primitive Instructions.

Increment Value
General Specific Size Description for SI and DI

MOVS MOVSB Move (copy) byte 1

MOVSW Move (copy) word 2

MOVSD Move (copy) doubleword* 4

CMPS CMPSB Compare bytes 1

CMPSW Compare words 2

CMPSD Compare doublewords* 4

SCAS SCASB Scan byte 1

SCASW Scan word 2

SCASD Scan doubleword* 4

STOS STOSB Store byte 1

STOSW Store word 2

STOSD Store doubleword* 4

LODS LODSB Load byte 1

LODSW Load word 2

LODSD Load doubleword* 4

You might also like