Searching \ for 'Complementing Carry' 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=complementing+carry
Search entire site for: 'Complementing Carry'.

Truncated match.
PICList Thread
'Complementing Carry'
1997\09\26@211531 by STEENKAMP [M.ING E&E]

flavicon
picon face
Hi all,

I have been stuck now for at least 1/2 hour trying to figure out a way to
conditionally complement the carry flag on an '84.  The best I could come
up with was:

  comf  STATUS, W
  movwf Temp
  btfsc My_Flags, Complement_Carry
   rrf  Temp, W

This just doesn't feel like the right way!

I also tried:
  movlw 01h
  btfsc MyFlags, Complement_Carry
   xorwf STATUS, F

but it didn't work.  I was hoping that the xorwf instruction would
operate as normal and change the carry flag and afterwards set the Z flag
if the result was 0, but C is totally protected from being modified.

Has anyone got a better approach (preferably one that does not need a
file register)?  Maybe its staring me in the face, but right now I'm
staring at the insides of my eyelids :-O

G'night (to all on this side of the globe)
Niki

1997\09\26@234735 by Dmitry Kiryashov

flavicon
face
N STEENKAMP [M.ING E&E] wrote:

> but it didn't work.  I was hoping that the xorwf instruction would
> operate as normal and change the carry flag and afterwards set the Z flag
> if the result was 0, but C is totally protected from being modified.
>
> Has anyone got a better approach (preferably one that does not need a
> file register)?  Maybe its staring me in the face, but right now I'm
> staring at the insides of my eyelids :-O

Try the following .

       movfw   status
       btfsc   MyFlags,Complement_Carry
       xorlw   1
       andlw   1       ;or something like that

WBR Dmitry .

1997\09\27@010158 by John Payson

picon face
> I also tried:
>    movlw 01h
>    btfsc MyFlags, Complement_Carry
>     xorwf STATUS, F
>
> but it didn't work.  I was hoping that the xorwf instruction would
> operate as normal and change the carry flag and afterwards set the Z flag
> if the result was 0, but C is totally protected from being modified.
>
> Has anyone got a better approach (preferably one that does not need a
> file register)?  Maybe its staring me in the face, but right now I'm
> staring at the insides of my eyelids :-O

Perhaps:
       rlf     FSR
       movlw   1       ; Could also be movf flag,w ...
       xorwf   FSR
       rrf     FSR

Note that when all is said and done FSR will have the same value it
started with, Z will almost certainly be cleared, carry will be toggled,
and nothing else will be affected.

1997\09\27@010849 by Andrew Warren

face
flavicon
face
N STEENKAMP [M.ING E&E] <spam_OUTPICLISTTakeThisOuTspamMITVMA.MIT.EDU> wrote:

{Quote hidden}

   Niki:

   Yes, you're right... It works like this:

   If you use an instruction whose destination is the status
   register, and if that instruction can modify the Carry, Zero, OR
   the Decimal-Carry flags, then the values that your instruction
   explicitly writes to ALL THREE of those bits are ignored and the
   Carry, Zero, and/or Decimal-Carry flags are set by  the
   processor as appropriate.

   When the PIC executes the "XORWF STATUS" instruction in your
   example code, the Zero, Carry, and Decimal-Carry flags are
   unchanged, then the Zero flag is adjusted.

   This behavior is fully documented in the data books.

> Has anyone got a better approach (preferably one that does not need
> a file register)?

   Yes:

       MOVF    STATUS,W
       BTFSC   Complement_Carry
       XORLW   1
       MOVWF   STATUS

   -Andy

=== Meet other PICLIST members at the Embedded Systems Conference:
=== 6:30 pm on Wednesday, 1 October, at Bytecraft Limited's booth.
===
=== For more information on the Embedded Systems Conference,
=== see: http://www.embedsyscon.com/

=== Andrew Warren - .....fastfwdKILLspamspam@spam@ix.netcom.com
=== Fast Forward Engineering - Vista, California
=== http://www.geocities.com/SiliconValley/2499

