;
; SYSLIB Module Name:  SMHL5D
; Author:  Richard Conn
; SYSLIB Version Number:  2.0
; Module Version Number:  1.0
; Module Entry Points:
;	MHL5DC		MHLDC
; Module External References:
;	None
;

;
;  MHL5DC --
;	STORE HL AS 5 DECIMAL CHARACTERS IN MEMORY
; ON INPUT, HL=VALUE AND DE=PTR TO 5-BYTE BUFFER
; ON OUTPUT, DE PTS TO NEXT BYTE AFTER BUFFER
;

PUTRG	MACRO
	PUSH	B	; SAVE BC, DE, HL
	PUSH	D
	PUSH	H
	ENDM
GETRG	MACRO
	POP	H	; RESTORE HL, DE, BC
	POP	D
	POP	B
	ENDM


MHL5DC::
	PUSH	PSW	; SAVE ALL REGS
	XCHG		; HL PTS TO BUFFER
	SHLD	MEMPTR
	XCHG
	PUTRG
	XRA	A	; A=0
	STA	LSFLG0	; TURN OFF LEADING <SP>
	JMP	PHDC
;
;  MHLDC --
;	STORE HL AS DECIMAL CHARACTERS W/LEADING SPACES IN 5-CHAR FIELD
; IN MEMORY; ON INPUT, HL=VALUE AND DE=PTR TO 5-BYTE BUFFER
; ON RETURN, DE PTS TO BYTE AFTER BUFFER
;
MHLDC::
	PUSH	PSW	; SAVE ALL REGS
	XCHG		; HL PTS TO BUFFER
	SHLD	MEMPTR
	XCHG
	PUTRG
	MVI	A,1	; A=1
	STA	LSFLG0	; TURN ON LEADING <SP>
;  PRINT HL USING LEADING <SP> FLAG LSFLG0
PHDC:	LXI	D,10000	; PRINT 10000'S
	CALL	PHDC1
	LXI	D,1000	; PRINT 1000'S
	CALL	PHDC1
	LXI	D,100	; PRINT 100'S
	CALL	PHDC1
	LXI	D,10	; PRINT 10'S
	CALL	PHDC1
	MOV	A,L	; PRINT 1'S
	ADI	'0'	; CONVERT TO ASCII
	CALL	PUT
	GETRG		; RESTORE ALL REGS
	XCHG		; DE=VALUE
	LHLD	MEMPTR	; GET PTR TO NEXT BYTE
	XCHG		; ... IN DE
	POP	PSW
	RET

;  BUFFERS
LSFLG0:	DS	1	; LEADING <SP> FLAG
MEMPTR:	DS	2	; PTR TO NEXT MEMORY ADDRESS
	
;  DIVIDE HL BY DE AND PRINT QUOTIENT WITH LEADING <SP>S
PHDC1:	MVI	C,0	; SET COUNT
PHDC2:	MOV	A,L	; SUB E FROM L
	SUB	E
	MOV	L,A	; RESULT IN L
	MOV	A,H	; SUB D FROM H W/BORROW
	SBB	D
	MOV	H,A	; RESULT IN H
	JC	PHDC3	; DONE IF CARRY SET (FURTHER BORROW)
	INR	C	; INCR COUNT
	JMP	PHDC2
PHDC3:	MOV	A,L	; ADD E TO L
	ADD	E
	MOV	L,A	; RESULT IN L
	MOV	A,H	; ADD D TO H W/CARRY
	ADC	D
	MOV	H,A	; RESULT IN H
	MOV	A,C	; GET RESULT
	ORA	A	; CHECK FOR ZERO
	JNZ	PHDC4
	LDA	LSFLG0	; CHECK FOR LEADING <SP>
	ORA	A	; PRINT VALUE IF NOT (A=0)
	JZ	PHDC4
	MVI	A,' '	; PRINT <SP>
	JMP	PUT
PHDC4:	XRA	A	; TURN OFF LEADING <SP>
	STA	LSFLG0
	MOV	A,C	; GET VALUE
	ADI	'0'	; CONVERT TO ASCII
PUT:
	PUSH	H	; SAVE HL
	LHLD	MEMPTR	; GET PTR
	MOV	M,A	; PUT BYTE
	INX	H	; PT TO NEXT
	SHLD	MEMPTR	; PUT PTR
	POP	H	; RESTORE HL
	RET

	END
