Voor een berekening zoek ik een converteer routine die een 3 bytes getal kan omzetten naar ca. 10 8 Digits (BCD getallen)
Liefst een routine zonder indirekt adressering.

Ik heb al gezocht en geprobeerd, maar kom er niet uit.

Ik heb een routine die een 2 bytes getal omzet in 5 digits (BCD).

Nu probeer ik deze uit te bereiden, maar dat is te hoog gegrepen voor mij.

Wie heeft er voor mij zo'n routine, kan de bestaande routine uitbreiden of kan mij hierbij hulpen ?


;** 2 BYTES --> BCD (0..9, 5 Digits) *******************************************

Bytes5bcd   SWAPF	  NumH,W     ;w  = A2*16+A3
            ANDLW   0x0F       ;w  = A3       PERSONALLY, I'D REPLACE THESE 2
            ADDLW   0xF0       ;w  = A3-16    LINES WITH "IORLW b'11110000B' " -AW
            MOVWF   Bcd01000   ;B3 = A3-16
            ADDWF   Bcd01000,F ;B3 = 2*(A3-16) = 2A3 - 32
            ADDLW   D'226'     ;w  = A3-16 - 30 = A3-46
            MOVWF   Bcd00100   ;B2 = A3-46
            ADDLW   D'50'      ;w  = A3-46 + 50 = A3+4
            MOVWF   Bcd00001   ;B0 = A3+4

            MOVF    NumH,W     ;w  = A3*16+A2
            ANDLW   0x0F       ;w  = A2
            ADDWF   Bcd00100,F ;B2 = A3-46 + A2 = A3+A2-46
            ADDWF   Bcd00100,F ;B2 = A3+A2-46  + A2 = A3+2A2-46
            ADDWF   Bcd00001,F ;B0 = A3+4 + A2 = A3+A2+4
            ADDLW   D'233'     ;w  = A2 - 23
            MOVWF   Bcd00010   ;B1 = A2-23
            ADDWF   Bcd00010,F ;B1 = 2*(A2-23)
            ADDWF   Bcd00010,F ;B1 = 3*(A2-23) = 3A2-69

            SWAPF   NumL,W     ;w  = A0*16+A1
            ANDLW   0x0F       ;w  = A1
            ADDWF   Bcd00010,F ;B1 = 3A2-69 + A1 = 3A2+A1-69 range -69...-9
            ADDWF   Bcd00001,F ;B0 = A3+A2+4 + A1 = A3+A2+A1+4 and Carry = 0

            RLF     Bcd00010,F ;B1 = 2*(3A2+A1-69) + C = 6A2+2A1-138 and Carry is 
                               ;now 1 as Bcd00010 register had to be negitive
            RLF     Bcd00001,F ;B0 = 2*(A3+A2+A1+4) + C = 2A3+2A2+2A1+9 (+9 not +8 
                               ;due to the carry from prev line)
            COMF    Bcd00001,F ;B0 = ~(2A3+2A2+2A1+9) = -2A3-2A2-2A1-10 (Bcd00001 
                               ;complement plus 1 is twos complement)
            RLF     Bcd00001,F ;B0 = 2*(-2A3-2A2-2A1-10) = -4A3-4A2-4A1-20

            MOVF    NumL,W     ;w  = A1*16+A0
            ANDLW   0x0F       ;w  = A0
            ADDWF   Bcd00001,F ;B0 = -4A3-4A2-4A1-20 + A0 = A0-4(A3+A2+A1)-20 range 
                               ;-215...-5 Carry=0
            RLF     Bcd01000,F ;B3 = 2*(2A3 - 32) = 4A3 - 64

            MOVLW   0x07       ;w  = 7
            MOVWF   Bcd10000   ;B4 = 7

            MOVLW   D'10'      ;w  = 10
Lb1:        ;do
            ADDWF   Bcd00001,F ;B0 += 10
            DECF    Bcd00010,F ;B1 -= 1
            BTFSS   3,0
	      ;skip no carry
            GOTO    Lb1         ;while B0 < 0
	      ;jmp carry