1997\09\28@125310 by Michael S. Hagberg

flavicon
face
At 03:14 AM 9/27/97 GMT+0200, you wrote:
{Quote hidden}

to complement any bit you can do the following. note: the carry bit (bit 0)
is in the status register (ram location 3).

       btfsc   3,0     ; status,c
       bcf     3,0
       btfss   3,0
       bsf     3,0

what instruction was used to place the data into 'w'. if it didn't set
the zero flag you could use this

       iorlw   0       ; sets the zero flag if 'w' is zero

michael

1997\09\28@142424 by Mike Keitz

picon face
On Sun, 28 Sep 1997 09:43:11 -0700 "Michael S. Hagberg"
<mhagbergspamKILLspamMAIL.PRIMARY.NET> writes:

>to complement any bit you can do the following. note: the carry bit
>(bit 0)
>is in the status register (ram location 3).
>
>        btfsc   3,0     ; status,c
>        bcf     3,0
>        btfss   3,0
>        bsf     3,0

This code will not work.  The bit will always be set at the end
regardless of its value at the start.  In order to make something like
this work, the "set if clear" part (the last 2 instructions) needs to be
bypassed if the bit was cleared because it was set by the first 2
instructions.  Complementing a bit without affecting W requires lengthy
goto instructions.

Others have already posted these 2 good solutions:
       movlw   1               ;xor mask
       rlf     file,f          ;Carry bit into file.0
       xorwf   file,f          ;Complement copy of carry bit
       rrf     file,f          ;Put data back, complemented
carry.
John Payson suggested using FSR, actually any file register can be used
as long as its intermediate value won't be examined by an interrupt
routine.  After this routine, the data in the file register will be the
same as it was at the start.

And the most direct solution of all (I beleive due to Andrew Warren):
       movfw   STATUS          ;Get status bits into W
       xorlw   1               ;Carry bit is bit 0 - complement
it.
       movwf   STATUS          ;Put complemented bits back
This one has the possible advantage of not affecting the Z bit.

Now to contribute something which I hope is useful and original.   I
hvaen't seen any posts about using add or subtract to complement carry.
Subtracting 1 from 0 will clear carry, but subtracting 0 from 0 will set
it.  So use something like this:
       movlw   0
       skpnc
       movlw   1               ;Now W=0 if C =0, W=1 if C=1
       sublw   0               ;0-W, sets C if W=0

By using a known-zero location, could be simplified to
       rlf     always0,w       ;C into LSB of W
       subwf   always0,w       ;Same as sublw 0, since f always 0.

Using subwf rather than sublw makes the code compatible with 12-bit PICs.
But it's hard to make this one conditional, since both instructions
affect carry, skipping only one of them will not leave C unchanged.
Using 2 conditional skips, the routine would only be 4 words, the same
length as the shortest other conditional ones proposed.

1997\09\29@095714 by Martin R. Green

picon face
How about:

       btfss My_Flags, Complement_Carry
       goto done
       btfsc STATUS, CARRY
       goto clear_carry
       bsf STATUS, CARRY
       goto done
clear_carry:
       bcf STATUS, CARRY
done:

It takes a few more locations and cycles, but it doesn't require a register.


HTH - Martin R. Green
.....elimarKILLspamspam.....bigfoot.com

----------
From:   N STEENKAMP [M.ING E&E][SMTP:EraseMESTEENKMPspam_OUTspamTakeThisOuTFIRGA.SUN.AC.ZA]
Sent:   Friday, September 26, 1997 11:14 PM
To:     PICLISTspamspam_OUTmitvma.mit.edu
Subject:        Complementing Carry

Hi all,

I have been stuck now for at least 1/2 hour trying to figure out a way to
conditionally complement the carry flag on an '84.  The best I could come
up with was:

  comf  STATUS, W
  movwf Temp
  btfsc My_Flags, Complement_Carry
   rrf  Temp, W

This just doesn't feel like the right way!

