LAB9
LAB9
Lab # 09:
To display and manipulate the strings using emu8086
software tool
Submission Profile
Comments: ______________________________________________________________________
Objectives
Theory:
Read the details given below in order to comprehend the basic operations of string instructions
programming. Study in detail and become familiar with the various ways and in which these
modes can be used.
String
The 8086 microprocessor is equipped with special instructions to handle string operations. By string it
mean a series of data words or bytes that reside in consecutive memory locations. The string
instructions of the 8086 permit a programmer to implement operations such as to move data from one
block of memory to a block elsewhere in memory. A second type of operation that is easily performed
is to scan a string and data elements stored in memory looking for a specific value. Other examples are
to compare the elements and two strings together in order to determine whether they are the same or
different. In 8086 assembly language, a memory string or string is simply a byte or word array.
Its purpose is to determine the direction in which string operations will proceed. The FLAGS register
contains six status flags and three control flags. We know that the status flags reflect the result of an
operation that the processor has done. The control flags are used to control the processor’s operations.
String operations are implemented by index registers SI and DI.
For example,
STRING1 DB ‘ABCDE’
Moving a String
.DATA
STRING1 DB “HELLO”
STRING2 DB 5 DUP (?)
And we want to move the contents of STRING1 (source string) into STRING2 (destination string).
This operation is needed for many string operations, such as duplication a string or concatenating
(attaching on string to the end of another string).
It copies the contents of the byte addressed by DS: SI, to the byte addressed by ES: DI. The contents of
the source byte are unchanged. After the byte has been moved, both SI and DI arc automatically
incremented If DF = 0, or decremented If DF= 1.
For example, to move the first two bytes of STRINGl to STRING2, we execute the following
Instructions:
MOV AX,@DATA
MOV DS, AX
MOV ES, AX
LEA SI, STRING1 ; SI points to source sting
LEA DI, STRING2 ; DI points to destination sting
CLD ; Clear DF
MOVSB ; move first byte
MOVSB ; move second byte
REP prefix
MOVSB moves only a single byte from the source string to the destination string. To move the entire
string, first initialize CX to the number N of bytes in the source string and execute,
REP MOVSB
The REP prefix causes MOVSB to be executed N times. After each MOVSB, CX is decremented until
it becomes 0. For example, to copy STRING1 of the preceding section Into STRING2, we execute
CLD
LEA DI,STRING2
REP MOVSB
MOVSW moves a word from the source to the destination string. It expects DS: SI to point to a source
string word, and ES: DI to point to a destination string word. After the string word has been moved,
both SI and DI arc automatically incremented by 2 if DF = 0, or decremented by 2 if DF= 1.
Storing a String
STOSB moves the contents of AL register to the byte address ES: DI (destination).
STOSW moves the contents of AX register to the word address ES: DI (destination).
For example the following instruction will store two “A”s in STRING1
MOV AX,@DATA
MOV ES, AX ; Initialize ES
LEA DI, STRING1 ; DI points to source string
CLD ; Process to the right
MOV AL, ’A’ ; Al has character to store
STOSB ; Store an ‘A’
STOSB ; Store another one
Lab Exercises:
Task 1
Write an assembly program to copy STRING1 into the STRING2 in reverse order.
Task 2
Write assembly language code to insert 30 between 20 and 40. (Assume DS and ES have been
initialized to the data segment).