Searching \ for '32 bit additions' 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=bit+additions
Search entire site for: '32 bit additions'.

Truncated match.
PICList Thread
'32 bit additions'
1997\02\19@113859 by mike

flavicon
picon face
Hi,

I have run into trouble implementing 4 byte (32 bit) adds
on a PIC.

I took the code I had for 16 bit adds:

       movf    a_0, w          ; b:=a+b  ( _0=LSB )
       addwf   b_0, f
       movf    a_1, w          ;
       skpnc
       addlw   .1
       addwf   b_1, f

and extended it to include _2 and _3:

       movf    a_2, w
       skpnc
       addlw   .1
       addwf   b_2, f
       movwf   a_3, w
       skpnc
       addlw   .1
       addwf   b_3, f          ; ( _3 is MSB )

The problem with this is that if a_1 is FF and a_0+b_0 caused
a carry, the addlw .1 will set the carry flag and the addwf b_1, f
will clear it.

I have tried several different versions, but have failed to find
a way to propogate the carry through all bytes properly.

Is there a standard way to do this? Am I missing something
obvious? Do any of the C compilers support 32 bit sums so I
could take a peek at what they do?

Thanks for your help,


Mike Watson

1997\02\19@121729 by Craig Knotts

flavicon
face
    Offhand, I'd add the least significant byte first, propogate the
    carries up, then add the next more significant byte, propogate the
    carries, etc.

    Not a particularly tight bit of code, but the carry flag is only good
    for a single 8 bit add at a time.


______________________________ Reply Separator _________________________________
Subject: 32 bit additions
Author:  Mayes uk <spam_OUTmikeTakeThisOuTspamD-M-G.DEMON.CO.UK> at Internet
Date:    2/19/97 4:22 PM


Hi,

I have run into trouble implementing 4 byte (32 bit) adds
on a PIC.

I took the code I had for 16 bit adds:

       movf    a_0, w          ; b:=a+b  ( _0=LSB )
       addwf   b_0, f
       movf    a_1, w          ;
       skpnc
       addlw   .1
       addwf   b_1, f

and extended it to include _2 and _3:

       movf    a_2, w
       skpnc
       addlw   .1
       addwf   b_2, f
       movwf   a_3, w
       skpnc
       addlw   .1
       addwf   b_3, f          ; ( _3 is MSB )

The problem with this is that if a_1 is FF and a_0+b_0 caused
a carry, the addlw .1 will set the carry flag and the addwf b_1, f
will clear it.

I have tried several different versions, but have failed to find
a way to propogate the carry through all bytes properly.

Is there a standard way to do this? Am I missing something
obvious? Do any of the C compilers support 32 bit sums so I
could take a peek at what they do?

Thanks for your help,


Mike Watson

1997\02\19@132106 by Bob Fehrenbach

picon face
Mayes uk <.....mikeKILLspamspam@spam@d-m-g.demon.co.uk> wrote:

{Quote hidden}

Mike,

I think your problem is in the original 16 bit add.  Compare with
this:

  movf    a+1, w
  addwf   b+1, f
  movf    a, w
  skpnc
  incfsz  a, w       <---  Compare with yours
  addwf   b, f


This concept can be easily extended.  The last four instructions
are basically an add with carry.


  movf    a+3, w
  addwf   b+3, f

  movf    a+2, w
  skpnc
  incfsz  a+2, w
  addwf   b+2, f

  movf    a+1, w
  skpnc
  incfsz  a+1, w
  addwf   b+1, f

  movf    a, w
  skpnc
  incfsz  a, w
  addwf   b, f

Standard discalimer:  I haven't actually tested this.


--
Bob Fehrenbach    Wauwatosa, WI     bfehrenbspamKILLspamexecpc.com

1997\02\19@221817 by John Payson

picon face
> I think your problem is in the original 16 bit add.  Compare with
> this:
>
>    movf    a+1, w
>    addwf   b+1, f
>    movf    a, w
>    skpnc
>    incfsz  a, w       <---  Compare with yours
>    addwf   b, f
>
> This concept can be easily extended.  The last four instructions
> are basically an add with carry.

Note that if you are doing an add-with-carry-in but don't care about carry-
out, or if you are doing an add-constant-with-carry in, you may shave off
one instruction if you have a "KZ" location available [KZ == known-zero; a
location in either RAM or unimplemented space which will is known to read
zero]

; Performs X+Y+(Carry) -> X; carry out is not reliable if Y==255

       rlf     KZ,w
       addwf   Y,w
       addwf   X,f

; Performs X+(const)+(Carry) -> X; (const) must be <255; carry out is
;   reliable.

       rlf     KZ,w
       addlw   (const)
       addwf   X,f

; Performs X+255+(Carry) -> X; carry out is reliable

       movlw   255
       btfss   C
        addwf  X,f

Note that all of these may be modified, with the addition of one more inst-
ruction, to output their result to another register unlike the general case
4-cycle add-with-carry for which there is no more efficient option than to
simply copy one operand to the destination register before the operation.

1997\02\20@064712 by mike

flavicon
picon face
In message  <.....pG0CzYi8aEmE089ynKILLspamspam.....execpc.com> EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU writes:
> Mike,
>
> I think your problem is in the original 16 bit add.  Compare with
> this:

Well, the 16 bit add works ok on 16 bits. It was actually pinched
from the output of one of the C compilers represented on the list.


>
>    movf    a+1, w
>    addwf   b+1, f
>    movf    a, w
>    skpnc
>    incfsz  a, w       <---  Compare with yours
>    addwf   b, f

I like it. I plugged it in and it works. I daren't admit how
much time I spent trying to figure it out.

Thanks for your help, Bob.

Best regards,

Mike

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