String Related Instructions
A string is nothing but a collection of ascii Characters codes. Many a time it require to perform some operation on strings such as copy a string, compare strings, concatenation of strings etc. 8086 has a powerful set of string related instructions and are DIscussed below:
-
REP GROUP:
Instructions | Description | Example | FLAG affected |
REP | It is used as a prefix instruction. Repeat until CX=0, REP decrements CX in each loop | ||
REPE / REPZ | Repeat the instruction until CX=0 or ZF is not equal to 1. REPE prefix will cause string instruction to be repeated, as long as compared bytes or words are equal or CX is not zero. GenerALly used with CMPS and SCAN |
||
REPNE / REPNZ | Repeat the following instruction until the compared string are not equal, and CX is not zero or ZF is not 1. |
-
MOV Group:
Instruction | Description | Example | |
MOVS/ MOVSB / MOVSW | These instructions MOVe a byte or word from a string to another. These instruction assume that source string is pointed by SI is in data segment and the destination string pointed by DI is in extra segment. CX contains count of bytes or words. These instructions are used with REP, REPE or REPNE prefixes. | – – – – – – MOV CX, no-of-bytes LEA SI, string1 LEA DI, string2 REP MOVSB – – – – – – ;here REP MOVSB decrements CX and does MOVSB until CX = 0 |
-
CMP group
Instructions | Description | Example | FLAG affected |
COMPS/ CMPSB / CMPSW | These instructions compare two string byte or two string word with another string. CX contains count of bytes or words. These instruction assume that source string is pointed by SI is in data segment and the destination string pointed by DI is in extra segment. These instructions are used with REP, REPE or REPNE prefixes. | – – – – – – MOV CX, no-of-bytes LEA SI, string1 LEA DI, string2 REPE CMPSB ; here REPE CMPSB repeat the comparison of string bytes until the end of string (CX=0) or until the compared bytes are not equal |
AF, CF, OF, PF, SF, ZF |
-
Scan group
Instructions | Description | Example | FLAG affected |
SCAS/ SCASB / SCASW | These instructions compare (scan) a string byte with a byte in AL or a string word in AX or the operand in the string. The string to be scanned must be extra segment whose offset should be in DI register. CX contains count of bytes or words to be scanned. These instructions are used with REP, REPE or REPNE prefixes. Scanning is repeated while bytes are not equal and it is not the end of the string i.e. CX is not zero | – – – MOV CX, no-of-bytes MOV AL, ‘A’ LEA DI, string1 REPNE SCASB string1 – – – ;By default SCAS uses name of string. REPNE SCASB string1 scans the string in extra segment pointed by DI, with a byte in AL register. If string is not found then ZF=0 and CX=0. If string is found, ZF=1 and DI will point to next byte after the found CHar/byte. |
AF, CF, OF, PF, SF AND ZF |
-
LODS group
Instructions | Description | Example | FLAG affected |
LODS/ LODSB / LODSW | It loads the string from data segment pointed by SI is in AL or a string word in AX or the operand in the string. The SI is automatically adjusted after each byte/word loaDIng. | – – – LEA SI, src-string1 LODS src-string1 – – – |
None |
-
STOS group
Instructions | Description | Example | FLAG affected |
STOS / STOSB / STOSW | It stores a byte from AL or word from AX into the string in extra segment pointed by DI. DI is automatically adjusted by one or two after each storing a byte or word. |
– – – LEA DI, dst-string MOV AL, ‘X’ STOSB dst-string1 – – – |
None |