You are on page 1of 5

String instructions 8086 has instructions that work on a character string also.

The instruction mnemonics end with S. String Instructions

MOVS CMPS 2-operand instructions MOVS (MOVe String) instruction

STOS

LODS 1-operand instructions

SCAS

Used for copying a string byte or word at a time. MOVSB for Move String Byte at a time. MOVSW for Move String Word at a time. MOVSB and MOVSW are more common than MOVS. Flags are not affected by the execution of MOVS/MOVSB/MOVSW instruction. Instruction MOVSB MOVSW Source pointed by DS:SI DS:SI Destination pointed by ES:DI ES:DI If D = 0 incr. by 1 SI and DI incr. by 2 SI and DI If D = 1 decr. by 1 SI and DI decr. by 2 SI and DI

MOVSB is equivalent to (assuming D = 0) MOV AL, [SI] MOV ES:[DI], AL PUSHF INC SI INC DI POPF; Flags not affected

MOVSW is equivalent to (assuming D = 0) MOV AX, [SI] MOV ES:[DI], AX PUSHF ADD SI, 2 ADD DI, 2 POPF; Flags not affected

It is usual to have REP (for REPeat) prefix for MOVSB / MOVSW instruction. The following code moves a 6-character string, as CX has 06. Without REP prefix, six MOVSB instructions are needed. MOV CX, 06 REP MOVSB EXIT: MOV DL, BL

Use of REP prefix with MOVSB MOV CX, 06 REP MOVSB EXIT: Next instrn. The above 3 instructions are equivalent to the 10 instructions on the right (assuming D= 0) MOV CX, 06 JCXZ EXIT AGAIN: MOV AL, [SI] MOV ES:[DI], AL PUSHF INC SI INC DI POPF LOOP AGAIN EXIT: Next instrn.

CMPS (CoMPare Strings) Instruction CMPS Compares two strings (of equal size), say String1 and String2, a byte or word at a time. String values remain unaffected. Only flags affected. Basically it performs the subtraction DS:[SI] - ES:[DI] CMPSB for comparing Strings Byte at a time. CMPSW for comparing Strings Word at a time. CMPSB and CMPSW are more common than CMPS.

Instruction CMPSB CMPSW

String1 pointed by DS:SI DS:SI

String2 pointed by ES:DI ES:DI

If D = 0 incr. by 1 SI and DI incr. by 2 SI and DI

If D = 1 decr. by 1 SI and DI decr. by 2 SI and DI

CMPSB is equivalent to (assuming D = 0)

CMPSW is equivalent to (assuming D = 0)

MOV AL, [SI] MOV AX, [SI] CMP AL, ES:[DI] CMP AX, ES:[DI] PUSHF PUSHF INC SI ADD SI, 2 INC DI ADD DI, 2 POPF POPF POPF used to ensure that flags are not again affected because of INC instructions

Normally CMPSB or CMPSW instructions are used to check if two given strings are same or not. To check for equality, REPE (Repeat while Equal) prefix is used. REPE can also be written as REPZ (Repeat while Z flag is set) or REP (Repeat, while equal is implied)

Use of REPE prefix with CMPSB MOV CX, 08 REPE CMPSB EXIT: JE EQUAL The above 3 instructions are equivalent to the 10 instructions on the right (assuming D= 0). REPE CMPSB instruction causes CMPSB to be repeated as long as the compared bytes are equal and CX contents is not yet 0000. MOV CX, 08 JCXZ EXIT AGAIN: MOV AL, [SI] CMP AL, ES:[DI] PUSHF INC SI INC DI POPF LOOPE AGAIN EXIT: JE EQUAL

