Searching \ for 'Two puzzles for you, All ( Probably useful trick' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=two+puzzles+you
Search entire site for: 'Two puzzles for you, All ( Probably useful trick'.

Truncated match.
PICList Thread
'Two puzzles for you, All ( Probably useful trick'
1997\03\24@023544 by Dmitry Kiryashov

flavicon
face
Hi All. Two programmer's trick puzzles are offer .

Puzzle N1>   Is required to exchange W with some file REGister .

That one will be winner_1 who will find shortest solution with minimal
;)
additional regs using .

Puzzle N2>   Is required to load any file REGister with value = VALUE ,
but W register haven't be changed after load is over .

The same requrement to winner_2 ;)

WBR Dmitry.

P.S. I've find shortest solutions - Do you find it too ?

1997\03\24@024958 by Andres Djordjalian

flavicon
face
Hi Dmitri!

> Puzzle N1>   Is required to exchange W with some file REGister .

XORWF reg,W
XORWF reg,f
XORWF reg,W

> Puzzle N2>   Is required to load any file REGister with value = VALUE ,
> but W register haven't be changed after load is over .

MOVWF reg
XORLW VALUE
XORWF reg,f
XORWF reg,W

> P.S. I've find shortest solutions - Do you find it too ?

You tell me. Bye!
                                                             Andres

1997\03\24@085127 by Dmitry Kiryashov

flavicon
face
Andres Djordjalian wrote:
>
> Hi Dmitri!
>
> > Puzzle N1>   Is required to exchange W with some file REGister .
>
> XORWF reg,W
> XORWF reg,f
> XORWF reg,W
Greatly! Bit to bit identity ;)

> > Puzzle N2>   Is required to load any file REGister with value = VALUE ,
> > but W register haven't be changed after load is over .
>
> MOVWF reg
> XORLW VALUE
> XORWF reg,f
> XORWF reg,W
I've another similar solution...

XORLW VALUE
MOVWF REG
XORLW VALUE
XORWF REG,F

WBR Dmitry.

1997\03\24@231145 by John Payson

picon face
> > > Puzzle N2>   Is required to load any file REGister with value = VALUE ,
> > > but W register haven't be changed after load is over .
> >
> > MOVWF reg
> > XORLW VALUE
> > XORWF reg,f
> > XORWF reg,W
> I've another similar solution...
>
> XORLW VALUE
> MOVWF REG
> XORLW VALUE
> XORWF REG,F

Depending upon /Value/, it may be possible to do the job in 1 to 5 cycles
without altering W _or_ the flags (useful perhaps in an ISR, since saving
context is such a pain).  If the old value in the f-register is known, then
any change may be done in 0 to 4 cycles (zero of course being if you want to
"change" the register to its existing value).

Here are some of the ways of setting a register in very few instructions:

[1] Set to zero: the obvious one...
       clrf    reg

[2] Set to a power of two.  Also pretty obvious...
       clrf    reg
       bsf     reg,bit

[3] Set to 255.  Still pretty obvious, but for one slight detail...
       clrf    reg
       decfsz  reg     ; Note: next instruction will NEVER be skipped.
                       ;   "decf" would affect flags.

[4] Right-aligned group of bits set [e.g. bits 0-5]...
       clrf    reg
       bsf     reg,6
       decfsz  reg

[5] Left-aligned group of bits set [e.g. bits 4-7]...
       clrf    reg
       decfsz  reg
       bcf     reg,4
       incfsz  reg

[6] Occasionally-useful method: [e.g. for %11110001]
       clrf    reg
       bsf     reg,5
       decfsz  reg
       swapf   reg

[7] General method: 2-4 bits set
       clrf    reg
       bsf     reg,bit1
       bsf     reg,bit2
       bsf     reg,bit3        [if needed]
       bsf     reg,bit4        [if needed]

[8] General method: 5-7 bits set:
       clrf    reg
       decfsz  reg
       bcf     reg,unbit1
       bcf     reg,unbit2      [if needed]
       bcf     reg,unbit3      [if needed]

