Searching \ for 'PIC Math FAQ page' 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/math/index.htm?key=math
Search entire site for: 'PIC Math FAQ page'.

Truncated match.
PICList Thread
'PIC Math FAQ page'
2000\02\11@171152 by jamesnewton

picon face
NEW MATH RESOURCE FOR PICs

I've been reorganizing the PIC FAQ math page and searching the archives to
round out the code available there. I think it's a good site thanks to all
the brilliant code contributed to the PICList. See:
http://techref.massmind.org/microchip/math
or
http://www.piclist.com/faq then select Routine library and Math.

1. I have a couple of snippets that don't have the authors name and would
love to give credit where its due.

2. If any of the credited authors who don't have web sites would like some
form of link so that they can be contacted, please let me know. If you would
like a web site for PIC or PC related stuff, let me know. I haven't copied
any code off of anyone else's homepage. If you see code that is posted on
your web page, rest assured I got it from the PICList archives. If you
didn't post it and want it removed, just let me know.

3. I'd really like to know if I've missed anything.  If I can host or mirror
your pages or copy the routines (with full credit, links, etc... of course)
from your web site, personal library, code vault or bag of tricks, <GRIN> I
would love to.

4. I'm worried that the comments I've added (of the form W= and A2=) on each
line of John Payson's 16 bit Binary to BCD conversion code may be incorrect.
Specifically: Am I getting the subtraction by addition idea correct? How can
comf do a negate when it does a ones complement vice a twos complement? Math
is not my strongest point. see:
http://techref.massmind.org/microchip/math/radix/b2bu-16b5d.htm

---
James Newton spam_OUTjamesnewtonTakeThisOuTspamgeocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!
Members can add private/public comments/pages ($0 TANSTAAFL web hosting)

2000\02\11@173452 by Scott Dattalo

face
flavicon
face
On Fri, 11 Feb 2000, James Newton wrote:

> 4. I'm worried that the comments I've added (of the form W= and A2=) on each
> line of John Payson's 16 bit Binary to BCD conversion code may be incorrect.
> Specifically: Am I getting the subtraction by addition idea correct? How can
> comf do a negate when it does a ones complement vice a twos complement? Math
> is not my strongest point. see:
> http://techref.massmind.org/microchip/math/radix/b2bu-16b5d.htm

Hi James,

after you sent me the (private) message the other day, I did spend a few
minutes running through the comments.

       movf    NumH,w  ;w  = A3*16+A2
       andlw   $0F     ;w  = A2
       addwf   Hund,f  ;B2 = A3-46 + A2 = A3+A2-46
>>>     addwf   Hund,f  ;B2 = 2*(A3+A2-46) = 2A3+2A2-92
       addwf   Ones,f  ;B0 = A3+4 + A2 = A3+A2+4

should be:
       addwf   Hund,f  ;B2 = 2*A2+A3-46


And

>>>     rlf     Tens,f  ;B1 = 2*(4A2+A1-92) = 8A2+2A1-184
>>>     rlf     Ones,f  ;B0 = 2*(A3+A2+A1+4) = 2A3+2A2+2A1+8
       comf    Ones,f  ;B0 = -2A3-2A2-2A1-8
       rlf     Ones,f  ;B0 = 2*(-2A3-2A2-2A1-8) = -4A3-4A2-4A1-16

-92 << 1  does not equal -184

-92 = 0xa4
and 0xa4 << 1 is 0x48

Then the next line is going to pick up the carry. So these two lines
should read instead:

>>>     rlf     Tens,f  ;B1 = 2*(4A2+A1+0xa4) = 8A2+2A1+0x48
>>>     rlf     Ones,f  ;B0 = 2*(A3+A2+A1+4)+1 = 2A3+2A2+2A1+9

These are the only two errors that immediately popped out. However, since
the results of these calculations are used further down in the code, the
errors are propogated. There may be a bug silently lurking in this code -
I hadn't noticed the C bit thing before...


