Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.sys.apple2.programmer > #2219 > unrolled thread

printf for 65c02

Started bywssimms@gmail.com
First post2016-02-13 20:29 -0800
Last post2016-02-18 13:37 -0800
Articles 20 on this page of 38 — 7 participants

Back to article view | Back to comp.sys.apple2.programmer


Contents

  printf for 65c02 wssimms@gmail.com - 2016-02-13 20:29 -0800
    Re: printf for 65c02 wssimms@gmail.com - 2016-02-13 20:31 -0800
      Re: printf for 65c02 wssimms@gmail.com - 2016-02-13 20:36 -0800
        Re: printf for 65c02 Antoine Vignau <antoine.vignau@laposte.net> - 2016-02-13 23:22 -0800
          Re: printf for 65c02 Michael Barry <barrym95838@yahoo.com> - 2016-02-15 22:57 -0800
            Re: printf for 65c02 wssimms@gmail.com - 2016-02-16 01:54 -0800
              Re: printf for 65c02 "Bill Garber" <willy46pa@comcast.net> - 2016-02-16 21:11 -0500
                Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-18 16:03 -0800
                  Re: printf for 65c02 wssimms@gmail.com - 2016-02-18 18:07 -0800
                  Re: printf for 65c02 Michael J. Mahon <mjmahon@aol.com> - 2016-02-19 03:01 -0600
                    Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-19 08:04 -0800
                      Re: printf for 65c02 qkumba <peter.ferrie@gmail.com> - 2016-02-19 10:05 -0800
                        Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-20 00:37 -0800
                          Re: printf for 65c02 qkumba <peter.ferrie@gmail.com> - 2016-02-20 09:46 -0800
                            Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-20 11:35 -0800
                        Re: printf for 65c02 wssimms@gmail.com - 2016-02-20 04:40 -0800
                          Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-20 08:14 -0800
                            Re: printf for 65c02 Michael J. Mahon <mjmahon@aol.com> - 2016-02-22 14:13 -0600
                          Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-20 10:52 -0800
                          Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-21 00:53 -0800
                            Re: printf for 65c02 wssimms@gmail.com - 2016-02-25 09:26 -0800
                              Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-25 12:36 -0800
          Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-18 07:38 -0800
    Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-16 15:27 -0800
      Re: printf for 65c02 wssimms@gmail.com - 2016-02-16 18:48 -0800
        Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-16 19:13 -0800
          Re: printf for 65c02 wssimms@gmail.com - 2016-02-17 07:49 -0800
          Re: printf for 65c02 wssimms@gmail.com - 2016-02-17 07:55 -0800
      Re: printf for 65c02 wssimms@gmail.com - 2016-02-16 18:55 -0800
        Re: printf for 65c02 wssimms@gmail.com - 2016-02-16 18:58 -0800
        Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-18 13:14 -0800
          Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-18 13:27 -0800
            Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-18 15:09 -0800
              Re: printf for 65c02 wssimms@gmail.com - 2016-02-18 17:58 -0800
                Re: printf for 65c02 Michael Barry <barrym95838@yahoo.com> - 2016-02-19 17:52 -0800
                  Re: printf for 65c02 wssimms@gmail.com - 2016-02-19 18:09 -0800
                    Re: printf for 65c02 Michael J. Mahon <mjmahon@aol.com> - 2016-02-20 00:27 -0600
    Re: printf for 65c02 Michael Pohoreski <michael.pohoreski@gmail.com> - 2016-02-18 13:37 -0800

Page 1 of 2  [1] 2  Next page →


#2219 — printf for 65c02

Fromwssimms@gmail.com
Date2016-02-13 20:29 -0800
Subjectprintf for 65c02
Message-ID<39e3480b-9493-4911-9b4d-550d8240e149@googlegroups.com>
I just posted 65c02 code on comp.sys.apple2 that calls printf.
I thought that might pique someone's interest, so here is my printf
for 65c02.

Some notes:
* it uses an external routine putchar, which can equated or linked
   however you like
* it uses an external argument stack. code calling printf pushes the
   16 bit arguments from right to left onto the stack. printf itself
   retrieves the stack pointer using the external function getsp and
   after stepping through the arguments, pops them all using the
   external function setsp. Needless to say, this argument stack is
   not intended to be the 65c02 hardware stack, although I guess
   it could be, if you like to live dangerously.
* the floating point format specifiers are accepted, but not imple-
   mented.
* I ripped this code out of a larger project for posting, so I had to
   make some small modifications. The code is thus, as posted,
   not tested, but the modifications were quite minor and everything
   should be correct.
