Searching \ for '[PIC]: INC/DEC 2 PACKED BCD NUMBERS' 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/microchip/devices.htm?key=pic
Search entire site for: 'INC/DEC 2 PACKED BCD NUMBERS'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: INC/DEC 2 PACKED BCD NUMBERS'
2005\08\01@140801 by luis

flavicon
face
Hi all,

I´m looking a rotine in asm to INC ou DEC 2 packed BCD numbers.
For example: 00000100 10011001 (499) + 1 = 00000101 00000000 (500)


Any help

Thanks in advanced.

Luis F.

2005\08\01@143927 by Jan-Erik Soderholm

face picon face
luis wrote :
> I´m looking a rotine in asm to INC ou DEC 2 packed BCD numbers.
> For example: 00000100 10011001 (499) + 1 = 00000101 00000000 (500)

What PIC series ?

Probably easier on PIC18-series
then on the "lower" series since
the PIC18 has an instruction set
that might help.

B.t.w, what have you tried so far ?

Regards,

Jan-Erik.




2005\08\01@145317 by luis

flavicon
face
Hi Jan,
I´m using a 16F series.
By the way, I need to INC and DEC a 2 packed numbers and no
INC ou DEC ok?
Luis F.


{Original Message removed}

2005\08\01@150326 by olin piclist

face picon face
Jan-Erik Soderholm wrote:
> Probably easier on PIC18-series
> then on the
> "lower" series since
> the PIC18 has an instruction set
> that might help.

Doesn't PIC 16 also have the DAW instruction and the DC bit?  I don't
remember since I've never used either.  Personally I think they are a waste
of silicon.  It's not worth burdening every chip with this stuff for the
very rare case when it comes in handy.  If you are doing BCD, it's hard to
argue you care about every last cycle of speed.


*****************************************************************
Embed Inc, embedded system specialists in Littleton Massachusetts
(978) 742-9014, http://www.embedinc.com

2005\08\01@150952 by Jan-Erik Soderholm

face picon face
> I´m using a 16F series.

Fine, so no BCD ("DAW") instruction then.

> By the way, I need to INC and DEC a
> 2 packed numbers and no INC ou DEC ok?

No, not very clear... :-)

Anyway, you have searched on the
piclist archive ? I bet there are
some BCD examples there....

Jan-Erik.


2005\08\01@151020 by Byron A Jeff

face picon face
On Mon, Aug 01, 2005 at 03:04:52PM -0300, luis wrote:
> Hi all,
>
> I?m looking a rotine in asm to INC ou DEC 2 packed BCD numbers.
> For example: 00000100 10011001 (499) + 1 = 00000101 00000000 (500)

I have conversion routines in my sunrise/sunset outdorr light controller.
You can find it here:

http://www.finitesite.com/d3jsys/clock.asm

The hex2bcd routine converts a hex number to its BCD equivalent. However,
the principles are the same:

1) Each digit needs to be checked to see if it's greater than 9. Digits
greater than 9 need to be compensated by adding/substracting 6.

So for your example (switching to hex for simplicity):

0499
+  1 Increment
---------------
049A Check for overflow in LSD. It's greater than 9 so adjust
+  6
---------------
04A0 Last digit adjusted. Work on next digit. Compensate
 60
---------------
0400 On the upper nybble you need to check carry. It carried so
100 You need to increment the low nybble of the high byte
-----------------
0500 You check the low nybble of the high byte to see if it overflowed.

It didn't so you are done.

Now the decrement is interesting. You have to subtract 6 to compensate
and take one away from the succeeding digit. Let's try decrementing
1000.

1000
-  1 Take away one.
-------------------
10FF Compensate for borrow in MSB
-100
-----
0FFF Now start compensating for digits.
-  6
-----
0FF9  
-060
-----
0F99
-600
-----
0999 No need to do the MSD.

That should be enough to get you going.

BAJ

2005\08\01@151024 by Byron A Jeff

face picon face
On Mon, Aug 01, 2005 at 03:04:52PM -0300, luis wrote:
> Hi all,
>
> I?m looking a rotine in asm to INC ou DEC 2 packed BCD numbers.
> For example: 00000100 10011001 (499) + 1 = 00000101 00000000 (500)

I have conversion routines in my sunrise/sunset outdorr light controller.
You can find it here:

http://www.finitesite.com/d3jsys/clock.asm

The hex2bcd routine converts a hex number to its BCD equivalent. However,
the principles are the same:

1) Each digit needs to be checked to see if it's greater than 9. Digits
greater than 9 need to be compensated by adding/substracting 6.

So for your example (switching to hex for simplicity):

