100% found this document useful (1 vote)
44 views

Assembly Lecture 11

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
44 views

Assembly Lecture 11

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Lecture # 11

The String
Instructions
Engr. Fiaz Khan

Engr. Muhammad Fiaz 1


Overview
• Discussion on a specialized group of instructions called string instructions.
• Definition of a memory string in 8086 assembly language as a byte or word
array.
• String instructions are tailored for array processing.
• Examples of String Operations:
• Copying a string into another string.
• Searching a string for a specific byte or word.
• Storing characters in a string.
• Comparing strings of characters alphabetically.

Engr. Muhammad Fiaz 2


Overview - Conti
• Advantages of String Instructions:
• Automatic updating of pointer registers.
• Enable memory-to-memory operations.
• Comparison with Register Indirect Addressing:
• String instructions provide certain built-in advantages compared to
using the register indirect addressing mode.
• Although tasks achievable with string instructions can be performed
using the register indirect addressing mode discussed in Chapter 10,
string instructions offer additional benefits.

Engr. Muhammad Fiaz 3


The Direction Flag
• Introduction to FLAGS Register:
• Explanation of the FLAGS register containing six status flags and three control flags.
• Differentiation between status flags reflecting the result of processor operations and control flags
used for controlling the processor's operations.
• Role of Control Flags:
• Discussion on the purpose of control flags in influencing the processor's behavior.
• Focus on Direction Flag (DF):
• Highlighting the function of the direction flag (DF) in determining the direction of string
operations.
• Mentioning that string operations are executed by the index registers SI and DI.
• Illustrative Example:
• Hypothetical scenario involving a declared string to demonstrate the role of the direction flag.

Engr. Muhammad Fiaz 4


Example
• | Offset address | Content | ASCII character |
• |---------------- |--------- |----------------- |
•| 0200h | 041h | A |
•| 0201h | 042h | B |
•| 0202h | 043h | C |
•| 0203h | 044h | D |
•| 0204h | 045h | E |

Engr. Muhammad Fiaz 5


Example-Conti
• String Declaration:
• Example string named "STRING1" declared as "ABCDE" with corresponding memory addresses
and content.
• Direction of String Operations:
• Explanation of how the direction flag (DF) influences the movement of index registers SI and DI
during string operations.
• When DF = 0, SI and DI move from left to right across the string (increasing memory addresses).
• When DF = 1, SI and DI move from right to left across the string (decreasing memory addresses).
• Representation in DEBUG Display:
• Illustration of how the value of the overflow flag (OF) is represented in DEBUG display, where OF
= 0 is symbolized by "UP" and OF = 1 by “DN".

Engr. Muhammad Fiaz 6


CLD and STD Instructions:
• To set the overflow flag (OF) to 0, use the CLD instruction.
• CLD stands for "Clear Direction Flag".
• To set the overflow flag (OF) to 1, use the STD instruction.
• STD stands for "Set Direction Flag".
• Effect on Other Flags:
• The CLD and STD instructions only affect the direction flag (DF) and do not
have any impact on the other flags.

Engr. Muhammad Fiaz 7


Moving a String
• String Definition:
• Two strings, STRING1 and STRING2, are defined with the contents
"HELLO" and a buffer size of 5 bytes, respectively.
• Purpose of String Operation:
• The operation of moving the contents of STRING1 (source string) into
STRING2 (destination string) is essential for various string operations,
such as duplicating a string or concatenating strings (joining one
string to the end of another string).

Engr. Muhammad Fiaz 8


Moving a String - Conti
• MOVSB Instruction:
• The MOVSB instruction is used to move a byte from the source string
to the destination string.
• MOVSB stands for "Move String Byte".
• It copies the content of the byte addressed by DS:SI to the byte
addressed by ES:DI, without changing the source byte.
• After moving the byte, both SI and DI (source and destination index
registers) are automatically incremented if OF = 0 or decremented if
OF = 1.

Engr. Muhammad Fiaz 9


Moving a String – Conti
• Example Execution:
• An example is provided to demonstrate moving the first two bytes of
STRING1 to STRING2 using the MOVSB instruction after initializing the
data segment registers and clearing the overflow flag (OF).

Engr. Muhammad Fiaz 10


Example Code
.DATA
STRING1 DB 'HELLO'
STRING2 DB 5 DUP (?)