* As posted, the code is about 1kb in size.

The code:

 
               xc
               rel
               use   sys
               dsk   printf.l

getsp          ext
setsp          ext
putchar        ext

bcd            ds    6
nval           dw    0

printd
               stx   nval
               sty   nval+1
               stz   bcd
               stz   bcd+1
               stz   bcd+2
               ldx   #16
               sed
:0a
               asl   nval
               rol   nval+1
               lda   bcd
               adc   bcd
               sta   bcd
               lda   bcd+1
               adc   bcd+1
               sta   bcd+1
               lda   bcd+2
               adc   bcd+2
               sta   bcd+2
               dex
               bne   :0a
               cld
               ldx   #2
               ldy   #5
:0b            lda   bcd,x
               lsr
               lsr
               lsr
               lsr
               clc
               adc   #'0'
               sta   bcd,y
               dey
               lda   bcd,x
               and   #$0F
               clc
               adc   #'0'
               sta   bcd,y
               dey
               dex
               bpl   :0b
               rts

printx
               stx   nval
               sty   nval+1
               ldx   #0
:1             lda   nval
               and   #15
               cmp   #10
               bcc   :2
               adc   #6
:2             adc   #'0'
               sta   bcd,x
               lsr   nval+1
               ror   nval
               lsr   nval+1
               ror   nval
               lsr   nval+1
               ror   nval
               lsr   nval+1
               ror   nval
               inx
               cpx   #6
               bne   :1
               rts

printo
               stx   nval
               sty   nval+1
               ldx   #0
:1             lda   nval
               and   #7
               clc
               adc   #'0'
               sta   bcd,x
               lsr   nval+1
               ror   nval
               lsr   nval+1
               ror   nval
               lsr   nval+1
               ror   nval
               inx
               cpx   #6
               bne   :1
               rts

rjust          db    0
ndigit         db    0
width          db    0

printf         ent
               jsr   getsp
               stx   argptr+1
               sty   argptr+2
               stz   argidx
               jsr   nxtarg
               stx   pkfmt+1
               sty   pkfmt+2
loop
               jsr   fmtc
               cmp   #0
               beq   :1a
               cmp   #'%'
               beq   :2a
:3
               jsr   putchar
               bra   loop
:1a
               jsr   fixstk
               rts
:2a
               stz   rjust
               stz   ndigit
               jsr   pkfmt
               cmp   #'-'
               bne   :2b
               jsr   fmtc
               inc   rjust
:2b
               jsr   gnum
               ldx   nval
               stx   width
               stz   ndfnd
               cmp   #'.'
               bne   :1b
               jsr   gnum
               ldx   nval
               stx   ndigit
:1b
               sta   :1d+1
               stz   fbidx
               ldx   #0
:1c
               lda   swtab0,x
               beq   :1e
               inx
:1d            cmp   #0
               bne   :1c
               dex
               txa
               asl
               tax
               jmp   (swtab1,x)
:1e            lda   :1d+1
               bra   :3

swtab0
               db    'd','o','x','f'
               db    'e','c','s','l'
               db    'r',0

swtab1
               dw    decimal
               dw    octal
               dw    hex
               dw    float
               dw    scien
               dw    charac
               dw    string
               dw    logical
               dw    remote

decimal
               jsr   nxtarg
               tya
               bpl   logic1
               eor   #$FF
               sta   nval+1
               txa
               eor   #$FF
               inc
               sta   nval
               bne   :0
               inc   nval+1
:0             lda   #'-'
               ldy   fbidx
               inc   fbidx
               sta   fbuf,y
               ldx   nval
               ldy   nval+1
               bra   logic1

logical
               jsr   nxtarg
logic1
               jsr   printd
nm2bf          ldx   #5
               ldy   fbidx
:1a            lda   bcd,x
               cpx   ndigit
               bcc   :1d
               dex
               bmi   :1c
               cmp   #'0'
               bne   :1b
               bra   :1a
:1d            dex
:1b            sta   fbuf,y
               iny
               lda   bcd,x
               dex
               bpl   :1b
:1c            sta   fbuf,y
               iny
               sty   fbidx
               jmp   prbuf

charac
               jsr   nxtargb
               cmp   #0
               beq   :1a
               ldy   fbidx
               sta   fbuf,y
               inc   fbidx
:1a            jsr   nxtargb
               cmp   #0
               beq   :1b
               ldy   fbidx
               sta   fbuf,y
               inc   fbidx
:1b            jmp   prbuf

string
               jsr   nxtarg
               stx   :1a+1
               sty   :1a+2
               ldx   ndigit
               ldy   #0
:1a
               lda   $FFFF,y
               beq   :1b
               iny
               dex
               bne   :1a
:1b
               tya
               ldx   :1a+1
               ldy   :1a+2
               jmp   prstr

hex
               jsr   nxtarg
               jsr   printx
               jmp   nm2bf

octal
               jsr   nxtarg
               jsr   printo
               jmp   nm2bf

float
               ldx   ndigit
               lda   ndfnd
               jsr   pfloat
               jmp   prbuf

scien
               ldx   ndigit
               lda   ndfnd
               jsr   pscien
               jmp   prbuf

remote
               jsr   nxtarg
               stx   :1+1
               sty   :2+1
               jsr   fixstk
:1             ldx   #0
:2             ldy   #0
               stx   argptr+1
               sty   argptr+2
               stz   argidx
               jsr   nxtarg
               stx   pkfmt+1
               sty   pkfmt+2
               jmp   loop

prbuf
               lda   fbidx
               ldx   #<fbuf
               ldy   #>fbuf
prstr
               stx   :sptr+1
               sty   :sptr+2
               sta   nchar
               lda   width
               sec
               sbc   nchar
               sta   nxtra
               bcc   :1a
               beq   :1a
               ldx   rjust
               bne   :1a
:2a
               lda   #' '
               jsr   putchar
               dec   nxtra
               bne   :2a
:1a
               lda   nchar
               beq   :2b
:1b
:sptr          lda   $FFFF
               inc   :sptr+1
               bne   :sp1
               inc   :sptr+2
:sp1
               jsr   putchar
               dec   nchar
               bne   :sptr
:2b
               lda   nxtra
               bmi   :1c
               beq   :1c
:2c
               lda   #' '
               jsr   putchar
               dec   nxtra
               bne   :2c
:1c
               jmp   loop


nxtra          db    0
nchar          db    0
fbidx          db    0
fbuf           ds    128

gnum
               stz   ndfnd
               stz   nval
               stz   nval+1
:1
               jsr   fmtc
               sec
               sbc   #'0'
               cmp   #'*'-'0'
               bne   :2
               jsr   nxtarg
               stx   dval
               bra   :3
:2
               cmp   #10
               bcs   :4
               sta   dval
:3
               inc   ndfnd
* x10
               lda   nval
               sta   ntmp
               asl
               asl
               clc
               adc   ntmp
               asl
               clc
               adc   dval
               sta   nval
               bra   :1
:4
               adc   #'0'-1
               rts


dval           db    0
ntmp           db    0
ndfnd          db    0
argidx         db    0

pkfmt          lda   $FFFF
               rts

fmtc           jsr   pkfmt
               inc   pkfmt+1
               bne   :x
               inc   pkfmt+2
:x             rts

nxtarg         jsr   nxtargb
               tax
               jsr   nxtargb
               tay
               rts
nxtargb        ldy   argidx
argptr         lda   $FFFF,y
               inc   argidx
               bne   :0
               inc   argptr+2
:0             rts

fixstk
               clc
               lda   argptr+1
               adc   argidx
               tax
               lda   argptr+2
               adc   #0
               tay
               jsr   setsp
               rts

pfloat
               jsr   nxtarg
               jsr   nxtarg
               ldx   #$FF
:0             inx
               lda   flts,x
               sta   fbuf,x
               bne   :0
               stx   fbidx
               rts

pscien
               jsr   nxtarg
               jsr   nxtarg
               ldx   #$FF
:0             inx
               lda   scis,x
               sta   fbuf,x
               bne   :0
               stx   fbidx
               rts


flts           asc   '<float>'
               db    0
scis           asc   '<scien>'
               db    0

[toc] | [next] | [standalone]


#2220

Fromwssimms@gmail.com
Date2016-02-13 20:31 -0800
Message-ID<ffa2b85f-1b42-4812-abe4-745c1d8ab84f@googlegroups.com>
In reply to#2219
Oh yeah, it uses self-modifying code. Thus it isn't romable or reentrant.

[toc] | [prev] | [next] | [standalone]


#2221

Fromwssimms@gmail.com
Date2016-02-13 20:36 -0800
Message-ID<0c83b947-ce37-4b9e-b28a-bf2dfc2bce96@googlegroups.com>
In reply to#2220
Am Sonntag, 14. Februar 2016 13:31:58 UTC+9 schrieb wss...@gmail.com:
> Oh yeah, it uses self-modifying code. Thus it isn't romable or reentrant.

Oh yeah, it is based on an old unix version of printf written in PDP-11 assembly language, so it's not quite the same as standard printf. For example, %d prints integers as signed values, and %l prints integers as unsigned values.

[toc] | [prev] | [next] | [standalone]


#2222

FromAntoine Vignau <antoine.vignau@laposte.net>
Date2016-02-13 23:22 -0800
Message-ID<e6ec3b03-b706-4245-b6cd-a5082fce4ad5@googlegroups.com>
In reply to#2221
Oh yeah, nice work, congrats!
Oh yeah, where are the comments?
av

[toc] | [prev] | [next] | [standalone]


#2238

FromMichael Barry <barrym95838@yahoo.com>
Date2016-02-15 22:57 -0800
Message-ID<f55c38fb-5e25-4f95-9852-fc2af9433a24@googlegroups.com>
In reply to#2222
On Saturday, February 13, 2016 at 11:22:26 PM UTC-8, Antoine Vignau wrote:
> Oh yeah, nice work, congrats!
> Oh yeah, where are the comments?
> av

A comment would die of loneliness in that source.

Mike B.

[toc] | [prev] | [next] | [standalone]


#2239

Fromwssimms@gmail.com
Date2016-02-16 01:54 -0800
Message-ID<80543671-2679-4343-a266-ef13923cec44@googlegroups.com>
In reply to#2238
Am Dienstag, 16. Februar 2016 15:57:27 UTC+9 schrieb Michael Barry:
> On Saturday, February 13, 2016 at 11:22:26 PM UTC-8, Antoine Vignau wrote:
> > Oh yeah, nice work, congrats!
> > Oh yeah, where are the comments?
> > av
> 
> A comment would die of loneliness in that source.
> 
> Mike B.

Well actually there is one comment:

* x10 

:-D

Sorry about the lack of comments. However, it really is quite straightforward. There's nothing tricky in it.
If someone actually has an interest in reading and understanding it, and finds themselves stumped, I'll be happy to answer any questions.

[toc] | [prev] | [next] | [standalone]


#2248

From"Bill Garber" <willy46pa@comcast.net>
Date2016-02-16 21:11 -0500
Message-ID<YYSdnccg65QnS17LnZ2dnUU78VmdnZ2d@giganews.com>
In reply to#2239
<wssimms@gmail.com> wrote in message news:8054367
1-2679-4343-a266-ef13923cec44@googlegroups.com...
> Am Dienstag, 16. Februar 2016 15:57:27 UTC+9
> schrieb Michael Barry:
>> On Saturday, February 13, 2016 at 11:22:26 PM
>> UTC-8, Antoine Vignau wrote:
>>> Oh yeah, nice work, congrats!
>>> Oh yeah, where are the comments?
>>> av
>>
>> A comment would die of loneliness in that source.
>>
>> Mike B.
>
> Well actually there is one comment:
>
> * x10
>
> :-D
>
> Sorry about the lack of comments. However, it really
> is quite straightforward. There's nothing tricky in it.
> If someone actually has an interest in reading and
> understanding it, and finds themselves stumped, I'll
> be happy to answer any questions.

Even with good commenting, I would be hard-pressed to
understand it well enough to have a question to ask.

Bill Garber  * http://www.sepa-electronics.com *


[toc] | [prev] | [next] | [standalone]


#2282

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-18 16:03 -0800
Message-ID<6462ff1b-9f37-45b7-be70-ed3d40b7e7c9@googlegroups.com>
In reply to#2248
On Tuesday, February 16, 2016 at 6:11:08 PM UTC-8, Bill Garber wrote:
> Even with good commenting, I would be hard-pressed to
> understand it well enough to have a question to ask.

*ahem*

I'm curious to know how the hell he knew to loop 16 times in DECIMAL mode in PrintD ! :-)

printd

               ldx   #16 
               sed 
:0a 
               asl   nval
               rol   nval+1 
 
               lda   bcd 
               adc   bcd 
               sta   bcd 
 

[toc] | [prev] | [next] | [standalone]


#2286

Fromwssimms@gmail.com
Date2016-02-18 18:07 -0800
Message-ID<deb3c816-f078-414c-8a4d-ff218b605185@googlegroups.com>
In reply to#2282
Am Freitag, 19. Februar 2016 09:03:49 UTC+9 schrieb Michael Pohoreski:
> On Tuesday, February 16, 2016 at 6:11:08 PM UTC-8, Bill Garber wrote:
> > Even with good commenting, I would be hard-pressed to
> > understand it well enough to have a question to ask.
> 
> *ahem*
> 
> I'm curious to know how the hell he knew to loop 16 times in DECIMAL mode in PrintD ! :-)
> 
> printd
> 
>                ldx   #16 
>                sed 
> :0a 
>                asl   nval
>                rol   nval+1 
>  
>                lda   bcd 
>                adc   bcd 
>                sta   bcd

The input value is a 16-bit integer.

printd 
               stx   nval 		;16-bit input value in nval
               sty   nval+1
               stz   bcd 		;initialize the BCD output to zero
               stz   bcd+1 
               stz   bcd+2 
               ldx   #16		;loop once for each bit in nval
               sed 
:0a 
               asl   nval 
               rol   nval+1 	;get the high bit of nval in C
               lda   bcd 
               adc   bcd 		;double the bcd number by adding
               sta   bcd 		;it to itself
               lda   bcd+1 
               adc   bcd+1 	;the bit from nval is added to the
               sta   bcd+1 	;sum yielding bcd=bcd*2+C
               lda   bcd+2 
               adc   bcd+2 	;note: in decimal mode ADC produces a carry
               sta   bcd+2 	;if the high nybble of a sum exceeds 1001 (9)
               dex
               bne   :0a 
               cld 

[toc] | [prev] | [next] | [standalone]


#2291

FromMichael J. Mahon <mjmahon@aol.com>
Date2016-02-19 03:01 -0600
Message-ID<vdadnVL_IcJDRFvLnZ2dnUU7-emdnZ2d@giganews.com>
In reply to#2282
Michael Pohoreski <michael.pohoreski@gmail.com> wrote:
> On Tuesday, February 16, 2016 at 6:11:08 PM UTC-8, Bill Garber wrote:
>> Even with good commenting, I would be hard-pressed to
>> understand it well enough to have a question to ask.
> 
> *ahem*
> 
> I'm curious to know how the hell he knew to loop 16 times in DECIMAL mode in PrintD ! :-)
> 
> printd
> 
>                ldx   #16 
>                sed 
> :0a 
>                asl   nval
>                rol   nval+1 
>  
>                lda   bcd 
>                adc   bcd 
>                sta   bcd 
>  
> 

Because there are 16 binary bits to be shifted out and "shifted in" to the
BCD result. 

This is the "double dabble" method of converting binary to decimal (or any
other radix for which "add" is implemented):

Clear the output value, shift out the high bit, double the output value in
the target radix and add the 0/1 shifted out, repeat (except clearing)
until all bits shifted out. 
-- 
-michael - NadaNet 3.1 and AppleCrate II:  http://michaeljmahon.com

[toc] | [prev] | [next] | [standalone]


#2292

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-19 08:04 -0800
Message-ID<fabf0ec4-4815-4068-bb84-938df14b4ab8@googlegroups.com>
In reply to#2291
On Friday, February 19, 2016 at 1:01:19 AM UTC-8, mjm...@aol.com wrote:
> Michael Pohoreski 
> > On Tuesday, February 16, 2016 at 6:11:08 PM UTC-8, Bill Garber wrote:
> >> Even with good commenting, I would be hard-pressed to
> >> understand it well enough to have a question to ask.
> > 
> > *ahem*
> > 
> > I'm curious to know how the hell he knew to loop 16 times in DECIMAL mode in PrintD ! :-)
> > 
> > printd
> > 
> >                ldx   #16 
> >                sed 
> > :0a 
> >                asl   nval
> >                rol   nval+1 
> >  
> >                lda   bcd 
> >                adc   bcd 
> >                sta   bcd 
> >  
> > 
> 
> Because there are 16 binary bits to be shifted out and "shifted in" to the
> BCD result. 
> 
> This is the "double dabble" method of converting binary to decimal (or any
> other radix for which "add" is implemented):
> 
> Clear the output value, shift out the high bit, double the output value in
> the target radix and add the 0/1 shifted out, repeat (except clearing)
> until all bits shifted out. 
> -- 
> -michael - NadaNet 3.1 and AppleCrate II:  http://michaeljmahon.com

Ah-ha!  Thanks Sheldon & Michael -- this makes a little more sense now!

I've never actually seen this algo. before. Oh wow, it's actually on Wikipedia.
https://en.wikipedia.org/wiki/Double_dabble

I guess it has fallen out of favor with (modern) CPU's getting rid of BCD mode and them having a dedicated MUL/DIV instruction.

[toc] | [prev] | [next] | [standalone]


#2293

Fromqkumba <peter.ferrie@gmail.com>
Date2016-02-19 10:05 -0800
Message-ID<40da0f39-8458-4d4e-b52e-68350ff4d199@googlegroups.com>
In reply to#2292
tprintf minified...

               xc 
               rel 
               dsk  tprintf.l 

spopw          ext 
putchar        ext 

bcd            ds    6 
nval           dw    0 

printd 
               stx   nval 
               sty   nval+1 
               stz   bcd 
               stz   bcd+1 
               stz   bcd+2 
               ldy   #16 
               sed 
:0a            asl   nval 
               rol   nval+1 
               ldx   #1
:0b            lda   bcd-1, x
               adc   bcd-1, x
               sta   bcd-1, x
               inx
               txa
               eor   #3
               bne   :0b
               dey
               bne   :0a
               cld 
               ldy   #5 
:0c            lda   bcd-1,x 
               lsr 
               lsr 
               lsr 
               lsr 
               clc 
               adc   #'0' 
               sta   bcd,y 
               dey 
               lda   bcd-1,x 
               and   #$0F 
               adc   #'0' 
               sta   bcd,y 
               dey 
               dex 
               bne   :0c
               rts 

printx 
               stx   nval 
               sty   nval+1 
               ldx   #0 
:1             lda   nval 
               and   #15 
               cmp   #10 
               bcc   :2 
               adc   #6 
:2             adc   #'0' 
               sta   bcd,x 
               ldy   #4
:3             lsr   nval+1 
               ror   nval 
               dey
               bne   :3
               inx 
               cpx   #6 
               bne   :1 
               rts 


prbf           ldx   #5 
:1a            lda   bcd,x 

               dex 
               bmi   :1c
               cmp   #'0' 

               beq   :1a 
               inx 

:1b            lda   bcd,x 
:1c            jsr   putchar 
               dex 
               bpl   :1b

               jmp   loop 

tprintf        ent 
               jsr   spopw 
               stx   fmtc+1 
               sty   fmtc+2 

loop           jsr   fmtc 
               and   #$7F 
               beq   done 
               cmp   #'%' 
               bne   :1 
               jsr   fmtc 
               cmp   #'d' 
               beq   :1F 
               cmp   #'x' 
               beq   :2F 
               cmp   #'s' 
               beq   :3F 
:1             jsr   putchar 
               bra   loop 
* %d 
:1F            jsr   spopw 
               jsr   printd 
               bra   prbf 
* %x 
:2F            jsr   spopw 
               jsr   printx 
               bra   prbf 
* %s 
:3F            jsr   spopw 
               stx   :8+1 
               sty   :8+2 
               ldx   #0 
:8             lda   $FFFF,x 
               beq   loop 
               jsr   putchar 
               inx 
               bne   :8 
               inc   :8+2 
               bra   :8 
* 
fmtc           lda   $FFFF 
               inc   fmtc+1 
               bne   done 
               inc   fmtc+2 
done           rts 

[toc] | [prev] | [next] | [standalone]


#2313

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-20 00:37 -0800
Message-ID<3a68465d-011f-4a02-acd6-5b0eb7ec1ed1@googlegroups.com>
In reply to#2293
On Friday, February 19, 2016 at 10:05:18 AM UTC-8, qkumba wrote:
> tprintf minified...

I'm these Dec2BCD optimizations ... switched X & Y around in the last half @_b

_PrintDec:
        STA _nDecWidth
        JSR NxtArgYX

PrintDecYX:
        STX _val+0      ; may be tempting to move this to NxtArgYX
        STY _val+1      ; as XYtoVal but others call us

        STZ _bcd+0
        STZ _bcd+1
        STZ _bcd+2

Dec2BCD:
        LDX   #16       ; 16 bits
        SED             ; "Double Dabble"
_Dec2BCD:               ; https://en.wikipedia.org/wiki/Double_dabble
        ASL _val+0
        ROl _val+1

; --- begin ---
        LDY #$FD
@DoubleDabble:
        LDA _bcd-$FD,Y
        ADC _bcd-$FD,Y
        STA _bcd-$FD,Y
        INY
        BNE @DoubleDabble
; --- end ---

        DEX
        BNE _Dec2BCD
        CLD

BCD2Char:
; --- begin ---
        LDX #5      ; was Y
        DEY         ; $FF - $FD = 2
_BCD2Char:          ; NOTE: Digits are reversed!
        LDA _bcd-$FD,Y  ; __c???    _b_?XX  a_YYXX