Scott

2000\02\11@180206 by jamesnewton

face picon face
Doh! adding A2 twice does not multiply by 2! Thanks for spotting that one.

>-92 = 0xa4
> and 0xa4 << 1 is 0x48
yes, but 0x48 or (72 + x) mod 256 is (x-184) mod 256 or in other words,
adding 72 and wrapping around 256 is the same as subtracting 184. I think
I'm right on that one.

I'm as puzzled as you are about the comf thing. I'm trying to find the book
I learned binary math from.

Thanks for your time, I know it's in great demand.

---
James Newton .....jamesnewtonKILLspamspam@spam@geocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!
Members can add private/public comments/pages ($0 TANSTAAFL web hosting)


{Original Message removed}

2000\02\11@181840 by Scott Dattalo

face
flavicon
face
On Fri, 11 Feb 2000, James Newton wrote:

> Doh! adding A2 twice does not multiply by 2! Thanks for spotting that one.
>
> >-92 = 0xa4
> > and 0xa4 << 1 is 0x48
> yes, but 0x48 or (72 + x) mod 256 is (x-184) mod 256 or in other words,
> adding 72 and wrapping around 256 is the same as subtracting 184. I think
> I'm right on that one.

True but, there's also that carry bit. A two's complement can be written
in terms of a one's complement:

two's complement of x = 1 + one's complement of x.
So, taking this snippet from your web page:

> >>>     rlf     Tens,f  ;B1 = 2*(4A2+A1-92) = 8A2+2A1-184
> >>>     rlf     Ones,f  ;B0 = 2*(A3+A2+A1+4) = 2A3+2A2+2A1+8
>         comf    Ones,f  ;B0 = -2A3-2A2-2A1-8
>         rlf     Ones,f  ;B0 = 2*(-2A3-2A2-2A1-8) = -4A3-4A2-4A1-16


    rlf     Tens,f  ;B1 = 2*(4A2+A1-92) = 8A2+2A1-184 (I'll leave this)
    rlf     Ones,f  ;B0 = 2*(A3+A2+A1+4) = 2A3+2A2+2A1+9
    comf    Ones,f  ;B0 = ~(2A3+2A2+2A1+9) = -2A3-2A2-2A1-8
    rlf     Ones,f  ;B0 = 2*(-2A3-2A2-2A1-8) = -4A3-4A2-4A1-16

The comf's comment is still the same...

So now that I think about it, Payson new what he was doing (ah, should I
have ever doubted...)

Scott

2000\02\11@183117 by jamesnewton

face picon face
Are you saying that the
    rlf     Tens,f  ;B1 = 2*(4A2+A1-92) = 8A2+2A1-184
is guarrenteed to generate a carry and that this is the +1 in the
    rlf     Ones,f  ;B0 = 2*(A3+A2+A1+4) = 2A3+2A2+2A1+9
and if so how can you be sure of the carry? Wow... I'm brain fried... I'll
have to think about this more later.

Thanks again.

---
James Newton jamesnewtonspamKILLspamgeocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!
Members can add private/public comments/pages ($0 TANSTAAFL web hosting)


{Original Message removed}

2000\02\11@235550 by Nikolai Golovchenko
flavicon
face
James, you miss a bit (literally). This routine works exactly as Scott deducted:

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

First, let's make clear how to do some math operations:
1) Negation - invert all bits and add one:
  comf reg, f
  incf reg, f
2) Multiply by two signed byte (arithmetical shift left)-
  bcf _C        ;clear carry
  rlf reg, f
3) Divide by two signed byte (arithmetical shift right)-
  rlf reg, w    ;sign extension
  rrf reg, f


I added some comments to this routine
(;; marked, too many commenters <G>)
;-------------------------------------------------------------------

;
; Binary-to-BCD.  Written by John Payson.
;
; Enter with 16-bit binary number in NumH:NumL.
; Exits with BCD equivalent in TenK:Thou:Hund:Tens:Ones.
;

       org     $0010   ;Start of user files for 16c84

