
*
* decst
*
* Convert the number in D into an ascii string
* representing a decimal number.  The string
* is pointed at by X (X should be set on entry!)
* Also on exit, D has the length of the string.
* The second entry point, 'decstn' will not print
* leading spaces for leading zeroes, but will
* suppress all leading zero info.  All strings
* start with a space.
*

decst pshs x save user pointer
 clr 0,-s set suppression flag
 bra decst2
*
decstn pshs x save user pointer
 clr 0,-s
 inc 0,s set for no fielding
decst2 pshs a
 lda #$20 setup leading space
 sta 0,x+ save in buffer
 puls a
 clr 0,-s set up bookkeeping
 clr 0,-s
 ldy #conlst point to constants
decst4 cmpd 0,y compare number to constant
 blo decst5
 subd 0,y do subtraction of constant
 inc 1,s bump digits counter
 bra decst4
decst5 pshs a save number
 tst 2,s zero digit?
 bne decst6
 tst 1,s any numbers output yet?
 bne decst6
 tst 3,s doing suppression?
 bne decst8
 lda #$20 set up space
 bra decst7
decst6 lda 2,s get digit count
 inc 1,s set 'got one' flag
 ora #$30 make ascii
decst7 sta 0,x+ save in buffer
decst8 puls a reset number
 clr 1,s clear out digit
 leay 2,y bump constant ptr
 cmpy #conend end of list?
 bne decst4
 leas 3,s clean up stack
 orb #$30 make last digit
 stb 0,x+ save in buffer
 clr 0,x null terminate string
 tfr x,d
 subd 0,s calculate string length
 puls x,pc return

* constants for convert

conlst fdb 10000
 fdb 1000
 fdb 100
 fdb 10
conend equ * end of list

 pag

* inch

inch pshs b,x
 ldx inchp get in pointer
inch1 cmpx inche end of buffer?
 bne inch4
 ldd infd get file desc
 sys read,inchb,512
 bes inch8
 cmpd #0 eof?
 beq inch6
 ldx #inchb reset ptrs
 stx inchp
 leax d,x set buffer end
 stx inche
 ldx inchp
inch4 lda 0,x+ get character
 stx inchp
 sez set status
 puls b,x,pc return
inch6 lda #$ff set eof
 puls b,x,pc return
inch8 tfr b,a get error
 puls b,x,pc return

* outch

outch pshs d,x
 ldx outchp
 cmpx #outch+512 end of buffer
 bne outch4
 ldd outfd get file desc
 sys write,outchb,512
 ldx #outchb reset ptr
outch4 lda 0,s get char
 sta 0,x+ place in buffer
 stx outchp save ptr
 puls d,x,pc return

* flush output

flusho ldd outchp get count
 subd #outchb
 beq flush2
 std iwrt2 save count in call
 ldd outfd get file desc
 sys ind,iwrt
flush2 rts return

* init io stuff

ioinit ldx #inchb set ptrs
 stx inchp
 stx inche
 ldx #outchb
 stx outchp
 stx outche
 ldd #0
 std infd set file desc
 ldd #1
 std outfd set output file desc
 rts return