Lb2:		;do
            ADDWF   Bcd00010,F ;B1 += 10
            DECF    Bcd00100,F ;B2 -= 1
            BTFSS   3,0
            GOTO    Lb2        ;while B1 < 0
Lb3:		;do
            ADDWF   Bcd00100,F ;B2 += 10
            DECF    Bcd01000,F ;B3 -= 1
            BTFSS   3,0
            GOTO    Lb3        ;while B2 < 0
Lb4:		;do
            ADDWF   Bcd01000,F ;B3 += 10
            DECF    Bcd10000,F ;B4 -= 1
            BTFSS   3,0
            GOTO    Lb4        ;while B3 < 0

            RETLW   0

Parallel in hardware zou het 32 IC's kosten...
(74184)

Ik kom niet verder dan een deling door 1000000,
de rest delen door 100000,
de rest delen door 10000,

Etc..

Je komt niet verder dan 8 digits.
De makkelijkste implementatie is door steeds af te trekken: het aantal keer dan 10000000 lukt staat in de eerste digit. Daarna het aantal keer dat 1000000 lukt in de 2de etc.
Geen idee hoe je dat in een pic-ding frut, maar 3 bytes substract moet niet zo spannend hoeven zijn.

Inderdaad 8 digits, tekst gewijzigd.

Veel delingen kosten erg veel tijd.

Daarom probeer ik bovenstaande routine met één ingangs byte uit te breiden.
Helaas is dat (voor mij) nog niet zo makkelijk...

Zijn die pic-dingen zo traag?
Aftrekken is toch bloedsnel?

Delingen met meerdere byte's (dus lange getallen) kost best wel wat tijd.

De bovenstaande routine werkt lekker snel (zonder delingen), maar helaas (nog) niet geschikt voor 3 byte's...

Op 8 november 2007 21:48:15 schreef Marco69:
Zijn die pic-dingen zo traag?
Aftrekken is toch bloedsnel?

Het gebruikte algoritme is slimmer dan "aftrekken in een loopje" en daardoor een stuk sneller, maar daardoor niet uit te breiden van 16 naar 32 bit.
Ik kan de uitleg er wel even bijgeven (die had de TS weggelaten) >:)

If you have a 4 digit hexadecimal number, it may be written as

N = a_3*16^3 + a_2*16^2 + a_1*16 + a_0

where a_i, i=0,1,2,3 are the four hexadecimal digits. If you 
wish to convert this to decimal, then you need to express N as

N = b_4*10^4 + b_3*10^3 + b_2*10^2 + b_1*10 + b_0

Where b_j, j=0,1,2,3,4 are the five decimal digits.
The reason there are 5 digits in the decimal representation
is because the maximum four digit hexadecimal number (0xffff)
requires 5 decimal digits (65535). Now the goal is to find
a set of equations that allow the b_j's to be expressed in
terms of the a_i's. There are infinitely many ways to do this.
Here are two of probably the simplest expressions.
  First, expand the 16^i's and then collect all coefficients
of the 10^j's:

N = a_3*4096 + a_2*256 + a_1*16 + a_0
  = a_3*4*10^3  + a_2*2*10^2 + (a_3*9 + a_2*5 + a_1)*10 +
    6*(a_3 + a_2 + a_1) + a_0


This gives us five equations:

b_0 = 6*(a_3 + a_2 + a_1) + a_0
b_1 = a_3*9 + a_2*5 + a_1
b_2 = 2*a_2
b_3 = 4*a_3
b_4 = 0

Which as John says, must be "normalized". Normalization in
this context means we need to reduce the b_j's such that
  0 <= b_j <= 9

In other words we need to find:

c_0 = b_0 mod 10

b_1 = (b_1 + (b_0 - c_0)/10)
c_1 =  b_1 mod 10

b_2 = (b_2 + (b_1 - c_1)/10) 
c_2 = b_2 mod 10

b_3 = (b_3 + (b_2 - c_2)/10)
c_3 = b_3 mod 10

