In: Computer Science
Please write in x86 Assembly language on Visual Studio
Write a program to compare two strings in locations str1 and str2. Initialize str1 to Computer and initialize str2 to Compater. Assume Str1 holds the correct spelling and str2 may have an incorrect spelling. Use string instructions to check if str2 is correct and if not correct the mistake in str2.
INCLUDE io.h | |
Cr EQU 0ah | |
Lf EQU 0dh | |
data SEGMENT | |
p_str1 DB Cr, Lf, 'Enter 1st string: ',0 | |
p_str2 DB Cr, Lf, 'Enter 2nd string: ',0 | |
p_not1 DB Cr, Lf, 'The strings are not same because of different lengths',0 | |
p_not2 DB Cr, Lf, 'The strings are not same because of different characters',0 | |
p_same DB Cr, Lf, 'The strings are the same',0 | |
str1 DB 100 DUP (?) | |
str2 DB 100 DUP (?) | |
data ENDS | |
code SEGMENT | |
ASSUME cs:code, ds:data | |
start: mov ax, data | |
mov ds, ax | |
;input str1 | |
output p_str1 | |
inputs str1, 100 | |
mov bx, cx | |
;input str2 | |
output p_str2 | |
inputs str2, 100 | |
;compare lengths | |
cmp bx, cx | |
jne l_not1;if different length jump | |
;initialize | |
lea si, str1 | |
lea di, str2 | |
;iterate to compare characters | |
nxt_chk:mov al, [si];copy the two bytes in ax | |
mov ah, [di] | |
cmp al, ah;compare the two bytes | |
jne l_not2;if not (ah==al) | |
inc si;increment the two bytes | |
inc di | |
dec cx;decrement the count(string length) | |
jz l_same;if count=0 the strings are same | |
jmp nxt_chk;else jump to ceck next byte | |
l_not1: output p_not1 | |
jmp quit | |
l_not2: output p_not2 | |
jmp quit | |
l_same: output p_same | |
quit: mov al, 00h | |
mov ah, 4ch | |
int 21h | |
code ENDS | |
END start |