; LOOKUP - find ASCII string in symbol tree
; Mike Gabrielson   8/10/78
; accepts: BC = address of string to look for, terminated by NULL
;          DE = address of ROOT pointer
;          HL = contents of ROOT pointer (equals address of
;		first node if not grounded)
; returns: HL = address of tree node containing the string
;          carry set if a new node had to be grown
;          else carry cleared (symbol already in tree)
; MEMORY OVERFLOW IS NOT TESTED FOR!
;================================================================

NULL	EQU	0	;end of string character

	EXTRN	SEARCH	;SEARCH is not defined in this assembly
	EXTRN	FIRST	;external pointer containing address of
			;first byte of available memory for
			;growing new nodes

LOOKUP: CALL	SEARCH	;Symbol already in tree?
	RNC		;yes, all done
	LHLD	FIRST	;no, get address of node about to sprout
	PUSH	H	;save address of new node for return
	XCHG		;HL := address of last pointer SEARCHed
	MOV	M,D	;replace grounded pointer with address
	INX	H	;of new node
	MOV	M,E
	XRA	A	;clear register A
	STAX	D	;and ground the two subtree pointers in the
	INX	D	;new node
	STAX	D
	INX	D
	STAX	D
	INX	D
	STAX	D
	INX	D
SAVNAM: LDAX	B	;get next character from caller's string
	STAX	D	;save in new node
	INX	B	;adjust pointers for next character
	INX	D
	CPI	NULL	;was that the end of the string?
	JNZ	SAVNAM	;no
	XCHG		;get address of next available byte into HL
	SHLD	FIRST	;save new FIRST
	POP	H	;yes, restore address of new node
	STC		;indicate new node was grown for caller
	RET

	END

