;
; SYSLIB Module Name:  SMADC
; Author:  Richard Conn
; SYSLIB Version Number:  2.0
; Module Version Number:  1.0
; Module Entry Points:
;	MA3DC		MADC
; Module External References:
;	None
;
;
;  MA3DC --
;	STORE REG A AS 3 DECIMAL CHARACTERS IN MEMORY
; ON INPUT, A=VALUE AND DE=PTR TO 3-BYTE BUFFER
; ON RETURN, DE PTS TO BYTE AFTER BUFFER
;

MA3DC::
	XCHG		; PTR IN HL
	PUSH	PSW	; SAVE A
	PUSH	PSW
	XRA	A	; TURN OFF LEADING <SP> FLAG
	STA	LSFLG
	JMP	PADC1
;
;  MADC --
;	STORE REG A AS DECIMAL CHARACTERS W/LEADING <SP> IN 3-CHAR FIELD
; IN MEMORY; ON INPUT, A=VALUE AND HL=PTR TO 3-BYTE BUFFER
; ON RETURN, HL PTS TO BYTE AFTER BUFFER
;
MADC::
	XCHG		; PTR IN HL
	PUSH	PSW	; SAVE A
	PUSH	PSW
	MVI	A,1	; TURN ON LEADING <SP> FLAG
	STA	LSFLG
;  PRINT ROUTINE
PADC1:	POP	PSW	; GET A
	PUSH	B	; SAVE BC
	MVI	B,100	; PRINT HUNDREDS
	CALL	PAC	; PRINT A CHAR
	MVI	B,10	; PRINT TENS
	CALL	PAC
	ADI	'0'	; CONVERT TO ASCII
	CALL	PUT	; PRINT
	POP	B	; RESTORE BC
	POP	PSW	; RESTORE A
	XCHG		; PTR BACK IN DE
	RET

;  BUFFER
LSFLG:	DS	1	; LEADING <SP> FLAG

;  PRINT RESULT OF DIVISION OF A BY B W/LEADING <SP> (INTEGER DIVISION)
PAC:	MVI	C,0	; SET COUNT
PACL:	SUB	B	; COMPUTE COUNT
	JC	PACD
	INR	C	; INCR COUNT
	JMP	PACL
PACD:	ADD	B	; ADD B BACK IN
	PUSH	PSW	; SAVE A
	MOV	A,C	; GET COUNT
	ORA	A	; ZERO?
	JNZ	PACD1
	LDA	LSFLG	; CHECK FOR LEADING <SP>
	ORA	A	; 0 MEANS NO
	JZ	PACD1
	MVI	A,' '	; PRINT <SP>
	CALL	PUT
	POP	PSW	; RESTORE A
	RET
PACD1:	XRA	A	; A=0
	STA	LSFLG	; TURN OFF LEADING <SP> FLAG
	MOV	A,C	; GET COUNT
	ADI	'0'	; CONVERT TO DECIMAL
	CALL	PUT	; PRINT IT
	POP	PSW	; RESTORE A
	RET
PUT:
	MOV	M,A	; PUT BYTE
	INX	H	; PT TO NEXT
	RET

	END