0499
+  1 Increment
---------------
049A Check for overflow in LSD. It's greater than 9 so adjust
+  6
---------------
04A0 Last digit adjusted. Work on next digit. Compensate
 60
---------------
0400 On the upper nybble you need to check carry. It carried so
100 You need to increment the low nybble of the high byte
-----------------
0500 You check the low nybble of the high byte to see if it overflowed.

It didn't so you are done.

Now the decrement is interesting. You have to subtract 6 to compensate
and take one away from the succeeding digit. Let's try decrementing
1000.

1000
-  1 Take away one.
-------------------
10FF Compensate for borrow in MSB
-100
-----
0FFF Now start compensating for digits.
-  6
-----
0FF9  
-060
-----
0F99
-600
-----
0999 No need to do the MSD.

That should be enough to get you going.

BAJ

2005\08\01@151337 by Byron A Jeff

face picon face
On Mon, Aug 01, 2005 at 03:03:43PM -0400, Olin Lathrop wrote:
> Jan-Erik Soderholm wrote:
> >Probably easier on PIC18-series
> >then on the
> >"lower" series since
> >the PIC18 has an instruction set
> >that might help.
>
> Doesn't PIC 16 also have the DAW instruction and the DC bit?

Yes on the DC bit. No on the DAW. You have to compensate by hand.

>  I don't
> remember since I've never used either.  Personally I think they are a waste
> of silicon.  It's not worth burdening every chip with this stuff for the
> very rare case when it comes in handy.  If you are doing BCD, it's hard to
> argue you care about every last cycle of speed.

Bingo. It's a test and an add/subtract for every digit. With packed BCD you'll
also need a couple of instructions to mask the other digit in the packed set.
So it's probably about 16 instructions to process a 4 digit, 2 byte packed
BCD number. A minimal cost really.

BAJ

2005\08\01@152420 by Maarten Hofman

face picon face
Depending on what you want to do with the result, I would probably go
with a redundant version... Something like:

movf VALUE,w
addlw 1
movwf VALUE
btfss STATUS,DC
return
andlw 0xf0
addlw 16+6
movwf VALUE
btfss STATUS,C
return
andlw 0x0f
addlw 6*16
movwf VALUE
incf VALUE+1,f
return

Which would still count to 16, but reset things on an overflow. You
would need a correction routine if you want to actually use the
result, but it allows for slightly faster increases and decreases, I
think.

Note that the above routine was written in the Email, and not tested in any way.

Greetings,
Maarten Hofman.

2005\08\01@160324 by Scott Dattalo

face
flavicon
face
> Hi all,
>
> I´m looking a rotine in asm to INC ou DEC 2 packed BCD numbers.
> For example: 00000100 10011001 (499) + 1 = 00000101 00000000 (500)

You can use this for incrementing:

http://www.piclist.com/techref/microchip/math/add/bcdp2.htm

And this for decrementing:

http://www.piclist.com/techref/microchip/math/sub/bcdp2.htm

In each case, the carry out can be used to cascade arithmetic operations
over several packed BCD numbers.

Scot

2005\08\02@005952 by Andrew Warren

face
flavicon
face
Luis:

This comes up every once in a while and is likely to be in the
archives somewhere.

The last time I answered the question, I came up with these.  I
didn't assemble or test them, but no one complained that they failed
to work...

To increment one byte:

   INCF    REG

   MOVLW   0x06
   ADDWF   REG
   SKPDC
   SUBWF   REG

   MOVLW   0x60
   ADDWF   REG
   SKPC
   SUBWF   REG

To decrement one byte:

   MOVLW   1
   SUBWF   REG

   MOVLW   0x06
   SKPDC
   XORWF   REG

   MOVLW   0x60
   SKPC
   XORWF   REG

The Carry flag is valid after each of those, so first inc/dec the low
byte, then use the carry to decide whether to inc/dec the high byte.

-Andy

=== Andrew Warren - spam_OUTfastfwdTakeThisOuTspamix.netcom.com

2005\08\02@015452 by Dmitriy Kiryashov

picon face
Hi Andy.

Squeezing the lemon :)

incf        reg,F        ;INCF    REG
movlw        0x66        ;MOVLW   0x06
addwf        reg,F        ;ADDWF   REG
skpndc                ;SKPDC
andlw        0xF0        ;SUBWF   REG
skpnc                ;MOVLW   0x60
andlw        0x0F        ;ADDWF   REG
subwf        reg,F        ;SKPC
               ;SUBWF   REG        ;;btw here Cy is always=1


movlw        0x01        ;MOVLW   1
subwf        reg,W        ;SUBWF   REG
skpdc                ;MOVLW   0x06
xorlw        0x06        ;SKPDC
skpc                ;XORWF   REG
xorlw        0x60        ;MOVLW   0x60
movwf        reg        ;SKPC
               ;XORWF   REG