c_4 = (b_4 + (b_3 - c_3)/10) mod 10
    = (b_2 - c_2)/10

Division by 10 can be done quite efficiently (as was shown
in another thread several months ago). However, it does require
a significant amount of code space compared to say repeated
subtractions. Unfortunately, there can be very many subtractions
that are required. For example, b_1 could be as large as 15*16,
or 240. 10 would have to be subtracted 24 times if you wish to
compute 240 mod 10. I presume John realized this inefficiency
and thus sought to express N so that repeated subtractions
could be used and that the total number of subtractions are
minimized. This leads to the next way that N can be expressed:

N = a_3*(4100 - 4) + a_2*(260 - 4) + a_1*(20-4) + a_0
  = 4*a_3*10^3 + (a_3 + 2*a_2)*10^2 + (6*a_2 + 2*a_1)*10 +
    a_0 - 4*(a_3 + a_2 + a_1)

This gives five new equations for the b_j's. 

b_0 = a_0 - 4*(a_3 + a_2 + a_1)
b_1 = 6*a_2 + 2*a_1
b_2 = a_3 + 2*a_2
b_3 = 4*a_3
b_4 = 0

However, these equations are still not conducive to the 
repeated subtraction algorithm, at least the way John has 
done it. In other words, it is possible to pre-condition 
each of the b_j's so that they are less than zero. Then 
the repeated subtractions can simultaneously perform the
"mod 10" and "/10" operations shown above. Consider the
equation b_0 for example, 

b_0 = a_0 - 4*(a_3 + a_2 + a_1)

Since each a_i must satisfy:  0 <= a_i <= 15, then b_0 
ranges:

 -60 <= b_0 <= 15

We can make b_0 negative by subtracting any number greater than
15. A logical choice is 20. This is because if we subtract 20
from b_0, we can add 2 to b_1 to keep the net result the same.
The reason we add "2" can be seen:

 b_1*10 + b_0 = b_1*10 + b_0 + 20 - 20
              = (b_1 + 2)*10 + b_0 - 20

Carrying this concept out for the rest of the b_i's we have.

b_0 = a_0 - 4*(a_3 + a_2 + a_1) - 20
b_1 = 6*a_2 + 2*a_1 + 2 - 140
    = 6*a_2 + 2*a_1 - 138
b_2 = a_3 + 2*a_2 + 14 - 60
    = a_3 + 2*a_2 - 46
b_3 = 4*a_3 + 6 - 70
    = 4*a_3 - 64
b_4 = 0 + 7
    = 7

Jammer dat dat niet kan (3 bytes dus 24 bits is genoeg).

Heb/weet jij een goede oplossing of heb je een stuk goed werkende code ?

Op 8 november 2007 22:02:44 schreef MMSoft:
Delingen met meerdere byte's (dus lange getallen) kost best wel wat tijd.

Zit je dan zo krap in je timing? Je praat hier over een paar bytes. Een substract-with-carry (meer is het niet) kan toch zo spannend niet zijn.

Je hebt natuurlijk wel gelijk dat het sneller en efficienter kan (dat heeft waarschijnlijk al lang iemand verzonnen...). Daar staat tegenover dat het niet echt zinnig is om dat te doen als het ding daarna staat te idlen...
Als het voor de hobby is, kan ik je nog begrijpen. Voor 'werk' kosten uren inmiddels zoveel dat als een werkende, lompe maar betrouwbare oplossing 'good enough' is, je het vooral ook niet mooier moet gaan maken. Jammer, maar vaak toch de praktijk...

Een hapklaar stukje code heb ik niet voor je. Algorithme zoals beschreven moet ik wel ergens in zowel AVR als 8051 assembly hebben.

Zit je dan zo krap in je timing? Je praat hier over een paar bytes. Een substract-with-carry (meer is het niet) kan toch zo spannend niet zijn.

Tja, er zijn momenten dat de PIC het even erg druk heeft.
Deze draait al op 20 Mhz, dus de klok snelheid verhogen is geen optie.

Het werkt intussen...

Iedereen die mee gedacht heeft bedankt !