Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created August 4, 2012 06:08
Show Gist options
  • Select an option

  • Save tanayseven/3255040 to your computer and use it in GitHub Desktop.

Select an option

Save tanayseven/3255040 to your computer and use it in GitHub Desktop.
program to compare two strings without using string instructions using 8086 compatible assembly language
;program to compare two strings without using string instructions
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment