Find limit of recursion is part of Short Circuit's Console Program Basics selection.
Task

Find the limit of recursion.




	org	100h
	lxi	b,0		; BC holds the amount of calls
	call	recur		; Call the recursive routine
	;;;	BC now holds the maximum amount of recursive calls one
	;;;	can make, and the stack is back to the beginning.
	;;;	Print the value in BC to the console. The stack is freed by ret
	;;;	so push and pop can be used.
	;;;	Make number into ASCII string
	lxi	h,num
	push	h
	mov	h,b
	mov	l,c
	lxi	b,-10
dgt:	lxi	d,-1
clcdgt:	inx	d
	dad	b
	jc	clcdgt
	mov	a,l
	adi	10+'0'
	xthl
	dcx	h
	mov	m,a
	xthl
	xchg
	mov	a,h
	ora	l
	jnz	dgt
	;;;	Use CP/M routine to print the string
	pop	d
	mvi	c,9
	call 	5
	rst	0
	;;;	Recursive routine
recur:	inx	b		; Count the call
	lxi	d,-GUARD	; See if the guard is intact (stack not full)
	lhld	guard		; (subtract the original value from the
	dad	d		; current one)
	mov	a,h		; If so, the answer should be zero
	ora	l
	cz	recur		; If it is, do another recursive call
	ret			; Return
	;;;	Placeholder for numeric output
	db	'00000'
num:	db	'$'
	;;;	The program doesn't need any memory after this location,
	;;;	so all memory beyond here is free for use by the stack.
	;;;	If the guard is overwritten, the stack has overflowed.
GUARD:	equ	$+2		; Make sure it is not a valid return address
guard:	dw	GUARD