Task

Solve the   Towers of Hanoi   problem with recursion.




	org	100h
	lhld	6	; Top of CP/M usable memory
	sphl		; Put the stack there
	lxi	b,0401h	; Set up first arguments to move()
	lxi	d,0203h
	call	move	; move(4, 1, 2, 3)
	rst	0	; quit program
	;;;	Move B disks from C via D to E. 
move:	dcr	b	; One fewer disk in next iteration
	jz	mvout	; If this was the last disk, print move and stop
	push	b	; Otherwise, save registers,
	push	d 
	mov	a,d	; First recursive call
	mov	d,e
	mov	e,a
	call	move	; move(B-1, C, E, D)
	pop	d	; Restore registers
	pop	b
	call	mvout	; Print current move
	mov	a,c	; Second recursive call
	mov	c,d
	mov	d,a
	jmp	move	; move(B-1, D, C, E) - tail call optimization
	;;;	Print move, saving registers.
mvout:	push	b	; Save registers on stack
	push	d
	mov	a,c	; Store 'from' as ASCII digit in 'from' space
	adi	'0'
	sta	out1
	mov	a,e	; Store 'to' as ASCII digit in 'to' space
	adi	'0'
	sta	out2
	lxi	d,outstr
	mvi	c,9	; CP/M call to print the string
	call	5
	pop	d	; Restore register contents
	pop	b
	ret
	;;;	Move output with placeholder for pole numbers
outstr:	db	'Move disk from pole '
out1:	db	'* to pole '
out2:	db	'*',13,10,'$'