String Manipulation Instructions in the 8086 microprocessor architecture are the set of Instructions that are used to manipulate strings in memory. The String manipulation Instructions offer different functionality such as copying, Searching, and Modifying Strings of data. Key String manipulation instruction in 8086 instruction sets includes different instructions such as MOVSB, CMPSB, SCASB, LODSB, STOSB, and other instructions which are going to be discussed further.
Different String Manipulation Instructions
The string is a series of data bytes or words available in memory at consecutive locations. It is either referred as byte string or a word string. Their memory is always allocated in a sequential order. Instructions used to manipulate strings are called string manipulation instructions. Following is the table showing the list of string manipulation instructions:
OPCODE | OPERAND | EXPLANATION | EXAMPLE |
REP | instruction | repeat the given instruction till CX != 0 | REP MOVSB |
REPE | instruction | repeat the given instruction while CX = 0 | REPE |
REPZ | instruction | repeat the given instruction while ZF = 1 | REPZ |
REPNE | instruction | repeat the given instruction while CX != 0 | REPNE |
REPNZ | instruction | repeat the given instruction while ZF = 0 | REPNZ |
MOVSB | none | moves contents of byte given by DS:SI into ES:DI | MOVSB |
MOVSW | none | moves contents of word given by DS:SI into ES:DI | MOVSW |
MOVD | none | moves contents of double word given by DS:SI into ES:DI | MOVD |
LODSB | none | moves the byte at address DS:SI into AL; SI is incr/decr by 1 | LODSB |
LODSW | none | moves the word at address DS: SI into AX; SI is incr/decr by 2 | LODSW |
LODSD | none | moves the double word at address DS:SI into EAX; SI is incr/decr by 4 | LODSD |
STOSB | none | moves contents of AL to byte address given by ES:DI; DI is incr/dec by 1 | STOSB |
STOSW | none | moves the contents of AX to the word address given by ES:DI; DI is incr/decr by 2 | STOSW |
STOSD | none | moves contents of EAX to the DOUBLE WORD address given by ES:DI; DI is incr/decr by 4 | STOSD |
SCASB | none | compares byte at ES:DI with AL and sets flags according to result | SCASB |
SCASW | none | compares word at ES:DI with AX and sets flags | SCASW |
SCASD | none | compares double word at ES:DI with EAX and sets flags | SCASD |
CMPSB | none | compares byte at ES:DI with byte at DS:SI and sets flags | CMPSB |
CMPSW | none | compares word at ES:DI with word at DS:SI and sets flags | CMPSW |
CMPSD | none | compares double word at ES:DI with double word at DS:SI and sets flags | CMPSD |
CMPS (Compare String): Compares source and
destination strings byte by byte or word by word.
CMPSB ; Compare byte
from DS:SI with ES:DI
CMPSW ; Compare word
from DS:SI with ES:DI
MOV CX, 5 ;
Number of elements to compare
LEA SI, STR1 ;
Load first string address
LEA DI, STR2 ;
Load second string address
REPE CMPSB ;
Compare strings while equal
JNE NOT_EQUAL ; If
not equal, jump to NOT_EQUAL
3. Scan Instructions
- SCAS
(Scan String): Searches for a byte or word in a string, comparing AL
or AX with the string element at ES:DI.
SCASB ; Scan byte in
string
SCASW ; Scan word in
string
- Example:
MOV AL, 'A' ;
Search for character 'A'
MOV CX, 10 ; Set
length of the string
LEA DI, STR ;
Load string address
REPE SCASB ;
Scan string for 'A'
JZ FOUND ; If
found, jump to FOUND
4. Store Instructions
- STOS
(Store String): Stores the AL or AX value into the
destination string (ES:DI).
STOSB ; Store byte
at ES:DI
STOSW ; Store word
at ES:DI
- Example:
MOV AL, 0xFF ; Set
value to store
MOV CX, 10 ; Set
length of the string
LEA DI, DEST ;
Load destination address
REP STOSB ;
Store AL to CX bytes
5. Load Instructions
- LODS
(Load String): Loads a byte or word from the source string (DS:SI)
into AL or AX.
LODSB ; Load byte
from DS:SI into AL
LODSW ; Load word
from DS:SI into AX
- Example:
MOV CX, 10 ;
Number of elements to load
LEA SI, SRC ;
Load source address
REP LODSB ;
Load bytes into AL
Repeating Prefixes
- REP:
Repeat until CX = 0.
- REPE/REPZ:
Repeat while equal (ZF = 1).
- REPNE/REPNZ:
Repeat while not equal (ZF = 0).
MOV CX, 10 ; Set
count
REP MOVSB ;
Repeat move byte CX times
REPE CMPSB ;
Repeat compare while equal
REPNE SCASB ;
Repeat scan while not equal
Direction Flag (DF)
- CLD
(Clear Direction Flag): Process strings forward (SI/DI
increment).
CLD ;
Increment SI and DI
- STD
(Set Direction Flag): Process strings backward (SI/DI
decrement).
STD ;
Decrement SI and DI
Example: Copying a String
MOV CX, 5 ; Set
number of bytes to copy
LEA SI, SRC ;
Load source address
LEA DI, DEST ;
Load destination address
CLD ;
Clear direction flag (forward)
REP MOVSB ;
Copy CX bytes from DS:SI to ES:DI
Example: Searching for a Character
MOV AL, 'X' ;
Character to search
MOV CX, 20 ;
String length
LEA DI, STRING ;
Load string address
CLD ;
Forward search
REPNE SCASB ;
Search for AL in the string
JZ FOUND ; If
found, jump to FOUND
MOV AX, -1 ; Not
found, return -1
JMP END
FOUND:
MOV AX, CX ;
Return position of found character
Key Points:
- String
instructions simplify handling of arrays and memory blocks.
- Use
of index registers (SI/DI) and counter (CX) automates
repetition.
- The
direction flag (DF) controls whether strings are processed forwards
or backwards.
- Repeating
prefixes (REP, REPE, REPNE) enable automated
loops.
No comments:
Post a Comment