String Instructions
MOVS, CMPS, SCAS, LODS, STOS with REP.
String instructions in the 8086 microprocessor are a specialized group of instructions designed to operate on sequences of bytes or words stored in memory. Instead of writing loops manually to process each element, these instructions handle repetitive memory operations efficiently using the SI, DI, and CX registers together. Understanding string instructions is critical for GATE aspirants and university students because they combine memory addressing, flag behavior, and repeat prefixes into compact, exam-relevant patterns.
Core Concept Explanation
String instructions are memory-to-memory or accumulator-to-memory operations that work on entire blocks of data. The 8086 uses two pointer registers for this purpose. The SI (Source Index) register, combined with DS, points to the source operand. The DI (Destination Index) register, combined with ES, points to the destination. After each operation, SI and DI are automatically incremented or decremented depending on the state of the Direction Flag (DF). When DF is 0, pointers move forward. When DF is 1, pointers move backward.
The MOVS instruction copies a byte or word from the location DS:SI to ES:DI, then updates both pointers. MOVSB operates on bytes, MOVSW on words. The CMPS instruction compares the byte or word at DS:SI with the one at ES:DI and sets the flags accordingly, without storing any result. This is useful for comparing two strings in memory.
The SCAS instruction compares AL (or AX) with the byte or word at ES:DI and sets flags, then updates DI. It is used to scan a string for a specific character. The LODS instruction loads the byte or word from DS:SI into AL or AX and updates SI. The STOS instruction stores AL or AX into the memory location ES:DI and updates DI. LODS and STOS together are often used for filtering or transforming data in a string.
The REP Prefix and Its Variants
The REP prefix causes the string instruction that follows to repeat automatically, decrementing CX by 1 after each iteration and stopping when CX becomes 0. This eliminates the need for explicit loop instructions. REP is used with MOVS, LODS, and STOS. For compare and scan operations, two conditional variants exist: REPE/REPZ repeats while the Zero Flag is set, and REPNE/REPNZ repeats while the Zero Flag is clear. These are used with CMPS and SCAS to search or compare strings until a mismatch or match is found.
The CX register must be loaded with the count before using any REP prefix. If CX is initialized to 0, the instruction does not execute even once. This is a commonly tested edge case in GATE. The direction flag should always be set explicitly using CLD (clear DF, forward direction) or STD (set DF, reverse direction) before string operations to avoid unpredictable behavior.
Mathematical Expression
For a REP MOVSB operation copying N bytes, the total number of memory accesses is 2N, since each byte requires one read from DS:SI and one write to ES:DI. If CX is initialized to N, the processor performs exactly N iterations. The effective address calculation for each access is: EA = Base Register + Index Register, where the base is DS or ES and the index is SI or DI respectively. After each byte operation, SI and DI change by 1 (or by 2 for word operations). The formula for the final pointer value after REP MOVSB with N bytes starting at offset S is: SI_final = SI_initial + N (when DF=0), SI_final = SI_initial - N (when DF=1).
Practical Understanding
In real programming scenarios, REP MOVS is used for fast memory block copy routines, equivalent to memcpy in C. REP SCASB is used to find the length of a null-terminated string by scanning for a zero byte. REPE CMPSB is used to compare two strings byte by byte and identify the first point of difference. LODS and STOS are often combined in a loop to translate or convert characters in a string, such as converting lowercase to uppercase.
These instructions are hardware-optimized and execute faster than manually written loops for the same task. In 8086 assembly language programming labs and practical examinations, string instructions are a fundamental skill. The segment override capability also allows modifying which segment DS:SI points to using a segment override prefix, giving flexibility in memory addressing.
Solved Numerical Example
Consider a program that copies 50 bytes from a source block starting at offset 1000h in DS to a destination block starting at offset 2000h in ES. Using REP MOVSB, calculate how CX is initialized and how many total memory bus cycles occur for the copy.
Given:
Number of bytes to copy = 50
Source offset (SI) = 1000h
Destination offset (DI) = 2000h
Direction: Forward (DF = 0)
Why this formula applies:
Each REP MOVSB iteration reads 1 byte from DS:SI and writes 1 byte to ES:DI.
Total memory accesses = 2 x N (one read + one write per byte)
Formula:
CX = N (number of bytes)
Total memory accesses = 2 x CX
Substitution:
CX = 50
Total accesses = 2 x 50
Calculation:
Total memory bus cycles = 100
Final Answer:
CX must be initialized to 50.
Total memory bus cycles = 100 (50 reads + 50 writes).Exam Tip: If CX = 0 before a REP instruction, the string instruction does NOT execute even once. This is a classic GATE trap. Also remember REPE CMPSB stops either when CX = 0 OR when ZF = 0, whichever occurs first.
- MOVSB/MOVSW copies from DS:SI to ES:DI and auto-updates both pointers.
- CMPSB/CMPSW compares DS:SI with ES:DI and sets flags without storing a result.
- SCASB/SCASW compares AL/AX with ES:DI to scan for a specific value.
- LODSB/LODSW loads from DS:SI into AL/AX; STOSB/STOSW stores AL/AX into ES:DI.
- REP repeats CX times; REPE stops on ZF=0; REPNE stops on ZF=1.
- CLD sets DF=0 for forward direction; STD sets DF=1 for reverse direction.
Quick Revision
- SI with DS is source; DI with ES is destination for all string instructions.
- DF=0 (CLD): pointers increment. DF=1 (STD): pointers decrement.
- REP prefix uses CX as count; CX=0 means instruction skipped entirely.
- REPE CMPSB: stops when CX=0 OR mismatch found (ZF=0).
- REPNE SCASB: stops when CX=0 OR match found (ZF=1).
- Total memory accesses for REP MOVSB with CX=N: 2N (N reads + N writes).
- Exam trap: SCAS uses ES:DI only, not DS:SI, even though data being scanned is in memory.
String Manipulation Codes
Examine string transfer and comparison instructions.