Open In App

String manipulation instructions in 8086 microprocessor

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this Article, We will be going through the String Manipulation Instruction, We will start our Article with the Introduction of the String Manipulation Instruction in the 8086, Then We will go through the different String Manipulation Instructions with some Examples, and At last, we will conclude our Article with Some FAQs.

What are String Manipulation Instructions in 8086 microprocessors?

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:

OPCODEOPERANDEXPLANATIONEXAMPLE
REPinstructionrepeat the given instruction till CX != 0REP MOVSB
REPEinstructionrepeat the given instruction while CX = 0REPE
REPZinstructionrepeat the given instruction while ZF = 1REPZ
REPNEinstructionrepeat the given instruction while CX != 0REPNE
REPNZinstructionrepeat the given instruction while ZF = 0REPNZ
MOVSBnonemoves contents of byte given by DS:SI into ES:DIMOVSB
MOVSWnonemoves contents of word given by DS:SI into ES:DIMOVSW
MOVDnonemoves contents of double word given by DS:SI into ES:DIMOVD
LODSBnonemoves the byte at address DS:SI into AL; SI is incr/decr by 1LODSB
LODSWnonemoves the word at address DS: SI into AX; SI is incr/decr by 2LODSW
LODSDnonemoves the double word at address DS:SI into EAX; SI is incr/decr by 4LODSD
STOSBnonemoves contents of AL to byte address given by ES:DI; DI is incr/dec by 1STOSB
STOSWnonemoves the contents of AX to the word address given by ES:DI; DI is incr/decr by 2STOSW
STOSDnonemoves contents of EAX to the DOUBLE WORD address given by ES:DI; DI is incr/decr by 4STOSD
SCASBnonecompares byte at ES:DI with AL and sets flags according to resultSCASB
SCASWnonecompares word at ES:DI with AX and sets flagsSCASW
SCASDnonecompares double word at ES:DI with EAX and sets flagsSCASD
CMPSBnonecompares byte at ES:DI with byte at DS:SI and sets flagsCMPSB
CMPSWnonecompares word at ES:DI with word at DS:SI and sets flagsCMPSW
CMPSDnonecompares double word at ES:DI with double word at DS:SI and sets flagsCMPSD

Examples of String manipulation instructions in 8086 microprocessor

Given below are some Examples of the String manipulation instructions in 8086 microprocessor

Example of REP with MOVSB Instruction

MOV AX, 7000H    Assign source segment address to AX
MOV DS, AX Load source segment address into DS
MOV AX, 8000H Assign destination segment address to AX
MOV CX, 0E0H Move the length of the string to the counter register CX
MOV SI, 3000H Assign source index address to SI
MOV DI, 4000H Assign destination index address to DI
CLD Ensure auto-increment mode is set by clearing the direction flag
REP MOVSB Repeat the move byte from source to destination instruction CX times

Example of REPE With CMPSB Instruction

MOV AX, SEG_STRING1         Move the segment address of STRING1 to AX
MOV DS, AX Load it to DS
MOV AX, SEG_STRING2 Move the segment address of STRING2 to AX
MOV ES, AX Load it to ES
MOV SI, OFFSET_STRING1 Move the offset of STRING1 to SI
MOV DI, OFFSET_STRING2 Move the offset of STRING2 to DI
MOV CX, 020H Move the length of the strings to CX
CLD Clear the direction flag, set auto-increment mode
REPE CMPSB Compare the 020H bytes of STRING1 and STRING2,While they are Equal,
If mismatch is found modify the flags and proceed with further Execution

NOTE: If Both Strings are Equal,CX becomes ZERO,the ZF is set otherwise ZF is reset

Example of REPNE With SCASW Instruction

MOV AX, SEGMENT_STR      Move the segment address of the String to AX
MOV ES, AX Load it to ES
MOV DI, OFFSET_STR Move the offset of String to DI
MOV CX, 020H Move the length of the String to CX
MOV AL, WORD_TO_FIND The word to be scanned for is loaded into AL
CLD Clear the direction flag
REPNE SCASW Scan the 020H words of the String until a match to the word is found

Example of LODSB Instruction

MOV CX, 10                      Set CX to the number of bytes to be read
MOV SI, OFFSET_STR Set SI to point to the start of the string
MOV DI, OFFSET_BUF Set DI to point to the destination buffer
CLD Clear the direction flag for auto-increment

READ_LOOP:
LODSB Load a byte from the memory location pointed to by SI into AL, and increment SI
STOSB Store the byte in AL at the memory location pointed to by DI, and increment DI
LOOP READ_LOOP Decrement CX and loop back if CX is not zero
HLT Halt the processor (assembly language instruction for stopping execution)

Conclusion

In this Article, We have gone through the String manipulation in the 8086 microprocessor which provides efficiently handling operations on strings of data stored in the memory. These instructions enable us to copying, searching and comparing Function in the memory. Also we have gone through the different examples of the Instructions which provides us the clear understanding of the String manipulation instructions set.


Similar Reads