I think it is in piclist archive somewhere.


WBR Dmitry.



Andrew Warren wrote:
{Quote hidden}

> -

2005\08\02@023007 by Andrew Warren

face
flavicon
face
Dmitriy Kiryashov <piclistspamKILLspammit.edu> wrote:

> Squeezing the lemon :)

   Very nice.

> SUBWF   REG     ;;btw here Cy is always=1

   Yes, you're right.

   Sorry, Luis, I was incorrect when I said that the Carry flag was
   valid after my routine.

   -Andy

=== Andrew Warren - .....fastfwdKILLspamspam.....ix.netcom.com

2005\08\02@070426 by luis

flavicon
face
Thank you very much to all that helped me, especially
to Andrew and Dmitriy.
I´m going to do some tests to INC/DEC the high byte
using the last example of Dmitriy.

Luis F.


{Original Message removed}

2005\08\02@080549 by Spehro Pefhany

picon face
At 01:48 AM 8/2/2005 +0400, you wrote:
>Hi Andy.
>
>Squeezing the lemon :)
>
>incf    reg,F   ;INCF    REG
>movlw   0x66    ;MOVLW   0x06
>addwf   reg,F   ;ADDWF   REG
>skpndc          ;SKPDC
>andlw   0xF0    ;SUBWF   REG
>skpnc           ;MOVLW   0x60
>andlw   0x0F    ;ADDWF   REG
>subwf   reg,F   ;SKPC
>                 ;SUBWF   REG    ;;btw here Cy is always=1

Nice, Dimitry. The zero flag is valid, so it can be used for carry.

BTW, if you substitute 0xA6 for 0x66 in the second instruction you get
an increment for packed BCD seconds or minutes (0..59).

Best regards,

Spehro Pefhany --"it's the network..."            "The Journey is the reward"
EraseMEspeffspam_OUTspamTakeThisOuTinterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
->> Inexpensive test equipment & parts http://search.ebay.com/_W0QQsassZspeff


2005\08\02@101409 by Scott Dattalo

face
flavicon
face
> Hi Andy.
>
> Squeezing the lemon :)
>
> incf        reg,F        ;INCF    REG
> movlw        0x66        ;MOVLW   0x06
> addwf        reg,F        ;ADDWF   REG
> skpndc                ;SKPDC
> andlw        0xF0        ;SUBWF   REG
> skpnc                ;MOVLW   0x60
> andlw        0x0F        ;ADDWF   REG
> subwf        reg,F        ;SKPC
>                ;SUBWF   REG        ;;btw here Cy is always=1

When this routine finishes, W contains 0x00, 0x06, 0x60, or 0x66. The
upper nibble is 0 when the carry should be set. So, if you follow this
with:

 sublw 0x5F ; or any number between 0x06 and 0x5F

The carry bit is restored and you can cascade the result:

 skpnc
  incf  reg2,F

followed by the binary to bcd conversion. (or you can write skpc/return).

Scott

2005\08\02@101409 by Scott Dattalo

face
flavicon
face
> Hi Andy.
>
> Squeezing the lemon :)
>
> incf        reg,F        ;INCF    REG
> movlw        0x66        ;MOVLW   0x06
> addwf        reg,F        ;ADDWF   REG
> skpndc                ;SKPDC
> andlw        0xF0        ;SUBWF   REG
> skpnc                ;MOVLW   0x60
> andlw        0x0F        ;ADDWF   REG
> subwf        reg,F        ;SKPC
>                ;SUBWF   REG        ;;btw here Cy is always=1

When this routine finishes, W contains 0x00, 0x06, 0x60, or 0x66. The
upper nibble is 0 when the carry should be set. So, if you follow this
with:

 sublw 0x5F ; or any number between 0x06 and 0x5F

The carry bit is restored and you can cascade the result:

 skpnc
  incf  reg2,F

followed by the binary to bcd conversion. (or you can write skpc/return).

Scott

2005\08\02@103651 by luis

flavicon
face


> When this routine finishes, W contains 0x00, 0x06, 0x60, or 0x66. The
> upper nibble is 0 when the carry should be set. So, if you follow this
> with:
>
>  sublw 0x5F ; or any number between 0x06 and 0x5F
>
> The carry bit is restored and you can cascade the result:
>
>  skpnc
>   incf  reg2,F
>
> followed by the binary to bcd conversion. (or you can write skpc/return).
>
> Scott
> --


Hi Scott

Making some simulations, I got this testing the Z flag.