REPNE (Repeat while not Equal) prefix is also present REPNE can also be written as REPNZ (Repeat while Zero flag is not set). REPNE CMPSB causes CMPSB instruction to be repeated as long as the compared bytes are not equal and CX content is not yet 0000. REPNE prefix is not commonly used. STOS (STOre String) instruction STOS is used for creating a string in memory a byte or word at a time. AL/AX contents copied to memory pointed by ES:[DI]. It does not affect any flag. STOSB is used for storing string byte at a time. STOSW is used for storing string word at a time. STOSB and STOSW are more common than STOS. Flags are not affected by the execution of this instruction. Instruction STOSB STOSW Source AL AX Destination ES:DI ES:DI If D = 0 incr. DI by 1 incr. DI by 2 If D = 1 decr. DI by 1 decr. DI by 2

STOSB is equivalent to (assuming D = 0) MOV ES:[DI], AL PUSHF INC DI POPF; Flags not affected

STOSW is equivalent to (assuming D = 0) MOV ES:[DI], AX PUSHF ADD DI, 2 POPF; Flags not affected

Suppose we want to initialize 6 consecutive memory locations with the same value 25H. The REP prefix for STOSB proves useful.

Use of REP prefix with STOSB

MOV AL, 25H MOV CX, 06 REP STOSB EXIT: Next instrn. Above 4 instructions are equivalent to the 9 instructions on the right (assuming D = 0) LODS (LOaD String) instruction

MOV AL, 25H MOV CX, 06 JCXZ EXIT AGAIN: MOV ES:[DI], AL PUSHF INC DI POPF LOOP AGAIN EXIT: Next instrn.

LODS is used for processing a string in memory a byte or word at a time. It copies contents of memory pointed by DS:[SI] into AL or AX. It does not affect any flag. LODSB is used for loading string byte at a time. LODSW is used for loading string word at a time. LODSB and LODSW are more common than LODS. Instruction LODSB LODSW Source DS: [SI] DS:[SI] Destination AL AX If D = 0 incr. SI by 1 incr. SI by 2 If D = 1 decr. SI by 1 decr. SI by 2

LODSB is equivalent to (assuming D = 0) MOV AL, DS:[SI] PUSHF INC SI POPF; Flags not affected

LODSW is equivalent to (assuming D = 0) MOV AX, DS:[SI] PUSHF ADD SI, 2 POPF; Flags not affected

REP prefix for LODS has no practical value. However, no syntax error is generated by the assembler if REP prefix is used with LODSB or LODSW. SCAS (SCAn String) instruction SCAS is used for scanning a string in memory for a particular byte or word. It compares contents of byte in AL or word in AX with byte or word at memory pointed by ES:[DI]. SCAS performs AL/AX contents minus byte or word pointed by ES:[DI]. Operand values are not changed. Flags are affected based on result of subtraction.

SCASB is used for scanning string byte at a time. SCASW is used for scanning string word at a time. SCASB and SCASW are more common than SCAS. Instruction SCASB SCASW Source AL AX Destination ES:[DI] ES:[DI] If D = 0 incr. DI by 1 incr. DI by 2 If D = 1 decr. DI by 1 decr. DI by 2

SCASB is equivalent to (assuming D = 0) CMP AL, ES:[DI] PUSHF INC DI POPF Flags affected based on CMP instruction

SCASW is equivalent to (assuming D = 0) CMP AX, ES:[DI] PUSHF ADD DI, 2 POPF

Normally SCASB or SCASW instructions are used to check if a particular byte or word is present in the given string. In such a case, REPNE (Repeat while Not Equal) prefix is used. REPNZ (Repeat while Zero flag Not set) is same as REPNE

MOV CX, 08 MOV AL, 35H REPNE SCASB JNE FAIL Above 4 instructions used for Linear search are equivalent to the 9 instructions on the right (assuming D = 0)

MOV CX, 08 MOV AL, 35H JCXZ EXIT AGAIN: CMP AL, ES:[DI] PUSHF INC DI POPF LOOPNE AGAIN EXIT: JNE FAIL

NOTE: In case it is desired to check if all the bytes or words in a string are equal to a particular value, the REPE (Repeat while Equal) prefix is used. REPE can also be written as REPZ (Repeat while Zero flag is set) or REP.

You might also like