String Instructions
in Assembly
Language
String instructions in assembly language offer a powerful way to
manipulate and process sequences of characters, which are
essential for tasks like text processing, data manipulation, and
user input handling.
by Susmitha .G
String Fundamentals
SI (Data Segment)
The source index (SI) register
points to the beginning of the
string within the data segment,
where the string is stored.
DI (Extra Segment)
The destination index (DI) register
points to the location in the extra
segment where the string will be
copied or processed.
Direction Flag (DF)
The direction flag (DF) determines
the direction of string processing.
DF=0 indicates increment, while
DF=1 indicates decrement.
Types of String Instructions
1 Repeat Instruction
These instructions are used to repeat a specific string
operation until a condition is met, controlled by the CX
register.
2 Move Instructions (MOVS)
These instructions move data from one memory
location to another, often used for copying strings.
3 Compare Instructions (CMPS)
These instructions compare the contents of two
memory locations, setting flags based on the
comparison result, used for finding patterns within
strings.
4 Scan Instructions (SCAS)
These instructions scan a string for a specific value
stored in the AL or AX register, used for finding a
specific character or pattern.
5 Load Instructions (LODS)
These instructions load a byte or word from a string
location pointed to by SI into AL or AX, used for
accessing individual characters.
6 Store Instructions (STOS)
These instructions store a byte or word from AL or AX
into a string location pointed to by DI, used for writing
to a string.
Move from Memory to Memory (MOVS)
MOVS
The MOVS instruction
copies the contents of a
byte or word from the
source address DS:SI to
the destination address
ES:DI.
MOVSB
This variant copies a
single byte from the
source address DS:SI to
the destination address
ES:DI.
MOVSW
This variant copies a
single word from the
source address DS:SI to
the destination address
ES:DI.
MOVSD
This variant copies a
double word from the
source address DS:SI to
the destination address
ES:DI.
Example: MOVS Instruction
Opcode Instruction Description
A4 MOVS m8, m8 Move byte at address DS:
(E)SI to address ES:(E)DI
A5 MOVS m16, m16 Move word at address DS:
(E)SI to address ES:(E)DI
A5 MOVS m32, m32 Move doubleword at
address DS:(E)SI to address
ES:(E)DI
A4 MOVSB Move byte at address DS:
(E)SI to address ES:(E)DI
A5 MOVSW Move word at address DS:
(E)SI to address ES:(E)DI
A5 MOVSD Move doubleword at
address DS:(E)SI to address
ES:(E)DI
Compare Memory with
Memory (CMPS)
Compare Logic
The CMPS instruction compares the values pointed to by DS:SI
and ES:DI, setting flags based on the comparison result. The
operands themselves are not altered.
Increment or Decrement
After the comparison, SI and DI are incremented (if DF = 0) or
decremented (if DF = 1), in preparation for comparing the next
element.
Direction Flag
The DF determines the direction of comparison, with DF = 0 for
increment and DF = 1 for decrement.
Scan a String (SCAS)
Scan Purpose
The SCAS instruction scans a string for a specific value stored in the AL or AX
register, comparing each character with the value.
String Location
The string to be scanned is pointed to by the ES:DI register pair.
Length
The CX register holds the length of the string to be scanned.
Direction Flag
The DF controls the direction of scanning. DF=0 increments DI, while DF=1
decrements DI.
Scan a String Example
The SCAS instruction is often used in loops to search for specific patterns or characters within a string. It
compares each character in the string with the value in AL or AX, and sets flags based on the result.
Loading Instructions (LODS)
LODS
The LODS instruction copies a byte or word from a string location pointed to by
SI into AL or AX.
LODSB
This variant loads a byte from the string location pointed to by SI into AL.
LODSW
This variant loads a word from the string location pointed to by SI into AX.
LODS Instructions:
Applications
The LODS instructions are crucial for processing strings character
by character, enabling tasks such as iterating through a string,
extracting specific characters, or performing character-based
operations.