; --- end ---
        LSR
        LSR
        LSR
        LSR
        CLC
        ADC #'0'+$80
; --- begin ---
        STA _bcd,X      ; __c??X    _b_YXX  aZYYXX
        DEX             ; was Y

        LDA _bcd-$FD,Y  ; __c??X    _b_YXX  aZYYXX
; --- end ---
        AND #$F
        CLC
        ADC #'0'+$80
; --- begin ---
        STA _bcd,X      ; __c?XX    _bYYXX  ZZYYXX
; --- end ---
        DEY
        DEX
        BPL _BCD2Char

[toc] | [prev] | [next] | [standalone]


#2318

Fromqkumba <peter.ferrie@gmail.com>
Date2016-02-20 09:46 -0800
Message-ID<db2ac5e5-051b-4933-9b7d-44618c7a017d@googlegroups.com>
In reply to#2313
> I'm these Dec2BCD optimizations ... switched X & Y around in the last half @_b

> BCD2Char:
> ; --- begin ---
>         LDX #5      ; was Y
>         DEY         ; $FF - $FD = 2
> _BCD2Char:          ; NOTE: Digits are reversed!
> ...
> DEY

This DEY is redundant if _BCD2Char is moved up by one line.

[toc] | [prev] | [next] | [standalone]


#2321

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-20 11:35 -0800
Message-ID<c5b76265-7407-40bb-9904-ee76c681f0b8@googlegroups.com>
In reply to#2318
On Saturday, February 20, 2016 at 9:46:26 AM UTC-8, qkumba wrote:

> This DEY is redundant if _BCD2Char is moved up by one line.

Whoops, guess I copy-pasted the old code, but yup, thanks!

[toc] | [prev] | [next] | [standalone]


#2315

Fromwssimms@gmail.com
Date2016-02-20 04:40 -0800
Message-ID<360bf5f9-2af6-46d7-a630-e85b6b4d3690@googlegroups.com>
In reply to#2293
Am Samstag, 20. Februar 2016 03:05:18 UTC+9 schrieb qkumba:
> tprintf minified...
>                xc 
>                rel 
>                dsk  tprintf.l 

Thanks. I've incorporated all of your changes into my source.
I wrote this code probably 8 or 9 months ago and I remember
thinking I wanted the loops unrolled at the time, but when I
now think of how I use this routine, the few cycles saved by
unrolling are completely inconsequential.

[toc] | [prev] | [next] | [standalone]


#2317

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-20 08:14 -0800
Message-ID<0fca2fb7-5ae8-435b-9df8-e604778d9ba5@googlegroups.com>
In reply to#2315
On Saturday, February 20, 2016 at 4:40:38 AM UTC-8, wss...@gmail.com wrote:
> Am Samstag, 20. Februar 2016 03:05:18 UTC+9 schrieb qkumba:
> > tprintf minified...
> >                xc 
> >                rel 
> >                dsk  tprintf.l 
> 
> Thanks. I've incorporated all of your changes into my source.
> I wrote this code probably 8 or 9 months ago and I remember
> thinking I wanted the loops unrolled at the time, but when I
> now think of how I use this routine, the few cycles saved by
> unrolling are completely inconsequential.

For printm I'm optimizing for size, `-Os` in gcc speak, so any byte savings are worth it.
Does anyone actually benchmark printing / printf on the 6502? :-)

[toc] | [prev] | [next] | [standalone]


#2354

FromMichael J. Mahon <mjmahon@aol.com>
Date2016-02-22 14:13 -0600
Message-ID<gMKdnc8qvYBr9lbLnZ2dnUU7-bkAAAAA@giganews.com>
In reply to#2317
Michael Pohoreski <michael.pohoreski@gmail.com> wrote:
> On Saturday, February 20, 2016 at 4:40:38 AM UTC-8, wss...@gmail.com wrote:
>> Am Samstag, 20. Februar 2016 03:05:18 UTC+9 schrieb qkumba:
>>> tprintf minified...
>>> xc 
>>> rel 
>>> dsk  tprintf.l 
>> 
>> Thanks. I've incorporated all of your changes into my source.
>> I wrote this code probably 8 or 9 months ago and I remember
>> thinking I wanted the loops unrolled at the time, but when I
>> now think of how I use this routine, the few cycles saved by
>> unrolling are completely inconsequential.
> 
> For printm I'm optimizing for size, `-Os` in gcc speak, so any byte savings are worth it.
> Does anyone actually benchmark printing / printf on the 6502? :-)
> 