NumH:   ds      1
NumL:   ds      1
TenK:   ds      1
Thou:   ds      1
Hund:   ds      1
Tens:   ds      1
Ones:   ds      1

Convert:                        ; Takes number in NumH:NumL
                               ; Returns decimal in
                               ; TenK:Thou:Hund:Tens:Ones
       swapf   NumH,w
       andlw   $0F             ;*** PERSONALLY, I'D REPLACE THESE 2
       addlw   $F0             ;*** LINES WITH "IORLW 11110000B" -AW
       movwf   Thou
       addwf   Thou,f
       addlw   $E2
       movwf   Hund
       addlw   $32
       movwf   Ones
;;At this point
;;Thou = 2 (A3 - 16) = 2 * A3 - 32
;;Hund = A3 - 16 - 30 = A3 - 46
;;Ones = A3 - 16 - 30 + 50 = A3 + 4
       movf    NumH,w
       andlw   $0F
       addwf   Hund,f
       addwf   Hund,f
       addwf   Ones,f
       addlw   $E9
       movwf   Tens
       addwf   Tens,f
       addwf   Tens,f
;;Now
;;Hund = Hund + 2 * A2 = A3 + 2 * A2 - 46
;;Ones = Ones + A2 = A3 + A2 + 4
;;Tens = (A2 - 23) * 3 = 3 * A2 - 69
       swapf   NumL,w
       andlw   $0F
       addwf   Tens,f
       addwf   Ones,f
;;Tens = Tens + A1 = 3 * A2 + A1 - 69
;;Ones = Ones + A1 = A3 + A2 + A1 + 4
;;C = 0
       rlf     Tens,f
;;Tens = Tens * 2 = 6 * A2 + 2 * A1 - 138
;;                  ~~~~~~~~~~~~~~~~~~~~~
;;Also bit C is set, because Tens register was negative.
       rlf     Ones,f
;;Ones  =  2  * Ones + 1 = 2 * (A3 + A2 + A1 + 4) + 1 = 2 * (A3 + A2 +
;;A1) + 9
;;C = 0
       comf    Ones,f
;;comf can be regarded like:
;;      comf Ones, f
;;      incf Ones, f
;;      decf Ones, f
;;First two instructions make up negation. So,
;;Ones  = - Ones - 1 = - 2 * (A3 + A2 + A1) - 9 - 1 = - 2 * (A3 + A2 +
;;A1) - 10
       rlf     Ones,f
;;Ones = 2 * Ones = - 4 * (A3 + A2 + A1) - 20
       movf    NumL,w
       andlw   $0F
       addwf   Ones,f
       rlf     Thou,f
;;Ones = Ones + A0 = A0 - 4 * (A3 + A2 + A1) - 20
;;                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;;C = 0
;;Thou = 2 * Thou = 4 * A3 - 64
;;                  ~~~~~~~~~~~
       movlw   $07
       movwf   TenK
;;TenK = 7
;;~~~~~~~~

                       ; At this point, the original number is
                       ; equal to
                       TenK*10000+Thou*1000+Hund*100+Tens*10+Ones ;
                       if those entities are regarded as two's
                       compliment ; binary.  To be precise, all of
                       them are negative ; except TenK.  Now the
                       number needs to be normal- ; ized, but this
                       can all be done with simple byte ; arithmetic.

       movlw   $0A                             ; Ten
Lb1:
       addwf   Ones,f
       decf    Tens,f
       btfss   3,0
        goto   Lb1
Lb2:
       addwf   Tens,f
       decf    Hund,f
       btfss   3,0
        goto   Lb2
Lb3:
       addwf   Hund,f
       decf    Thou,f
       btfss   3,0
        goto   Lb3
Lb4:
       addwf   Thou,f
       decf    TenK,f
       btfss   3,0
        goto   Lb4

       retlw   0



Best regards,
Nikolai

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