TITLE Substring Procedure
.MODEL SMALL
.STACK 100h
.DATA

msg1	DB	'Enter Substring:', 0dh,0ah, '$'
msg2	DB  0dh, 0ah, 'Enter Comparison String:', 0dh, 0ah,'$'
str1	DB	80 dup (0)
str2	DB	80 dup (0)
stop	DW	?
start	DW	?
lenstr	DW	?
okmsg	DB	0dh, 0ah, 'Substring found.$'
nomsg	DB  0dh, 0ah, 'Substring not found.$'
.CODE

MAIN PROC
	mov AX, @DATA
	mov DS, AX
	mov ES, AX

; ask for substring
	mov AH, 9
	lea DX, msg1
	int 21h

; store string
	lea DI, str1
	call READ_STR
	mov lenstr, BX  ; readstr returns length of input in BX
			; we need to stash this somewhere

; ask for next string
	lea DX, msg2
	int 21h

; get next stirng
	lea DI, str2
	call READ_STR ; bx now has length of D1

; see if string null or str1 longer than str2
	or BX, BX  ; null string?
	je NO
	cmp lenstr, 0
	je NO
	cmp lenstr, bx
	jg NO
; do comparison
	lea SI, str1
	lea DI, str2
	cld ; process left to right

; initialize search
	mov stop, DI 
	add stop, BX  ; bx has length of str2, stop has base address of str2
	mov CX, lenstr ; cx now has length of str1
	sub stop, CX ; stop is decremented length of str1
	mov start, DI ; initialize start to base address of str2

REPEATLOOP:
	mov CX, lenstr
	mov DI, start ; reset DI

	lea SI, str1
	repe CMPSB ; subtract/compare
	je YES

	inc start ; this moves comparison slowly to the right
	mov AX, start
	cmp AX, stop
	jnle NO
	jmp REPEATLOOP
YES:
	lea DX, okmsg
	jmp DISPLAY

NO:
	lea DX, nomsg

DISPLAY: 
	mov AH, 9
	int 21h

; exit
	mov AH, 4ch
	int 21h
MAIN ENDP

READ_STR PROC
; read and stores a string
; input: DI offset of a string
; output: DI offset of a string
; BX = number of characters read

	push AX
	push DI
	cld
	xor BX,BX
	mov AH,1
	int 21h ; read character into AL

WHILE1:
	cmp AL,0dh	; look for CR
	je	END_WHILE1
	cmp AL, 8h	; backspace?
	jne ELSE1
; else
	dec DI ; move string ptr back
	dec BX ; move counter back
	jmp READ ; get another char
ELSE1:
	stosb ; automatically increments DI
	inc BX ; move counter up
READ:
	int 21h
	jmp WHILE1
END_WHILE1:
	pop DI
	pop AX
	ret
READ_STR ENDP

end MAIN
