Beruflich Dokumente
Kultur Dokumente
20: Write an assembly language program to copy a block of ‘n’ data bytes (or
string) from data segment of memory to another section of memory in extra segment.
Program Analysis: Data stored in a memory block, called source. Data is to be copied at
another memory block, called destination. In all string instructions the source address is given
by DS:SI and destination address is given by ES:DI.
PROGRAM:
LEA SI, LIST1 ; initialize SI with the offset address of the source location
LEA DI, LIST2 ; initialize DI with the offset address of the destination location
MOV CX, 5 ; initialize counter
REP MOVSB ; Repeat MOVSB instruction CX times.
HLT ; stop the program
; data
LIST1 DB 12, 43, 54, 65, 76
LIST2 DB 5 DUP(0) ; destination initialised with 5 bytes all equal to 0.
Example 5.21: Write an assembly language program to exchange a block of ‘n’ data bytes
(or string) from data segment of memory with another section of memory in extra
segment.
Program Analysis: This program is similar to the above program. Only difference is that now
we have to interchange source and destination data, instead of only copying the source data to
the destination.
PROGRAM:
#make_COM#
; data
LIST1 DB 12, 43, 54, 65, 76
LIST2 DB 22, 33, 44, 55, 66
Example 5.22: A set of 8 data bytes is stored in the data segment of memory starting at
the offset address 2050H. Write an assembly language program to check each data byte
for bit D7 and D0. If D7 or D0 is 1, reject the data byte; otherwise, store the data bytes at
different memory locations starting from offset 2060H in the data segment.
Program Analysis: This program is also similar to Example 5.20. But now we have to copy
selected data, not all. Data has 1 in D0 and D7 position must be rejected.
ROL and ROR instructions are used to check D7 and D0 position respectively. If Carry flag is
set either of the two instructions, the number is rejected, otherwise copied to the destination
memory location.
Count is directly given in the question, i.e., 8. But this program may work for any number of
data bytes, by replacing the count by exact count instead of 8.
PROGRAM:
#make_COM#
Program Analysis: This program is also similar to Example 5.20. But we have to copy data
in reverse order at the destination. For this initialise the source pointer (i.e., SI register) with
the starting address of the source data and initialise destination pointer (i.e., DI register) with
the last address of the destination. Copy the first source data to last destination address. Then,
increment the SI and decrement the DI. Repeat the process until the counter reaches zero.
PROGRAM:
#make_COM#
Example 5.24: Write an 8085 assembly program to check if the two strings stored at two
different memory locations are same. If two strings are same, set Carry flag, otherwise
reset.
Program Analysis:
In this program we have to compare two strings stored in two different memory blocks. String
is nothing but a set of characters and the ASCII code of each character is stored in the memory
in contiguous locations. For example ASCII code of character ‘A’ is 65 in decimal, code of
character ‘B’ is 66 and so on. Hence, string may be treated as set of data bytes.
Two memory pointers are used. One memory pointer SI is initialised with the starting address
of the first set of data and second memory pointer DI is initialised with the starting address of
the second set of data. The carry flag is reset by CLC instruction initially.
The two strings are compared using CMPSB instruction with REPE prefix, which will repeat
the CMPSB instruction until any character in the two stings is found to be unequal. The CL is
decremented automatically by REPE prefix and SI and DI are automatically decremented by
CMPSB instruction. The CMPSB instruction is repeated by REPE prefix, while zero flag is
set. If zero flag is reset, i.e., compared data in the two set is not same, the REPE is terminated.
JNZ EXIT instruction exits the execution from the loop.
If zero flag is set after comparison, i.e., compared data in the two set is same. Then we continue
the comparison with next data by incrementing the two memory pointers. If the two strings are
exactly same, the REPE will terminate when counter CL reaches zero, without resetting zero
flag. Hence, JNZ instruction after REPE will not execute. Carry flag is set by STC instruction.
At the end of the program if carry flag is found to be set, means the two strings are same. And
if carry is found to be reset, means at least one character in the two strings is different.
PROGRAM:
#make_COM#
STR1 DB 'HFLLO'
STR2 DB 'HELLO'
5.1.2 Program to search a data byte in the given data list (or string)
Example 5.25: Write an assembly language program to search a data byte in the list of
‘n’ data bytes stored in the data segment of the memory. If the data byte is found, set
carry flag and if not found reset carry flag.
Program Analysis:
In this program we have to search a given data byte in the given list of data. To do this we will
copy the data byte to be searched into AL and compare with all the data bytes in the list using
SCASB instruction with REPNE prefix.
If it matches with any data byte in the list, the REPNE is terminated and zero flag will set. If
Zero flag is set, means, the data is found and execution is transferred to a different location
where carry flag is set.
If no match is found is the list, then zero flag will not set and REPNE will be terminated only
when counter reaches zero and carry flag is reset.
PROGRAM:
#make_COM#
LIST DB 12H, 43H, 23H, 7H, 21H, 34H, 9H, 10H, 0B2H, 2AH
Example 5.26: Write an assembly language program to scan a data byte in the memory
block of 128 bytes starting from 2501H. Assume that the data byte to be scanned is stored
at 2500H. Store the number that how many times the given data byte is found in the
memory block, at memory location 4000H and also display the data byte and the number
at output port 00H and 01H respectively.
(Raj. Univ. V Sem., CE, 2005)
Program Analysis: In previous program of Example 5.25, the aim was to search the given
data byte in the list and if it is found for the first time anywhere in the list program is terminated.
Result is displayed in terms of 01 (if found) or 00 (if not found).
But, in this program, a given data byte is to searched in the list till end and program should
count how many times it is available in the given list.
Register B is initialised with 00H. Every time the searched data byte is matched with any
number in the list, B in incremented by one. Hence, B stores the count.
PROGRAM:
#make_COM#
Result: the byte to be searched (i.e., 23H) at DS:3000H and the occurrence of the byte (i.e., 2)
in the given list is stored at DS:3001H.
Example 5.27 Write a program to convert the lowercase alphabet in the given string
stored in memory to uppercase alphabets and store in another location. The other
characters in the string should not change.
Solution:
The string stored in the memory, means, the ASCII value in 8-bit binary form corresponding
to each character is stored in contiguous memory location. For example the string ‘comp$1’
will be stored in the memory (starting from offset, say, 3000H) as shown in the table below:
The ASCII of lowercase alphabets lies between 97 (a) – 122 (z) and that of uppercase alphabet
lies between 65 (A) – 90 (Z). The difference in the ASCII value of the corresponding lowercase
and uppercase alphabets is 32 (in decimal).
Hence in the following program a single character is read from the memory one by one and
checked whether it is a lowercase alphabet or not. If it is a lowercase alphabet (ASCII lies
between 97-122) than 32 is subtracted from its ASCII value, else left unchanged.
#make_COM#
STR1 DB 'comp$1'
STR2 DB 6 DUP (0)
The given string ‘comp$1’ will be converted to ‘COMP$1’ and will be stored at the location
where STR2 is initialized.
Assignment: Write the program to convert a given string from uppercase to lowercase
alphabets, similarly, without converting other characters.