I have a 8086 Assembly Language Program to find out whether a given sub-string is present or not in a main string of characters. Its working fine when the sub string is a single character.Otherwise not. Help me to find the bug.
here is my code snippet
print macro arg
lea dx,arg
mov ah,09h
int 21h
endm
data segment
CarriageReturn equ 0dh ; Next Line
LineFeed equ 0ah ; Printer Next Line
Message1 db CarriageReturn,LineFeed,"Enter the string:$"
Message2 db CarriageReturn,LineFeed,"Enter the sub string:$"
Message3 db CarriageReturn,LineFeed,"sub string found $"
Message4 db CarriageReturn,LineFeed,"sub string not found $"
String db 100 dup(?)
SubString db 100 dup(?)
outs dw 100 dup(?)
begin db 0000h
n db 0000h
StringLength db 0000h
SubStringLength db 0000h
dif db 0000h
n1 db 0000h
of1 dw 0000h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data ; Point ds ( Data Segement )
mov ds,ax ; To data segment
lea si,String ; si to Main String
lea di,SubString ; di to Sub String
mov cl,00h
print Message1 ; Call Macro to Display String "Enter the String"
LoopReadMainString:
mov ah,01h ; Dos Function To Read Character From Standard Input
int 21h ; Dos Interrupt 21h
; OUTPUT : AL = character from the standard input device
cmp al,0dh ; Cmp if carrage return or Enter Key is Pressed
mov StringLength,cl ; Store the Length of String in Memory
je l1 ; If Enter Key is pressed jump to l1
mov [si],al ; Store Character in Memory Address of Main String
inc si ; Increment to Next Memory Location for next character in Main String
inc cl ; Increment String Length Counter
jmp LoopReadMainString
l1:
print Message2 ; Call Macro to Display String "Enter the sub string"
mov cl,00h
jmp LoopReadSubString
LoopReadSubString:
mov ah,01h ; Dos Function To Read Character From Standard Input
int 21h ; Dos Interrupt 21h
cmp al,0dh ; Cmp if carrage return or Enter Key is Pressed
mov SubStringLength,cl ; Store the Length of Sub String in Memory
je l3 ; If Enter Key is pressed Jump to l3
mov [di],al ; Store Character in Memory Address of Sub Main String
inc di ; Increment to Next Memory Location for next character in Main String
inc cl ; Increment String Length Counter
jmp LoopReadSubString
l3:
mov al,StringLength
cmp al,SubStringLength ; Cmp if StringLength = SubStringLength
jz l4 ; Jump to l4 if zero flag is set, ZF = 1 if equal
jnc l4 ; Jump if carry flag not set to l4
jc exit1 ; if carry flag is set go to exit
l4:
lea si,String ; Load effective address of string to si, ideally point si to memory location of String
lea di,SubString ; Point di to memory location of SubString
mov bl,[di] ; copy character in memory location of di ( SubString ) to bl
mov begin,bl ; copy character to begin variable
mov cl,StringLength ; cl or cx is generally used as counter, copy String Length to cl
jmp l5 ; Jmp to l5.
l5:
cmp cl,00h ; cmp to see if the value of cl counter ie length of string value is 0
je exit1 ; jump to exit.
mov al,[si] ; copy character in main string to al
cmp al,begin ; Compare character in main string with character in substirng ( both are pointing to memory location )
mov bl,SubStringLength ; bl will contain the string length of substring
jz l8 ; if they are same jump to l8
inc si ; inc Main String Pointer to point to next character
dec cl ; counter cx is decrease to compare to zero the first line in l5
jmp l5 ; loop . jump to l5
l8:
dec cl
mov ax,si
mov of1,ax
jmp l6
l6:
cmp bl,00h
je exit2
mov al,[si]
cmp al,[di]
inc si
inc di
dec bl
jnz l7
jmp l6
l7:
mov si,of1
mov al,[si]
inc si
lea di,SubString
jmp l5
exit1:
print Message4 ; Print "Substring Found"
mov ah,4ch ; Dos Sub Function To Exit Program
int 21h ; Dos Interrupt 21h
exit2:
print Message3 ; Print "Substring Not Found"
mov ah,4ch ; Dos SubFunction To Exit Program
int 21h ; Dos Interrupt 21h
code ends
end start
print macro arg
lea dx,arg
mov ah,09h
int 21h
endm
data segment
CarriageReturn equ 0dh ; Next Line
LineFeed equ 0ah ; Printer Next Line
Message1 db CarriageReturn,LineFeed,"Enter the string:$"
Message2 db CarriageReturn,LineFeed,"Enter the sub string:$"
Message3 db CarriageReturn,LineFeed,"sub string found $"
Message4 db CarriageReturn,LineFeed,"sub string not found $"
String db 100 dup(?)
SubString db 100 dup(?)
outs dw 100 dup(?)
begin db 0000h
n db 0000h
StringLength db 0000h
SubStringLength db 0000h
dif db 0000h
n1 db 0000h
of1 dw 0000h
data ends
code segment
assume cs:code,ds:data
start:
mov ax,data ; Point ds ( Data Segement )
mov ds,ax ; To data segment
lea si,String ; si to Main String
lea di,SubString ; di to Sub String
mov cl,00h
print Message1 ; Call Macro to Display String "Enter the String"
LoopReadMainString:
mov ah,01h ; Dos Function To Read Character From Standard Input
int 21h ; Dos Interrupt 21h
; OUTPUT : AL = character from the standard i/0 devi
cmp al,0dh ; Cmp if carrage return or Enter Key is Pressed
mov StringLength,cl ; Store the Length of String in Memory
je Next ; If Enter Key is pressed jump to l1
mov [si],al ; Store Character in Memory Address of Main String
inc si ; Increment to Next Memory Location for next character in Main String
inc cl ; Increment String Length Counter
jmp LoopReadMainString
Next:
print Message2 ; Call Macro to Display String "Enter the sub string"
mov cl,00h
jmp LoopReadSubString
LoopReadSubString:
mov ah,01h ; Dos Function To Read Character From Standard Input
int 21h ; Dos Interrupt 21h
cmp al,0dh ; Cmp if carrage return or Enter Key is Pressed
mov SubStringLength,cl ; Store the Length of Sub String in Memory
je Nextto ; If Enter Key is pressed Jump to l3
mov [di],al ; Store Character in Memory Address of Sub Main String
inc di ; Increment to Next Memory Location for next character in Main String
inc cl ; Increment String Length Counter
jmp LoopReadSubString
Nextto:
lea si,String
lea di,SubString
CheckFirstLetter:mov al,[si]
mov bl,[di]
cmp al,bl
jne CheckNextLetter
inc di
dec SubStringLength
jnz CheckNextLetter
print Message3
jmp exit
CheckNextLetter:inc si
dec StringLength
mov dl,StringLength
cmp dl,00h
jnz checkit
print Message4
checkit:mov al,[si]
mov bl,[di]
cmp al,bl
je CheckFirstLetter
print Message4
exit:
mov ah,4ch ; Dos Sub Function To Exit Program
int 21h ; Dos Interrupt 21h
code ends
end start