.CODE
MOV AX, @DATA ; initialize DS and ES
MOV DS, AX
MOV ES, AX
LEA SI, STRING1 ; SI points to source string
LEA DI, STRING2 ; DI points to destination string
CLD ; clear DF
MOVSB ; move first byte
MOVSB ; move second byte

Engr. Muhammad Fiaz 11


Engr. Muhammad Fiaz 12
The REP Prefix
• Introduction to REP Prefix:
• The REP prefix is used with the MOVSB instruction to perform
repeated string operations.
• MOVSB for Single Byte Move:
• MOVSB instruction moves only one byte from the source string to the
destination string.

Engr. Muhammad Fiaz 13


The REP Prefix -Conti
• Moving Entire String:
• To move the entire string, initialize the CX register with the number of
bytes in the source string and use the REP MOVSB instruction.
• REP prefix causes MOVSB to execute N times, where N is the value in
CX.
• After each MOVSB, CX is decremented until it becomes 0.

Engr. Muhammad Fiaz 14


The REP Prefix -Conti
• Example Execution:
• For instance, to copy the contents of STRING1 to STRING2, the
following steps are executed:
• Clear direction flag (CLD).
• Load the offset of STRING1 into SI and STRING2 into DI.
• Initialize CX with the number of characters in STRING1.
• Use REP MOVSB to copy the entire string.

Engr. Muhammad Fiaz 15


The REP Prefix -Conti

Engr. Muhammad Fiaz 16


The REP Prefix -Conti

Engr. Muhammad Fiaz 17


MOVSW
• Introduction to MOVSW:
• MOVSW is the word form of the MOVSB instruction.
• Functionality of MOVSW:
• MOVSW moves a word (two bytes) from the source string to the
destination string.
• Similar to MOVSB, it expects DS:SI to point to a source string word
and ES:DI to point to a destination string word.

Engr. Muhammad Fiaz 18


MOVSW
• Effect on Index Registers:
• After moving a string word, both SI and DI are increased by 2 if DF = 0
(direction flag cleared) or decreased by 2 if DF = 1 (direction flag set).
• Impact on Flags:
• MOVSB and MOVSW instructions do not affect the processor flags.

Engr. Muhammad Fiaz 19


STOSB
• Introduction to STOSB and STOSW:
• STOSB instruction: Stores the contents of the AL register into the byte
addressed by ES:DI.
• STOSW instruction: Stores the contents of the AX register into the
word at address ES:DI and updates DI by 2 according to the direction
flag setting.
• Effect on Index Register (DI):
• DI is incremented if OF = 0 (direction flag cleared) or decremented if
OF = 1 (direction flag set) after both STOSB and STOSW instructions.

Engr. Muhammad Fiaz 20


STOSB
• Impact on Flags:
• STOSB and STOSW instructions do not affect the processor flags.
• Example Usage (STOSB):
• The provided example demonstrates how to use STOSB to store two
characters ('A') in STRING1.
• Initialize ES and DI to point to STRING1.
• Process to the right (increment DI).
• Store 'A' in the first byte.
• Store another 'A' in the next byte.

Engr. Muhammad Fiaz 21


Engr. Muhammad Fiaz 22
Load String
• Introduction to LODSB and LODSW:
• LODSB instruction: Loads a byte from the memory address pointed to
by DS:SI into AL.
• LODSW instruction: Loads a word (2 bytes) from the memory address
pointed to by DS:SI into AX.
• Increment/Decrement of SI:
• After LODSB or LODSW, SI is incremented by 1 (for LODSB) or 2 (for
LODSW) if DF (direction flag) is 0, and decremented if DF is 1.

Engr. Muhammad Fiaz 23


Load String - Conti
• Usage of LODSB:
• LODSB can be used to examine the characters of a string.
• Effect on Flags:
• LODSB and LODSW instructions do not affect the processor flags.
• Illustration of LODSB:
• Example: If STRING1 is defined as 'ABC', the code provided
successively loads the first and second bytes of STRING1 into AL.

Engr. Muhammad Fiaz 24


Engr. Muhammad Fiaz 25
Scan String
• Introduction to SCASB and SCASW:
• SCASB instruction: Used to examine a string for a target byte. The target
byte is in AL.
• SCASW instruction: Used to examine a string for a target word. The target
word is in AX.
• Operation of SCASB:
• SCASB subtracts the string byte pointed to by ES:DI from AL and sets the
flags based on the result.
• The result of the subtraction is not stored.
• Afterward, DI is incremented if DF (direction flag) is 0 or decremented if DF
is 1.

Engr. Muhammad Fiaz 26