I also tried:
  movlw 01h
  btfsc MyFlags, Complement_Carry
   xorwf STATUS, F

but it didn't work.  I was hoping that the xorwf instruction would
operate as normal and change the carry flag and afterwards set the Z flag
if the result was 0, but C is totally protected from being modified.

Has anyone got a better approach (preferably one that does not need a
file register)?  Maybe its staring me in the face, but right now I'm
staring at the insides of my eyelids :-O

G'night (to all on this side of the globe)
Niki

1997\09\29@111200 by Martin R. Green

picon face
Michael, your sample code will ALWAYS set carry on.  Plus, if the carry
flag starts off cleared, it will test to see if cleared (which it is), then
will test to see if it is set, which is unnecessary, since you already know
it must be cleared.

Martin R. Green
@spam@elimarKILLspamspambigfoot.com

----------
From:   Michael S. Hagberg[SMTP:KILLspammhagbergKILLspamspamMAIL.PRIMARY.NET]
Sent:   Sunday, September 28, 1997 12:43 PM
To:     RemoveMEPICLISTTakeThisOuTspammitvma.mit.edu
Subject:        Re: Complementing Carry

<SNIP>

to complement any bit you can do the following. note: the carry bit (bit 0)
is in the status register (ram location 3).

       btfsc   3,0     ; status,c
       bcf     3,0
       btfss   3,0
       bsf     3,0

what instruction was used to place the data into 'w'. if it didn't set
the zero flag you could use this

       iorlw   0       ; sets the zero flag if 'w' is zero

michael

1997\09\29@111205 by Martin R. Green

picon face
Regarding your suggestion about using sublw, be aware that sublw (and addlw),
are not available in the 12CXX devices.

CIAO - Martin R. Green
spamBeGoneelimarspamBeGonespambigfoot.com

----------
From:   Mike Keitz[SMTP:TakeThisOuTmkeitzEraseMEspamspam_OUTJUNO.COM]
Sent:   Sunday, September 28, 1997 2:18 PM
To:     RemoveMEPICLISTspamTakeThisOuTmitvma.mit.edu
Subject:        Re: Complementing Carry

On Sun, 28 Sep 1997 09:43:11 -0700 "Michael S. Hagberg"
<mhagbergEraseMEspam.....MAIL.PRIMARY.NET> writes:

>to complement any bit you can do the following. note: the carry bit
>(bit 0)
>is in the status register (ram location 3).
>
>        btfsc   3,0     ; status,c
>        bcf     3,0
>        btfss   3,0
>        bsf     3,0

This code will not work.  The bit will always be set at the end
regardless of its value at the start.  In order to make something like
this work, the "set if clear" part (the last 2 instructions) needs to be
bypassed if the bit was cleared because it was set by the first 2
instructions.  Complementing a bit without affecting W requires lengthy
goto instructions.

Others have already posted these 2 good solutions:
       movlw   1               ;xor mask
       rlf     file,f          ;Carry bit into file.0
       xorwf   file,f          ;Complement copy of carry bit
       rrf     file,f          ;Put data back, complemented
carry.
John Payson suggested using FSR, actually any file register can be used
as long as its intermediate value won't be examined by an interrupt
routine.  After this routine, the data in the file register will be the
same as it was at the start.

And the most direct solution of all (I beleive due to Andrew Warren):
       movfw   STATUS          ;Get status bits into W
       xorlw   1               ;Carry bit is bit 0 - complement
it.
       movwf   STATUS          ;Put complemented bits back
This one has the possible advantage of not affecting the Z bit.

Now to contribute something which I hope is useful and original.   I
hvaen't seen any posts about using add or subtract to complement carry.
Subtracting 1 from 0 will clear carry, but subtracting 0 from 0 will set
it.  So use something like this:
       movlw   0
       skpnc
       movlw   1               ;Now W=0 if C =0, W=1 if C=1
       sublw   0               ;0-W, sets C if W=0

