Searching \ for '[PIC] xorlw question' 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: 'xorlw question'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] xorlw question'
2009\01\06@155011 by Harry H. Arends

flavicon
face
LS,

Could someone explain this construction to me.
I looked in the MPASM help file but cant find a description.

   xorlw          (0x05 ^ 0x20)            ; CV545

Regards

Harry

2009\01\06@160707 by olin piclist

face picon face
Harry H. Arends wrote:
> Could someone explain this construction to me.
> I looked in the MPASM help file but cant find a description.
>
>     xorlw  (0x05 ^ 0x20)    ; CV545

The "^" is documented in the manual, so I guess you're asking why someone
would do this?  To answer that, we need to see a few more lines around this
one.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2009\01\06@161646 by Tamas Rudnai

face picon face
You probably already found the description of XORLW so I guess you are
asking about the (0x05 ^ 0x20)... This is an expression that can be
evaluated at compile time, so the compiler will do that replacing it with
the literal. The oparator ^ is an XOR actually, but you can find the
explanation for that in the help.

Tamas


On Tue, Jan 6, 2009 at 8:50 PM, Harry H. Arends <spam_OUTh.arendsTakeThisOuTspamhome.nl> wrote:

{Quote hidden}

> -

2009\01\06@161823 by Harold Hallikainen

face
flavicon
face

> LS,
>
> Could someone explain this construction to me.
> I looked in the MPASM help file but cant find a description.
>
>     xorlw          (0x05 ^ 0x20)            ; CV545
>
> Regards
>
> Harry


The ^ is a bitwise exclusive or. Since the bits of these two constants do
not overlap, you just end up with 0x25. That is then xored with whatever
is in w. Don't really know what they were trying to do!

Harold



--
FCC Rules Updated Daily at http://www.hallikainen.com - Advertising
opportunities available!

2009\01\06@163620 by Harry H. Arends

flavicon
face
Part of code:
===============================================
FindCV:
   bcf     NOCV
   bcf     RDONLY
   xorlw   0x00                      ; CV513
   btfsc   STATUS,Z
   retlw   PRIM_ADDR0
   xorlw   (0x00 ^ 0x08)              ; CV521
   btfsc   STATUS,Z
   retlw   PRIM_ADDR1
   xorlw   (0x08 ^ 0x1C)              ; CV541
   btfsc   STATUS,Z
   retlw   CVAR_29
   xorlw   (0x1C ^ 0x02)            ; CV515
   btfsc   STATUS,Z
   retlw   MAX_BRIGHT
   xorlw        (0x02 ^ 0x03)            ; CV516
   btfsc          STATUS,Z
   retlw        E_CV516
   xorlw          (0x03 ^ 0x04)                    ; CV517
   btfsc        STATUS,Z
   retlw        E_CV517
   xorlw          (0x04 ^ 0x05)                    ; CV518
   btfsc          STATUS,Z
   retlw          E_CV518
   xorlw          (0x05 ^ 0x20)                    ; CV545
   btfsc        STATUS,Z
   retlw          E_CV545
   xorlw        (0x20 ^ 0x21)                    ; CV546
   btfsc          STATUS,Z
   retlw        E_CV546
   bsf            RDONLY
   xorlw        (0x21 ^ 0x06)                    ; CV519
   btfsc          STATUS,Z
   retlw        MFG_VR_CODE
===================================================
This routine is called with the actual CV value in w eg CV513=0x00 and
CV519=0x06

{Quote hidden}

> -

2009\01\06@165328 by Maarten Hofman

face picon face
Redwood Shores, 6 januari 2009.

Dear Harry,

They are using the ^-XOR to undo the modification they made to the source
variable in the previous xorlw. So the value in W is modified by the check,
and by adding the previous value to the next check using ^, it undoes this
damage while doing the next check at the same time.

I can imagine writing code like this, although with this amount of values
I'd probably opt for a jump table instead (if the memory was available). I
certainly would've added more comments about what was happening especially
in the beginning.

Greetings,
Maarten Hofman.

2009/1/6 Harry H. Arends <.....h.arendsKILLspamspam.....home.nl>

{Quote hidden}

2009\01\06@180758 by olin piclist

face picon face
Harry H. Arends wrote:
>     xorlw   (0x08 ^ 0x1C)      ; CV541
>     btfsc   STATUS,Z
>     retlw   CVAR_29
>     xorlw   (0x1C ^ 0x02)     ; CV515
>     btfsc   STATUS,Z
>     retlw   MAX_BRIGHT

What's happening here is that he is testing the original value in W against
a succession of constants.  He checks for a particular constant by XORing it
into W, then checking for the result being 0.  That works, but corrupts the
value in W.  He therefore XORs the test value again into W, which restores W
to its original value, then he XORs the new test value into W to be able to
check for zero again.

The optimization is to note that XORing with two successive values is the
same as XORing with the XOR of the two values.  Therefore XORing with the
previous test value to restore W then XORing with the new test value is done
in one step.  He wrote out the two values separately and let the assembler
find the XOR of them to make it easier to see what's going on.