Slightly more than half of the possible values from 0-255 can be done in
four or fewer cycles using one of the above methods; the remainder will
take five.  As far as I can tell there are no methods shorter than the
above which do not affect flags or W; can anyone find any potentially-use-
ful cases I missed?

1997\03\25@035743 by Wolfram Liebchen

flavicon
face
At 21:58 24.03.97 -0600, you wrote:
{Quote hidden}

Doesn't CLRF alter the Z-flag?!

>
>[2] Set to a power of two.  Also pretty obvious...
>        clrf    reg
>        bsf     reg,bit
>
>[3] Set to 255.  Still pretty obvious, but for one slight detail...
>        clrf    reg
>        decfsz  reg     ; Note: next instruction will NEVER be skipped.

Don't we have SETF reg ? (Perhaps only 17Cxxx)

regards,

Wolfram



+-----------------------------------------------------+
| Wolfram Liebchen                                    |
| Forschungsinstitut fŸr Optik, TŸbingen, Deutschland |
| spam_OUTliebchenTakeThisOuTspamffo.fgan.de                         |
+-----------------------------------------------------+

1997\03\25@204814 by John Payson

picon face
{Quote hidden}

I thought [perhaps I need to get out my book] that clrf only affected the Z
flag on the 17Cxx parts.  Hmm... I'll have to double-check that.

> >[2] Set to a power of two.  Also pretty obvious...
> >        clrf    reg
> >        bsf     reg,bit
> >
> >[3] Set to 255.  Still pretty obvious, but for one slight detail...
> >        clrf    reg
> >        decfsz  reg     ; Note: next instruction will NEVER be skipped.
>
> Don't we have SETF reg ? (Perhaps only 17Cxxx)

Not on any of the 16Cxx devices.

> regards,
>
> Wolfram

Hmm... would be embarassing if I'm wrong on /clrf/.

1997\03\25@215648 by John Payson

picon face
> >> > MOVWF reg
> >> > XORLW VALUE
> >> > XORWF reg,f
> >> > XORWF reg,W
> >> I've another similar solution...
> >>
> >> XORLW VALUE
> >> MOVWF REG
> >> XORLW VALUE
> >> XORWF REG,F

Okay, I'll embarrass myself again on this problem by observing that the
latter solution has some advantages over the former:

[1] It may safely be used on registers in which some bits will not read back
   as written (e.g. I/O ports).  The bits that read back incorrectly won't
   necessarily be set right, but they won't affect the value in W either.

[2] It may be extended to set several locations to the same or different
   values.  The cost, in instructions, is 2 cycles per location, plus 1 per
   "distinct" value, plus 1.  Note that if two of the values written XOR to
   a third, then the third value is a freebie.  Thus, suppose it's desired
   to store 85 into R85a and R85b, 15 into R15, and 90 into R90.  Since 85
   xor 15 is 90, you could use the following:

       xorlw   90
       movwf   R90
       movwf   R15
       xorlw   15
       xorwf   R15
       movwf   R85a
       movwf   R85b
       xorlw   85
       xorwf   R85a
       xorwf   R85b
       xorwf   R90

   Eleven instructions to do the four loads.  Not too bad (though if a spare
   location were available, it could have been done in nine)

1997\03\26@065546 by Dmitry Kiryashov

flavicon
face
Hi All!

There are some more trick(idea).

....
SUBWF REG,W
SUBWF REG,F
ADDWF REG,W
....
the code sequency above may be used to exchange W with some REG(s)
at one time with comparision greater_or_equal or so.

For example:

SUBWF REG0,W ;REG0(old)-W
SKPC         ;REG0(old)>=W
GOTO LABEL1

SUBWF REG0,W ;restore i.e. REG0-(REG0-W)=W
SUBWF REG1,W
SKPC         ;REG1>=W
GOTO LABEL2

SUBWF REG1,W ;restore W
.....

LABEL1:
SUBWF REG0,F ;REG0(new)=REG0(old)-(REG0(old)-W)=W
ADDWF REG0,W ;W=REG0(new)+(REG0(old)-W)=REG0(old)
....

LABEL2:
SUBWF REG1,F ;=W
ADDWF REG1,W ;=REG1(old)
....

WBR Dmitry.

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