How to resolve the algorithm Find limit of recursion step by step in the 8080 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find limit of recursion step by step in the 8080 Assembly programming language

Table of Contents

Problem Statement

Find the limit of recursion.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find limit of recursion step by step in the 8080 Assembly programming language

Source code in the 8080 programming language

	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

	org	100h
	lxi	h,-top	; Subtract highest used location from stack pointer
	dad	sp	
	xra	a	; This gives bytes, but a call takes two bytes;
	ora	h	; so HL should be divided by two to give the actual
	rar		; number.
	mov	h,a
	mov	a,l
	rar
	mov	l,a
	;;;	The number of free stack words is now in HL, output it
	lxi	d,num
	push	d
	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
	;;;	Placeholder for numeric output
	db	'00000'
num:	db	'$'	
	;;;	The program does not need any memory beyond this point.
	;;;	This means anything from this place up to SP is free for the
	;;;	stack.
top:	equ	$

  

You may also check:How to resolve the algorithm Empty program step by step in the Axe programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Infinity step by step in the RLaB programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the Haskell programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Stata programming language