Searching \ for 'All those binary-bcd converting routines, here's m' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=all+those+binary
Search entire site for: 'All those binary-bcd converting routines, here's m'.

Truncated match.
PICList Thread
'All those binary-bcd converting routines, here's m'
1997\03\06@075447 by Frank A. Vostenbosch

flavicon
face
On Wed 4 Mar, Steve Hardy wrote:
> > Finally I do welcome all input.  Especially if someone can improve on the
> > speed/space of this one.
>
> DAA is a bit tekno for this task.  My philosophy when it comes to
> binary to decimal decoding is that decimal is almost always used
> by humans (or printers) and hence speed is not an overriding concern.
> I would go for the most compact code.  Successive subtraction of
> powers of ten would be my first choice.

That is quite simple to implement, but _you_can_do_better_, in speed as
well as storage terms.  Here's one I wrote earlier -- it converts a 32-bit
integer into 10 packed BCD digits.  Amazingly (well, don't tell your
girlfriend because she won't be impressed) the number of cycles it takes
is always the same, namely 2785 cycles (778us running at the all-time-
favourite 3.57MHz).
No particular copyright on this code.

; +---------------------------------------------------------------------+
; |                                                                     |
; |   Binary to BCD conversion.                                         |
; |                                                                     |
; +---------------------------------------------------------------------+
;
; In:   reg3,reg2,reg1,reg0 : 32-bit integer
; Act:  Converts a 32 bit binary number to packed BCD
; Out:  dig4,dig3,dig2,dig1,dig0 : BCD result (must be in increasing address
order)
; Regs: Temp, Count
; Note: Uses one stack level internally.
;       Execution time is 2785 cycles (1948 cycles using in-line code for
B2B_Adjust).

bin2bcd         movlw   32
               str     Count
               clr     dig4
               clr     dig3
               clr     dig2
               clr     dig1
               clr     dig0
               clc
               jp      B2B_Enter

B2B_Loop        movlw   dig0
               str     fsr
               call    B2B_Adjust
               call    B2B_Adjust
               call    B2B_Adjust
               call    B2B_Adjust
               call    B2B_Adjust
B2B_Enter       rl      reg0
               rl      reg1
               rl      reg2
               rl      reg3
               rl      dig0
               rl      dig1
               rl      dig2
               rl      dig3
               rl      dig4
               loop    Count,B2B_Loop
               return

B2B_Adjust      movlw   3
               addwf   ind,w
               str     Temp
               btfsc   Temp,3
               str     ind
               movlw   0x30
               addwf   ind,w
               movwf   Temp
               btfsc   Temp,7
               str     ind
               dec     fsr     ; dig0 must be at highest address
               return

; -----------------------------------------------------------------------

Enjoy,
Frank

------------------------------------------------------------------------
Frank A. Vorstenbosch                           Phone:  +44-181-941 7899
Electronics & Software Engineer                 Mobile:  +44-976-430 569
Eidos Technologies Ltd., Hampton, Middx         Fax:    +44-181-941 7895

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...