;
; SYSLIB Module Name:  SINSTR
; Author:  Richard Conn
; SYSLIB Version Number:  2.0
; Module Version Number:  1.0
; Module Entry Points:
;	INSTR
; Module External References:
;	None
;

;
;  SINSTR --
;	INSTRING Function.  Scan string pointed to by HL for the string
; pointed to by DE.  A string in this case is a vector of bytes terminated
; by a binary 0.
;	On return, Zero Flag is set if found, and HL points to 1st byte
; of substring within scanned string.  DE and BC are not affected.  If
; string not found, Zero Flag is not set and HL is not affected.
;

INSTR::
	PUSH	B	; SAVE REGISTERS
	PUSH	H	; SAVE ORIGINAL HL

SCAN:
	PUSH	H	; SAVE PTR TO STRINGS
	PUSH	D

SCANL:
	LDAX	D	; GET BYTE FROM STRING TO SCAN FOR
	ORA	A	; END OF STRING?
	JZ	FOUND	; FOUND IF SO
	MOV	B,A	; SAVE IN B
	MOV	A,M	; GET BYTE OF STRING BEING SCANNED
	ORA	A	; END OF STRING?
	JZ	NOT$FOUND
	CMP	B	; COMPARE STRING ELEMENTS
	JNZ	NEXT
	INX	H	; PT TO NEXT BYTE
	INX	D
	JMP	SCANL	; CONTINUE SCAN

;  NO MATCH -- POINT TO NEXT BYTE IN STRING BEING SCANNED
NEXT:
	POP	D	; GET PTRS
	POP	H
	INX	H	; PT TO NEXT
	JMP	SCAN	; SCAN AGAIN

;  MATCH -- ZERO FLAG IS SET -- GET POINTERS
FOUND:
	POP	D	; GET PTRS
	POP	H

	POP	B	; CLEAR HL FROM STACK
	POP	B	; GET BC
	RET

;  NO MATCH -- SET NOT ZERO AND RESTORE ORIGINAL POINTERS
NOT$FOUND:
	POP	D	; GET PTRS
	POP	H

	POP	H	; RESTORE ORIGINAL HL
	POP	B	; GET BC
	MVI	A,0FFH	; SET NOT FOUND
	ORA	A	; SET NOT ZERO
	RET

	END
