finding a sub-string in a string in assembly language programming

[org 0x0100]
mov si,str1
mov di,substr2
call findlen
mov si,str1
mov di,substr2
call findSubstr
jmp finish

findlen:
mov cx, 0xffff
xor al, al
repne scasb
mov ax, 0xffff
sub ax, cx
mov cx,ax
ret

findSubstr:
push cx
push si
push di
repe cmpsb
jcxz there
jmp notthere


notthere:
pop di
pop si
pop cx
add si,1
cmp byte[si],0
jne findSubstr
mov ax, 0xb800
mov es, ax
mov ax,0x0700
mov di,1920
mov si,0
reprnt1
mov al,[print2+si]
mov word [es:di], ax
add di,2
add si,1
cmp si,9
jne reprnt1

ret



there:
pop di
pop si
pop cx
mov ax, 0xb800
mov es, ax
mov ax,0x0700
mov di,1920
mov si,0
reprnt
mov al,[print1+si]
mov word [es:di], ax
add di,2
add si,1
cmp si,5
jne reprnt
ret

finish:
mov ax,0x4c00
int 21h
str1: db 'Marry has a little lamb.',0
substr1: db 'Marry',0
substr2: db 'lamb..',0
print1: db 'found'
print2: db 'not found'

Comments

Popular Posts