This is not very good programming because each test value is now in two
places, leaving open the possibility of making a mistake and getting them
out of sync.  If I were to use this successive XOR technique, I would make a
macro you pass the test value and the associated return value to.  The macro
would use a assembly time variable to track the previous test value and
compute the new combined XOR value.  The result would be easier to read and
more maintainable, and you know that the previous value is always XORed out
correctly.  The equivalent code to the snippet above would look something
like:

    check_val h'1C', cvar_29     ;CV541
    check_val h'02', max_bright  ;CV515


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2009\01\06@181548 by Jinx

face picon face
>     xorlw  (0x05 ^ 0x20)    ; CV545

Harry, as Maarten said, the 5 restores W, and 20 is the next test. Here's
one I use for checking a character in a serial string. The commented
XORLW values are what are executed (ie what's in Program Memory).

        movfw   indf0
chk_n    xorlw   "N"  ;XORLW 0x4e
        bnz     chk_f        ;if not "N"
        bsf     off_on       ;else set to "device on"
        bra     ss

chk_f    xorlw   "N"^"F"  ;XORLW 0x08
        bnz     chk_st       ;if not "F"
        bra     ss           ;else leave as "device off"

chk_st   xorlw   "F"^"S"      ;XORLW 0x15
        bz      st_req       ;status request
        bra     ss

If you follow the bit patterns in binary you'll see how it works, for example

W ^ 0x4e
W ^ 0x4e then W ^ 0x46

 4e 01001110
^ 46 01000110
 --------------
 08 00001000

Here's another one

goes_hi  movfw   dev_no      ;set test bit, goes high
        xorlw   .1
        bnz     h2
        movlw   .1
        iorwf   instath
        bra     in_rdy
h2       xorlw   .1^.2       ;restore, test new value
        bnz     h3
        movlw   .2
        iorwf   instath
        bra     in_rdy
h3       xorlw   .2^.3
        bnz     h4



2009\01\06@184849 by Jinx

face picon face
> >     xorlw   (0x08 ^ 0x1C)      ; CV541
> >     btfsc   STATUS,Z
> >     retlw   CVAR_29
> >     xorlw   (0x1C ^ 0x02)     ; CV515
> >     btfsc   STATUS,Z
> >     retlw   MAX_BRIGHT

> This is not very good programming because.......

What you said Olin and also it would be much more readable if all
those ghastly btfsc STATUS,Z were replaced with skpnz

Being so many, a simple lookup/comparison table might be better

2009\01\06@185838 by Jan-Erik Soderholm

face picon face
Olin Lathrop wrote:
{Quote hidden}

Or one could EQU some constants to the actual values and
use them instead :

CV111  equ  h'08'             ; or whatever it was...
CV541  equ  h'1C'
CV515  equ  h'02'

  xorlw   (CV111 ^ CV541)     ; CV541
  btfsc   STATUS,Z
  retlw   CVAR_29
  xorlw   (CV541 ^ CV515)     ; CV515
  btfsc   STATUS,Z
  retlw   MAX_BRIGHT


Then each value is in only one place and can be reused
in other places in the code easily, if needed.

Jan-Erik.

2009\01\06@195429 by William \Chops\ Westfield

face picon face

>> Harry H. Arends wrote:
>>>    xorlw   (0x08 ^ 0x1C)      ; CV541
>>>    btfsc   STATUS,Z
>>>    retlw   CVAR_29
>>>    xorlw   (0x1C ^ 0x02)     ; CV515
>>>    btfsc   STATUS,Z
>>>    retlw   MAX_BRIGHT
>>

>> This is not very good programming

But they're such LOVELY comments.  Not!

BillW

2009\01\07@044628 by Harry H. Arends
flavicon
face
A jump table isent a possibilty becourse not all CV's are used.

Harry
{Quote hidden}

why
> > > someone would do this?  To answer that, we need to see a few
more
> > > lines around this one.
> > >
> > >
> > >
> ********************************************************************
> > > Embed Inc, Littleton Massachusetts,
> http://www.embedinc.com/products
> > > (978) 742-9014.  Gold level PIC consultants since 2000.
> > > --
>

2009\01\07@044927 by Harry H. Arends

flavicon
face
I wil give it a try with youre solution. Thanks

Harry

{Quote hidden}

> -

2009\01\07@051725 by Jan-Erik Soderholm

face picon face
Harry H. Arends wrote:
> I wil give it a try with youre solution. Thanks
>

Why ? Doesn't the original code work ?



{Quote hidden}

>> --

2009\01\07@053852 by Tamas Rudnai

face picon face
Hi Harry,

Once I did something like this (It is not perfect as it does not care of
page boundaries but might help):

switch  macro   file
       variable swtch = 0
       movf    file,W
       endm

case    macro   l, addr
       xorlw   l ^ swtch
       btfsc   STATUS,Z
       goto    addr
swtch = l
       endm

;---- testing ----
       switch  TEST
       case    b'11110000', FIRST-CASE
       case    b'11111000', SECOND-CASE
       case    b'11111110', THIRD-CASE
       goto    NON-OF-THEM


You can replace the "goto addr" with returns easily.

Tamas



On Wed, Jan 7, 2009 at 9:49 AM, Harry H. Arends <RemoveMEh.arendsspam_OUTspamKILLspamhome.nl> wrote:

{Quote hidden}

2009\01\07@063324 by Harry H. Arends

flavicon
face
It does but it may be reducing the amount memory used.
And using more CV's easier.

{Quote hidden}

XORed
{Quote hidden}

2009\01\07@071906 by Jan-Erik Soderholm

face picon face
Harry H. Arends wrote:
> It does but it may be reducing the amount memory used.
> And using more CV's easier.

I'm not sure that the macro solution sugested by
Olin would save any code space, it mainly makes
the code a bit more readable, but as I understand
it, the expanded macro would take more or less
the same code space as before.

Jan-Erik.



{Quote hidden}

2009\01\07@073252 by Tamas Rudnai

face picon face
> I'm not sure that the macro solution sugested by
> Olin would save any code space,

Only if the macro is 'clever enough' to recognise sequential elements and
replaces the xorlw/btfss/retlw code pattern to decfsz/retlw. Or even more if
it recognises larger sequences and replaces the code to jump table. Not sure
if that is possible with the original MPLAB macros or with Olin's macro
preprocessor. A HLL compiler is a better candidate for doing such
optimisations in my opinion.

Tamas



On Wed, Jan 7, 2009 at 12:18 PM, Jan-Erik Soderholm <
TakeThisOuTjan-erik.soderholm.....spamTakeThisOuTtelia.com> wrote:

{Quote hidden}

>

2009\01\07@080115 by Michael Rigby-Jones

picon face


> -----Original Message-----
> From: TakeThisOuTpiclist-bouncesspamspammit.edu [piclist-bouncesEraseMEspammit.edu] On
Behalf
> Of Harry H. Arends
> Sent: 07 January 2009 09:46
> To: 'Microcontroller discussion list - Public.'
> Subject: RE: [PIC] xorlw question
>
> A jump table isent a possibilty becourse not all CV's are used.
> #

That doesn't prevent you using a jump table though?

Regards

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

2009\01\07@081256 by olin piclist

face picon face
Harry H. Arends wrote:
> A jump table isent a possibilty becourse not all CV's are used.

Of course it's possible.  A direct table where the index is the value the
existing code is checking would then be sparse, but certainly possible.
Whether that is a good idea is a matter of how sparse and tradeoffs between
program memory, execution speed, and other factors we haven't been told.

Then there are other forms of tables, like entries with value/address you
search thru.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2009\01\07@081618 by olin piclist

face picon face
Harry H. Arends wrote:
> I wil give it a try with youre solution.

Why do you need to touch it at all?  I wasn't advocating a particular
solution, just pointing out that what was there was poorly written.  That
doesn't mean it needs to be "fixed" if it's working.  While the existing
code is messy and hard to maintain, the logic appears to be correct.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2009\01\07@082010 by olin piclist

face picon face
Harry H. Arends wrote:
> It does but it may be reducing the amount memory used.

The macro method I suggested produces exactly the same instructions, just in
a easier to understand and maintain manner.

> And using more CV's easier.

If you do have to change it, then converting it once to use the macro method
is probably worth it.


********************************************************************
Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products
(978) 742-9014.  Gold level PIC consultants since 2000.

2009\01\07@092531 by Jan-Erik Soderholm

face picon face
Olin Lathrop wrote:
> Harry H. Arends wrote:
>> I wil give it a try with youre solution.
>
> Why do you need to touch it at all?  I wasn't advocating a particular
> solution, just pointing out that what was there was poorly written.  That
> doesn't mean it needs to be "fixed" if it's working.  While the existing
> code is messy and hard to maintain, the logic appears to be correct.
>

If if was *my* code, I'd simply replace the numeric constants
in the code with EQU'ed symbolic constants instead. That
makes it a little easier to read and much easier/safer
if the code for a particular "CVxxx" has to change.
Each CVxxx value is in a single place. And it's no
logical changes in the code itself, so the change
is rather safe to do.

2009\01\07@105359 by William \Chops\ Westfield

face picon face

On Jan 7, 2009, at 2:38 AM, Tamas Rudnai wrote:

{Quote hidden}

This is exactly what Olin was talking about, I think, and looks easy  
enough to modify.  Replacing the goto's with retlw will bypass the  
page boundry issues as well.

Notice how much easier the resulting code is to read and understand!

BillW

2009\01\07@114046 by Harry H. Arends

flavicon
face
I may use it to make the code more readable when documenting the code
afterwards.

Harry
{Quote hidden}

> -

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