String-Instructions-in-Assembly-Language.pptx

  • 1.
    String Instructions in Assembly Language Stringinstructions in assembly language offer a powerful way to manipulate and process sequences of characters, which are essential for tasks like text processing, data manipulation, and user input handling. by Susmitha .G
  • 2.
    String Fundamentals SI (DataSegment) The source index (SI) register points to the beginning of the string within the data segment, where the string is stored. DI (Extra Segment) The destination index (DI) register points to the location in the extra segment where the string will be copied or processed. Direction Flag (DF) The direction flag (DF) determines the direction of string processing. DF=0 indicates increment, while DF=1 indicates decrement.
  • 3.
    Types of StringInstructions 1 Repeat Instruction These instructions are used to repeat a specific string operation until a condition is met, controlled by the CX register. 2 Move Instructions (MOVS) These instructions move data from one memory location to another, often used for copying strings. 3 Compare Instructions (CMPS) These instructions compare the contents of two memory locations, setting flags based on the comparison result, used for finding patterns within strings. 4 Scan Instructions (SCAS) These instructions scan a string for a specific value stored in the AL or AX register, used for finding a specific character or pattern. 5 Load Instructions (LODS) These instructions load a byte or word from a string location pointed to by SI into AL or AX, used for accessing individual characters. 6 Store Instructions (STOS) These instructions store a byte or word from AL or AX into a string location pointed to by DI, used for writing to a string.
  • 4.
    Move from Memoryto Memory (MOVS) MOVS The MOVS instruction copies the contents of a byte or word from the source address DS:SI to the destination address ES:DI. MOVSB This variant copies a single byte from the source address DS:SI to the destination address ES:DI. MOVSW This variant copies a single word from the source address DS:SI to the destination address ES:DI. MOVSD This variant copies a double word from the source address DS:SI to the destination address ES:DI.
  • 5.
    Example: MOVS Instruction OpcodeInstruction Description A4 MOVS m8, m8 Move byte at address DS: (E)SI to address ES:(E)DI A5 MOVS m16, m16 Move word at address DS: (E)SI to address ES:(E)DI A5 MOVS m32, m32 Move doubleword at address DS:(E)SI to address ES:(E)DI A4 MOVSB Move byte at address DS: (E)SI to address ES:(E)DI A5 MOVSW Move word at address DS: (E)SI to address ES:(E)DI A5 MOVSD Move doubleword at address DS:(E)SI to address ES:(E)DI
  • 6.
    Compare Memory with Memory(CMPS) Compare Logic The CMPS instruction compares the values pointed to by DS:SI and ES:DI, setting flags based on the comparison result. The operands themselves are not altered. Increment or Decrement After the comparison, SI and DI are incremented (if DF = 0) or decremented (if DF = 1), in preparation for comparing the next element. Direction Flag The DF determines the direction of comparison, with DF = 0 for increment and DF = 1 for decrement.
  • 7.
    Scan a String(SCAS) Scan Purpose The SCAS instruction scans a string for a specific value stored in the AL or AX register, comparing each character with the value. String Location The string to be scanned is pointed to by the ES:DI register pair. Length The CX register holds the length of the string to be scanned. Direction Flag The DF controls the direction of scanning. DF=0 increments DI, while DF=1 decrements DI.
  • 8.
    Scan a StringExample The SCAS instruction is often used in loops to search for specific patterns or characters within a string. It compares each character in the string with the value in AL or AX, and sets flags based on the result.
  • 9.
    Loading Instructions (LODS) LODS TheLODS instruction copies a byte or word from a string location pointed to by SI into AL or AX. LODSB This variant loads a byte from the string location pointed to by SI into AL. LODSW This variant loads a word from the string location pointed to by SI into AX.
  • 10.
    LODS Instructions: Applications The LODSinstructions are crucial for processing strings character by character, enabling tasks such as iterating through a string, extracting specific characters, or performing character-based operations.