Searching \ for 'Ouch! More bin2bcd code -- how about a contest?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=ouch+more+bin2bcd
Search entire site for: 'Ouch! More bin2bcd code -- how about a contest?'.

Truncated match.
PICList Thread
'Ouch! More bin2bcd code -- how about a contest?'
1997\03\06@050616 by Frank A. Vostenbosch

flavicon
face
Ouch!  It seems that I accidentally posted my previous message twice to
the list, and it wasn't even the correct code.
Thing is, I generally use a lot of macros in my code to make it more
readable and less typing, i.e. STR rather than MOVWF.  These macros
make PIC assembly look more like other processors, but it of course
means that *you* can't assemble the code without problems.

So once more, here's my 32-bit binary to BCD code, which by the way
seems to be both shorter and faster than most of the *16-bit*
solutions I've seen.
40 instructions, two temporary registers needed and 2785 cycles.

If enough people ask, I'll write a 16-bit version as well, which
should run in about 1100 cycles.
Anyone care to try to do better than that?

; +---------------------------------------------------------------------+
; |                                                                     |
; |   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).

0000: 3020  bin2bcd         movlw   32
0001: 00A9                  movwf   Count
0002: 01A0                  clrf    dig4
0003: 01A1                  clrf    dig3
0004: 01A2                  clrf    dig2
0005: 01A3                  clrf    dig1
0006: 01A4                  clrf    dig0
0007: 1003                  bcf     status,status_carry
0008: 2810                  goto    B2B_Enter

0009: 3024  B2B_Loop        movlw   dig0
000A: 0084                  movwf   fsr
000B: 201C                  call    B2B_Adjust
000C: 201C                  call    B2B_Adjust
000D: 201C                  call    B2B_Adjust
000E: 201C                  call    B2B_Adjust
000F: 201C                  call    B2B_Adjust
0010: 0DA5  B2B_Enter       rlf     reg0,f
0011: 0DA6                  rlf     reg1,f
0012: 0DA7                  rlf     reg2,f
0013: 0DA8                  rlf     reg3,f
0014: 0DA4                  rlf     dig0,f
0015: 0DA3                  rlf     dig1,f
0016: 0DA2                  rlf     dig2,f
0017: 0DA1                  rlf     dig3,f
0018: 0DA0                  rlf     dig4,f
0019: 0BA9                  decfsz  Count,f
001A: 2809                  goto    B2B_Loop
001B: 0008                  return

001C: 3003  B2B_Adjust      movlw   3
001D: 0700                  addwf   ind,w
001E: 00AA                  movwf   Temp
001F: 19AA                  btfsc   Temp,3
0020: 0080                  movwf   ind
0021: 3030                  movlw   0x30
0022: 0700                  addwf   ind,w
0023: 00AA                  movwf   Temp
0024: 1BAA                  btfsc   Temp,7
0025: 0080                  movwf   ind
0026: 0384                  decf    fsr,f
0027: 0008                  return

Cheers,
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

1997\03\06@083821 by Hamilton Feltman

flavicon
face
Alright! Here's mine! It's on 7 bit though! Ha Ha. Sorry I just pasted it
out of my last program. I used it to return a number from 1 to 128, but the
tens column is from 0 to 12! I used the decimal point of the 10's column as
the '1' in the hundreds column. It is the subtraction method. I hope Jory
doesn't boot me off the list for posting trivial programs!

;   ---------------------------------------------------------------------
Split:  ; This fuction takes in an 8 bit number, in CurrentData1, and
               ; splits it up into two numbers for display: a 10's column and
               ; a 1's column. digit2 is 10's and digit1 is 1's after return

       movf    CurrentData1,w          ; mov to w. Currentdata1 is not changed
       addlw   0x01                    ; add 1 (midi)
       movwf   DisplayData     ; put in DisplayData
       movlw   0xff                    ;
       movwf   Counter         ; preload Counter with 0xff
       movlw   0x0a                    ; preload w with 0x0a
Loop1:

       incf    Counter,f                       ; start counting
       subwf   DisplayData,f   ; DisplayData - 10

       btfsc   _carry          ; test if below zero
        goto   Loop1           ; no, go back to Loop1

       addwf   DisplayData,w   ; yes, add 10 back on, DisplayData = Digit1
       movwf   Digit1

       movf    Counter,w                       ; Counter = Digit2
       movwf   Digit2

       bsf     _DontSplit
       return
;  ---------------------------------------------------------------------


Hamilton Feltman
spam_OUThamiltonTakeThisOuTspambudhi.com
Programmer and Sound Engineer
|_     _||_ .    _  _  _ _
|_)(_)(_|| )| . (_ (_)( | )
http://www.budhi.com

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