-
Table of Contents
8086 PROGRAM TO COMPARE TWO STRINGS
When working with assembly language programming, one common task is comparing two strings. In this article, we will discuss how to write an 8086 program to compare two strings. We will explore the steps involved in comparing strings, the logic behind the comparison process, and provide a sample program for better understanding.
Understanding String Comparison
String comparison is a fundamental operation in programming that involves checking if two strings are equal or not. In assembly language programming, strings are represented as arrays of characters, with each character stored in a specific memory location. To compare two strings, we need to iterate through each character in both strings and check if they are equal.
Steps to Compare Two Strings
- Load the memory address of the first string into a register.
- Load the memory address of the second string into another register.
- Iterate through each character in both strings.
- Compare the characters at the current position in both strings.
- If the characters are equal, continue to the next character.
- If the characters are not equal, exit the loop and indicate that the strings are not equal.
- If all characters are equal, indicate that the strings are equal.
Sample 8086 Program
Here is a sample 8086 program to compare two strings:
“`assembly
.model small
.stack 100h
.data
string1 db ‘Hello$’
string2 db ‘World$’
msg1 db ‘Strings are equal$’
msg2 db ‘Strings are not equal$’
.code
main proc
mov ax, @data
mov ds, ax
mov si, offset string1
mov di, offset string2
repeat:
mov al, [si]
mov bl, [di]
cmp al, bl
jne not_equal
inc si
inc di
cmp al, ‘$’
jne repeat
mov ah, 09h
lea dx, msg1
int 21h
jmp exit
not_equal:
mov ah, 09h
lea dx, msg2
int 21h
exit:
mov ah, 4ch
int 21h
main endp
end main
“`
Conclusion
Comparing two strings in assembly language programming can be a challenging task, but with the right approach and logic, it can be achieved efficiently.
. By following the steps outlined in this article and studying the sample program provided, you can gain a better understanding of how string comparison works in the 8086 architecture. Remember to practice writing and executing programs to strengthen your skills in assembly language programming.