Find substring in string in Assembly – masm 8086

Find substring in string in Assembly – masm 8086


org 100h
.data
buf1 DB "Enter String :::: $"
buf2 DB 0dh,0ah,"Enter Substring to search :::: $"
buf3 DB 0dh,0ah,"Substring Found $",1
buf4 DB 0dh,0ah,"Substring not Found...$"
str DB 120,120 DUP(?)
str_len DW 0
substr DB 120,120 DUP (?)
substr_len DW 0
counter DW 0
.code
;initilize Data Segment
mov ax,@DATA
mov DS,ax
;==========Input String=====
lea dx,buf1
call Display
mov dx,offset str
mov ah,0Ah
int 21h
mov bl,[str+1]
mov str_len,bx
;=======Input Substring======
lea dx,buf2
call display
mov dx,offset substr
mov ah,0Ah
int 21h
;checking if substring is in string or not
lea SI,str ;SI contain String
add SI,2
lea DI,substr ;DI contain substring
add DI,2
mov bx,substr_len
cmp str_len,bx
jg G ;CX will contain greater value
mov cx,bx
G:
mov cx,str_len
;======Cheking if substring or not====
L:
lodsb
cmp al,0Dh
je final
cmp al,' '
jne next
final:
cmp counter,0
je exit
mov counter,0
lea DI,substr
add DI,2
jmp next1
next:
dec SI
next1:
cmpsb
je h
inc counter
h:
LOOP L
exit:
cmp counter,0
je Found
lea dx,buf4
call Display
jmp terminate
Found:
lea dx,buf3
call Display
terminate:
;to terminate program
mov ah,4ch
int 21h
ret
Display PROC
mov ah,09h
int 21h
ret
Display ENDP

Output:

capture

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s