Since practically any I/O device will be much slower than even a 1MHz
processor, optimizing for space, not speed, is almost always the right
answer. ;-)
-- 
-michael - NadaNet 3.1 and AppleCrate II:  http://michaeljmahon.com

[toc] | [prev] | [next] | [standalone]


#2320

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-20 10:52 -0800
Message-ID<ec05529a-7c28-4ce6-a7b4-68f35cf53daa@googlegroups.com>
In reply to#2315
If you don't care about the total decimal output width, i.e. you always output 6 digits "as-is", there is no need to store the output bytes into the bcd buffer and then reverse print THAT.  Just print'em as they get processed. :-)

        LDX #5          ; was Y
@BCD2Char:              ; NOTE: Digits are reversed!
        DEY             ; $FF - $FD = 2
        LDA _bcd-$FD,Y  ; __c???   _b_?XX   a_YYXX
        LSR
        LSR
        LSR
        LSR
        CLC
        ADC #'0'+$80
        JSR COUT        ; __c??X   _b_YXX   aZYYXX
        DEX
        LDA _bcd-$FD,Y  ; __c??X   _b_YXX   aZYYXX
        AND #$F
        CLC
        ADC #'0'+$80
        JSR COUT        ; __c?XX   _bYYXX   ZZYYXX
        DEX
        BPL @BCD2Char

[toc] | [prev] | [next] | [standalone]


#2334

FromMichael Pohoreski <michael.pohoreski@gmail.com>
Date2016-02-21 00:53 -0800
Message-ID<9a454219-ca12-4a86-aa19-510b6ad286c9@googlegroups.com>
In reply to#2315
On Saturday, February 20, 2016 at 4:40:38 AM UTC-8, wss...@gmail.com wrote:
> Thanks. I've incorporated all of your changes into my source.
> I wrote this code probably 8 or 9 months ago and I remember
> thinking I wanted the loops unrolled at the time, but when I
> now think of how I use this routine, the few cycles saved by
> unrolling are completely inconsequential.

This may be of interest:  

I optimized PrintDec so that it supports a variable width output AND doesn't need to do a "reverse print" using the bcd buffer.  The trick was figuring out how to print out one nibble when the width is odd. :-)  Solution was to not store the character width but use the bcd offset of where to start printing from, which is width/2.

I was able to factor out the common code for PrintHex and PrintDec since they both need a HexToChars functionality.  (See version 37 in the printm thread for the full details.)


    .if USE_DEC_5
        PrintDec5:
                LDA #5/2        ; offset into _bcd buffer
        .if USE_DEC_2 || USE_DEC_3
                db $2C          ; BIT $abs skip next instruction
        .endif
    .endif

    .if USE_DEC_3
        PrintDec3:
                LDA #3/2        ; offset into bcd buffer
        .if USE_DEC_2
                db $2C          ; BIT $abs skip next instruction
        .endif
    .endif

    .if USE_DEC_2
        PrintDec2:
                LDA #0          ; special: print 2 digits
    .endif

        _PrintDec:
                STA _nDecWidth
                JSR NxtArgYX    ; stores YX in _temp

        PrintDecYX:
                STZ _bcd+0
                STZ _bcd+1
                STZ _bcd+2

        Dec2BCD:
                LDX   #16       ; 16 bits
                SED             ; "Double Dabble"
        @Dec2BCD:               ; https://en.wikipedia.org/wiki/Double_dabble
                ASL _temp+0
                ROL _temp+1

                LDY #$FD        ; $00-$FD=-3 bcd[0] bcd[1] bcd[2] bcd[3]
        @DoubleDabble:          ;              Y=FD   Y=FE   Y=FF   Y=00
                LDA _bcd-$FD,Y
                ADC _bcd-$FD,Y
                STA _bcd-$FD,Y
                INY
                BNE @DoubleDabble

                DEX
                BNE @Dec2BCD
                CLD

        DecWidth:
                LDY #3          ; default to 6 digits
                BEQ @EvenBCD    ; special case 0 -> only 2 digits
                                ; otherwise have odd digits,
                                ; Print low nibble, skip high nibble
        @OddBCD:                ; Y = num digits/2 to print
                LDA _bcd,Y      ; __c???   _b_?XX   a_YYXX
                JSR HexA
                JSR PutChar
                DEY
        @EvenBCD:
                LDA _bcd,Y      ; __c???   _b_?XX   a_YYXX
                JSR PrintHexByte
                DEY
                BPL @EvenBCD

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.sys.apple2.programmer


csiph-web