By using a known-zero location, could be simplified to
       rlf     always0,w       ;C into LSB of W
       subwf   always0,w       ;Same as sublw 0, since f always 0.

Using subwf rather than sublw makes the code compatible with 12-bit PICs.
But it's hard to make this one conditional, since both instructions
affect carry, skipping only one of them will not leave C unchanged.
Using 2 conditional skips, the routine would only be 4 words, the same
length as the shortest other conditional ones proposed.

1997\09\29@123341 by Mike Keitz

picon face
Earlier, I wrote:
> Complementing a bit without affecting W requires
>lengthy goto instructions.

Not true!  As a final thought on this neverending subject, consider:
       bcf     tmp,0           ;Use value of 0
       skpc                    ;if carry is 1
       bsf     tmp,0           ;if carry 0, use 1.
       rrf     tmp,f           ;New bit to carry bit.
This does require a temporary register, but not W.  It doesn't affect any
status bits except carry.  It can be made conditional by skipping the rrf
conditionally.

1997\09\29@140328 by John Payson

picon face
> > Complementing a bit without affecting W requires
> >lengthy goto instructions.
>
> Not true!  As a final thought on this neverending subject, consider:
>         bcf     tmp,0           ;Use value of 0
>         skpc                    ;if carry is 1
>         bsf     tmp,0           ;if carry 0, use 1.
>         rrf     tmp,f           ;New bit to carry bit.
> This does require a temporary register, but not W.  It doesn't affect any
> status bits except carry.  It can be made conditional by skipping the rrf
> conditionally.

If you add an "rlf tmp,f" to the beginning of that, then "tmp" will be
unaffected (though making it conditional will be a little harder).

1997\09\29@174640 by Mike Keitz

picon face
On Mon, 29 Sep 1997 12:42:41 -0500 John Payson <EraseMEsupercatspamMCS.NET> writes:

[The routine below complements C without affecting W, but it needs a
temporary RAM 'tmp']

>>         bcf     tmp,0           ;Use value of 0
>>         skpc                    ;if carry is 1
>>         bsf     tmp,0           ;if carry 0, use 1.
>>         rrf     tmp,f           ;New bit to carry bit.

>If you add an "rlf tmp,f" to the beginning of that, then "tmp" will be
>unaffected (though making it conditional will be a little harder).

No, this won't work because the rotate would put the original carry bit
which needs to be tested into bit 0 of tmp-- which is the same bit that
needs to be modified.  So you'd need to use gotos to make sure it is
modified properly.  No advantage over modifying C in place.

>From an information theory point of view, there are only 9 bits active,
C and the 8 bits of tmp.  So there isn't an extra bit to use to develop a
new value while still saving all 8 bits of tmp.

One could use brute-force on a single extra bit (The Z bit in the STATUS
register being a possible choice as a rather expendable bit):

;  First, put complement of C into 'bit'
       bcf     bit             ;Assume new value will be 0
       skpc
       bsf     bit             ;C = 0, therefore new value is 1.
;  Then copy 'bit' into C
       clrc                    ;Set C to 0
       btfsc   bit             ;if bit is 0
       setc                    ;if bit 1, C = 1

At 6 instructions/6 cycles this routine isn't anything to write home
about though, maybe not even competitive with the goto ones.  (For those
not familiar with the Microchip assembler, skpc, clrc, etc. are
predefined macros for bit operations on the Carry bit in the STATUS
register)

1997\09\30@121635 by Michael S. Hagberg

flavicon
face
At 03:14 AM 9/27/97 GMT+0200, you wrote:
{Quote hidden}

i should be awake when i reply, i knew something was wrong with that code
but just looked at it long enough to convince myself that it looked ok.

try this

       swap   status,f      ; this will save the carry bit in bit 4
       btfss  status,4      ; was the carry set
       bcf    status,0      ; no, clear the carry
       btfsc  status,4      ; was the carry cleared
       bsf    status,0      ; no, set the carry
       iorlw  0             ; set the zero flag


michael

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