incf TENS_ONES,F
movlw 0x66
addwf TENS_ONES,F
skpndc
andlw 0xF0
skpnc
andlw 0x0F
subwf TENS_ONES,F
skpnz
incf HUND,1

Is there some problem to do like this.

Best Regards

Luis F.




2005\08\02@105132 by Scott Dattalo

face
flavicon
face

> Hi Scott
>
> Making some simulations, I got this testing the Z flag.
>
> incf TENS_ONES,F
> movlw 0x66
> addwf TENS_ONES,F
> skpndc
> andlw 0xF0
> skpnc
> andlw 0x0F
> subwf TENS_ONES,F
> skpnz
> incf HUND,1
>
> Is there some problem to do like this.

Ahh, that's even better! The only assumption being made is that the BCD
number is in a valid format prior to the increment. The carry is only set
if the input is 0x99. Tricky...

Scott

2005\08\03@011115 by Dmitriy Kiryashov

picon face
Hi Scott.

Long time no challenges :)

{Quote hidden}

0xYZ value initially in reg. We have three scenarios.

1. 0 <= Y <= 9, 0 <= Z < 9; c=0, dc=0, then W=0x66 at the end
2. 0 <= Y < 9, Z = 9 ( or higher ); c=0, dc=1, then W=0x60
3. Y = 9 ( or higher ), Z = 9 ( or higher ); c=1, dc=1, W=0x00

W cannot be 0x06 I believe. Z bit after subwf reg,F is only good
to cascade carry to next level if value in the reg is not above 0x99
initially ( it can be corrupted somehow ) It may be safer to rewrite
it this way

incf  reg,F
movlw 0x66        ;bcd adjust
addwf reg,F
skpndc
andlw 0xF0
skpnc
andlw 0x0F

;c=0 & dc=0 -> W=0x66 & z=0
;c=0 & dc=1 -> W=0x60 & z=0
;c=1 & dc=1 -> W=0x00 & z=1 (here is carry even if reg!=0 )

skpnz
incf reg1,F ;if carry increment next

subwf reg,F ;then update previous one

movlw 0x66        ;bcd adjust
addwf reg1,F
;etc



WBR Dmitriy.

2005\08\03@053105 by Regulus Berdin

picon face
Hi Dmitriy,

I had posted this about 6 or 7 years ago. 7 cycles :) excluding return.
radix is HEX.

BCD_INCR:
    MOVLW 67
    ADDWF CNT,F
    MOVLW -60
    SKPDC
     MOVLW -66
    SKPC
     ADDWF CNT,F
    RETURN

BCD_DECR:
    MOVLW 0xFF
    ADDWF CNT,F
    MOVLW -6
    SKPC
     MOVLW -66
    SKPDC
     ADDWF CNT,F
    RETURN

On BCD_INCR, Z flag is set when overflow occurs.
But on BCD_DECR, adding "XORLW -0x66" at the end will set Z flag during
an underflow.

regards,
Reggie

Dmitriy Kiryashov wrote:
{Quote hidden}

2005\08\03@124516 by Dmitriy Kiryashov

picon face
Hi Reggie.

Nice logical approach indeed :)
How would you cascade this approach
if bcd number is several bytes sized ?


WBR Dmitriy.



{Quote hidden}

2005\08\03@183023 by Regulus Berdin

picon face
Hi Dmitriy,

Something like this: (untested)

; Multi byte BCD increment
; by Regulus Berdin
;

INC:

 MOVLW  BCD_LSB
 MOVWF FSR
 MOVLW BCD_CNT
 MOVWF CNT

LOOP:

 MOVLW 67
 ADDWF INDF,F
 MOVLW -60
 SKPDC
  MOVLW -66
 SKPC
  ADDWF CNT,F

 SKPZ
  RETURN

 INCF FSR,F
 
 DECFSZ CNT,F
   GOTO LOOP

RETURN


; Multi byte BCD decrement
; by Regulus Berdin
;

DECR:

 MOVLW  BCD_LSB
 MOVWF FSR
 MOVLW BCD_CNT
 MOVWF CNT

LOOP:

 MOVLW 0xFF
 ADDWF CNT,F
 MOVLW -6
 SKPC
  MOVLW -66
 SKPDC
  ADDWF CNT,F

 XORLW -0x66
 SKPZ
  RETURN

 INCF FSR,F
 
 DECFSZ CNT,F
   GOTO LOOP

RETURN


Also have posted similar to this many years ago. I think it is also in
the archive but not on the piclist code repository.

regards,
Reggie


On 8/3/05, Dmitriy Kiryashov <vze27bymspamspam_OUTverizon.net> wrote:
{Quote hidden}

> -

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