Scan String - Conti
• Operation of SCASW:
• SCASW subtracts the word addressed by ES:DI from AX and sets the
flags.
• DI is increased by 2 if DF is 0 or decreased by 2 if DF is 1.
• Effect on Flags:
• All the status flags are affected by SCASB and SCASW instructions.

Engr. Muhammad Fiaz 27


Engr. Muhammad Fiaz 28
SCASB Example Explaination
• Initialization:
• Increment/Decrement:
• The string "ABC" is located in • Since the direction flag (DF) is usually cleared by
memory, and DI points to the default, DI is automatically incremented to point
beginning of the string. to the next byte in the string, which is 'B'.
• AL contains the byte 'B' that we want • Comparison:
to compare.
• Now, 'B' (pointed to by DI) is compared with 'B'
• Comparison: (in AL).
• The byte pointed to by DI is • Since 'B' and 'B' are equal, the zero flag (ZF) is
compared with the byte in AL. set to indicate a match.
• DI points to 'A', so 'A' is compared • Result:
with 'B'. • Since there is a match, further processing can be
• Since 'A' and 'B' are not equal, the done based on this result. For example, you can
check the value of the zero flag (ZF) to see if
zero flag (ZF) is cleared. there was a match or not.

Engr. Muhammad Fiaz 29


Compare String
• CMPSB Instruction: Compares byte values between two memory
locations.
• Functionality: It subtracts the byte addressed by ES:DI from the byte
addressed by DS:SI and sets the flags accordingly.
• Effect on Registers: After comparison, SI and DI are both incremented
if DF = 0 (direction flag), or decremented if DF = 1.

Engr. Muhammad Fiaz 30


Compare String
• Word Version (CMPSW): Similar to CMPSB but works with word-sized
data. It subtracts word values and sets flags accordingly.
• Increment/Decrement: If DF = 0, SI and DI are increased by 2; if DF =
1, they are decreased by 2.
• Usefulness: CMPSW is beneficial for comparing word arrays,
especially in numerical operations.

Engr. Muhammad Fiaz 31


Engr. Muhammad Fiaz 32
Engr. Muhammad Fiaz 33
Instruction Description Purpose Effect on Registers
Copy bytes from SI, DI are
Move byte from DS:SI to
MOVSB source to incremented/decreme
ES:DI
destination nted
DI is
Store byte in AL to Store byte from AL
STOSB incremented/decreme
ES:DI into destination
nted
SI is
Load byte from DS:SI Load byte from
LODSB incremented/decreme
into AL source into AL
nted
Search for DI is
Scan byte in AL with
SCASB specific byte in incremented/decreme
byte at ES:DI
string nted
Compare
SI, DI are
Compare byte at DS:SI corresponding
CMPSB incremented/decreme
with byte at ES:DI bytes in two
Engr. Muhammad Fiaz nted 34
strings
REPE and REPZ
• String Comparison with REP Prefix: CMPSB or CMPSW can be used with
the REPE or REPZ prefix.
• Initialization of CX: CX is set to the length of the shorter string.
• REPE CMPSB/CMPSW: Repeatedly compares string bytes or words until
there's a mismatch or CX becomes 0.
• Flag Handling: Flags are set based on the last comparison result.
• Usage of CMPSB: CMPSB can compare strings alphabetically or check if
one string is a substring of the other.
• Example: If STR1 and STR2 are strings of length 10, the instructions will put
0 in AX if the strings are identical, 1 if STR1 comes first alphabetically, or 2 if
STR2 comes first alphabetically (assuming DS and ES are initialized).

Engr. Muhammad Fiaz 35


Finding a Substring of a String
String 1 String 2 Main String
SUB1 SUB2 MAINST

'ABC' 'CAB' 'ABABCA'

Engr. Muhammad Fiaz 36


Engr. Muhammad Fiaz 37
General Form of the String Instructions

Engr. Muhammad Fiaz 38


ENGR. MUHAMMAD FIAZ
LECTURER COMPUTER SCIENCE

TELF/EF SET / ACEPT CERTIFIED

MICROSOFT ACADEMY TRAINER

ORACLE CERTIFIED PROFESSIONAL

GOOGLE CERTIFIED PROFESSIONAL

THANK YOU! Lahore, Pakistan

+92-320-7617-093

muhammad.fiaz@cs.uol.edu.pk
Do you have any questions?
https://www.linkedin/in/fiazofficials

Engr. Muhammad Fiaz 39

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy