1;----------------------------------------------------------------------
2; Floating Point Library for 6502
3;----------------------------------------------------------------------
4; (C)1978 Microsoft
5
6n0999	.byt $9b,$3e,$bc,$1f,$fd
7n9999	.byt $9e,$6e,$6b,$27,$fd
8nmil	.byt $9e,$6e,$6b,$28,$00
9
10fout
11	ldy #1
12foutc
13	lda #' '	;if positive, print space
14	bit facsgn
15	bpl fout1
16	lda #'-'	;if neg
17fout1
18	sta fbuffr-1,y	;store the character.
19	sta facsgn	;make fac pos for qint.
20	sty fbufpt	;save for later
21	iny
22	lda #'0'	;get zero to type if fac = 0
23	ldx facexp
24	bne *+5
25	jmp fout19
26
27	lda #0
28	cpx #$80	;is number < 1?
29	beq fout37	;no
30	bcs fout7
31fout37
32	lda #<nmil	;mult by 10~6
33	ldy #>nmil
34	jsr fmult
35	lda #$f7
36fout7
37	sta deccnt	;save count or zero it.
38fout4
39	lda #<n9999
40	ldy #>n9999
41	jsr fcomp	;is number .gt. 999999.499?
42			;or 999999999.5?
43	beq bigges
44	bpl fout9	;yes, make it smaller.
45fout3
46	lda #<n0999
47	ldy #>n0999
48	jsr fcomp	;is number .gt.99999,9499?
49			;or 99999999.90625?
50	beq fout38
51	bpl fout5	;yes. done multiplying.
52fout38
53	jsr mul10	;make it bigger.
54	dec deccnt
55	bne fout3	;see if taht does it.
56fout9			;this always goes.
57	jsr div10	;make it smaller.
58	inc deccnt
59	bne fout4	;see if that does it.
60fout5			;this always goes.
61	jsr faddh	;add a half to round up.
62bigges
63	jsr qint
64	ldx #1		;decimal point count.
65	lda deccnt
66	clc
67	adc #$0a	;should number be printed in E notation?
68			;(ie, is number .lt. .01?)
69	bmi foutpi	;yes.
70	cmp #$0b	;is it .gt. 999999 (9999999999)?
71	bcs fout6	;yes, use E notation.
72	adc #$ff	;number of palces before decimal point.
73	tax		;put into accx.
74	lda #2		;no E notation.
75foutpi
76	sec
77fout6
78	sbc #2		;effectively add 5 to orig exp.
79	sta tenexp	;that is the exponent to print.
80	stx deccnt	;number of decimal places.
81	txa
82	beq fout39
83	bpl fout8	;some places before dec pnt.
84fout39
85	ldy fbufpt	;get pointer to output.
86	lda #'.'	;put in "."
87	iny
88	sta fbuffr-1,y
89	txa
90	beq fout16
91	lda #'0'	;get the ensuing zero.
92	iny
93	sta fbuffr-1,y
94fout16
95	sty fbufpt	;save it for later.
96fout8
97	ldy #0
98foutim
99	ldx #$80	;first pass through ,accb has msb set.
100fout2
101	lda faclo
102	clc
103	adc foutbl+3,y
104	sta faclo
105	lda facmo
106	adc foutbl+2,y
107	sta facmo
108	lda facmoh
109	adc foutbl+1,y
110	sta facmoh
111	lda facho
112	adc foutbl,y
113	sta facho
114	inx		;it was done yet another time.
115	bcs fout41
116	bpl fout2
117	bmi fout40
118
119fout41
120	bmi fout2
121fout40
122	txa
123	bcc foutyp	;can use acca as is.
124	eor #$ff	;find 11.(a).
125	adc #$0a	;c is still on to complete negation.
126			;and will always be on after.
127foutyp
128	adc #$2f	;get a character to print.
129	iny
130	iny
131	iny
132	iny
133	sty fdecpt
134	ldy fbufpt
135	iny		;point to place to store output.
136	tax
137	and #$7f	;get rid of msb.
138	sta fbuffr-1,y
139	dec deccnt
140	bne stxbuf	;not time for dp yet.
141	lda #'.'
142	iny
143	sta fbuffr-1,y	;store dp.
144stxbuf
145	sty fbufpt	;store pointer for later.
146	ldy fdecpt
147	txa		;complement accb.
148	eor #$ff	;complement acca.
149	and #$80	;save only msb.
150	tax
151	cpy #fdcend-foutbl
152	bne fout2	;continue with output.
153fouldy
154	ldy fbufpt      ;get back output pointer.
155fout11
156	lda fbuffr-1,y	;remove trailing blanks.
157	dey
158	cmp #'0'
159	beq fout11
160	cmp #'.'
161	beq fout12	;run into dp. stop.
162	iny		;something else, save it.
163fout12
164	lda #'+'
165	ldx tenexp
166	beq fout17	;no exponent to output.
167	bpl fout14
168	lda #0
169	sec
170	sbc tenexp
171	tax
172
173