=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Date: Tue, 21 Mar 2000  00:28:35
    From: Dmitry Kiryashov 
      To: PICLIST@MITVMA.MIT.EDU
 Subject: Re: Reversing the order of bits in a register.
--------------------------------------------------------------------------------

Hi Scott and Nikolai and others ;-) Sorry I was a little bit busy...

I guess we guys always have some fun solving some challenges ;-)

Here is procedure I've done for myself recently to overcome the
same situation.

;       Input X  = abcdefgh , Output X = hgfedcba
;       Written by Dmitry A. Kiryashov 2000
;       12 clocks/words

reverse8bit:

        swapf   X,W     ;efghabcd
        xorwf   X,W     ;efghabcd
                        ;abcdefgh

        andlw   0x66    ;.fg..bc.
                        ;.bc..fg.

        xorwf   X,F     ;afgdebch
;
        rrf     X,W
        rrf     X,F     ;hafgdebc
;
        andlw   0x55    ;.a.g.e.c
        addwf   X,F     ;h.f.d.b.
                        ;a.g.e.c.
        rrf     X,F     ;.h.f.d.b
                        ;.a.g.e.c

        addwf   X,F     ;ahgfedcb
;
        rlf     X,W
        rlf     X,F     ;hgfedcba
                        ;it can be replaced
                        ;with rlf X,W
                        ;if necessary...

WBR Dmitry.


----------


Nikolai Golovchenko wrote:
>
> On Sunday, March 19, 2000 Scott Dattalo wrote:
> > For a single byte there is a 14-cycle solution. But since it looks like your
> > having fun, I wouldn't want to spoil anything. :)
>
> I have more fun to learn from PICLIST members :)
>
> > BTW, John Payson posted the original solution (for reversing 7 bits). Dmitry
> > optimized it slightly (again for 7 bits). John's trick is applicable here as
> > well.
>
> > Hint, for the general case, it takes 4 cycles to swap 2 bits. However, it only
> > takes 2 cycles for the first two bits.
>
> > Scott
>
> AFAIR, Dmitry used swap to place two bits in place, and then xor'd to
> swap individual pairs of bits.
>
> How about this:
> ;In =   abcdefgh
> ;Out =  hgfedcba
>
>         rlf In, w       ;carry = a
>         swapf In, f     ;In = efghabcd
>         rlf In, w       ;w  = fghabcda  (-g---c-a), carry = e
>
>         andlw 0xEF
>         skpnc
>         iorlw 0x10      ;w  = fghebcda  (-g-e-c-a)
>
>         btfsc In, 6
>         xorlw 0xA0
>         btfsc In, 4
>         xorlw 0xA0      ;w  = hgfebcda  (hgfe-c-a)
>
>         btfsc In, 2
>         xorlw 0x0A
>         btfsc In, 0
>         xorlw 0x0A      ;w  = hgfedcba
>
> ;14 instructions so far
> ;1 more to copy w to Out
>         movwf Out
>
> >From the practical point of view, it's better to have ability to
> reassign pins in any order. I use pins masks always - it's easier to
> route PCB then.
>
>  Nikolai


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Date: Ïí, 20 ìàð 2000  15:29:49
    From: Scott Dattalo 
      To: Nikolai Golovchenko 
 Subject: Re: Reversing the order of bits in a register.
--------------------------------------------------------------------------------



On Mon, 20 Mar 2000, Nikolai Golovchenko wrote:

> On Sunday, March 19, 2000 Scott Dattalo wrote:
> > For a single byte there is a 14-cycle solution. But since it looks like your
> > having fun, I wouldn't want to spoil anything. :)
> 
> I have more fun to learn from PICLIST members :)
> 
> > BTW, John Payson posted the original solution (for reversing 7 bits). Dmitry
> > optimized it slightly (again for 7 bits). John's trick is applicable here as
> > well.
> 
> > Hint, for the general case, it takes 4 cycles to swap 2 bits. However, it only
> > takes 2 cycles for the first two bits.
> 
> > Scott
> 
> AFAIR, Dmitry used swap to place two bits in place, and then xor'd to
> swap individual pairs of bits.

yep.

> 
> How about this:
> ;In =   abcdefgh
> ;Out =  hgfedcba
> 
>         rlf In, w       ;carry = a
>         swapf In, f     ;In = efghabcd
>         rlf In, w       ;w  = fghabcda  (-g---c-a), carry = e
> 
>         andlw 0xEF
>         skpnc
>         iorlw 0x10      ;w  = fghebcda  (-g-e-c-a)
> 
>         btfsc In, 6
>         xorlw 0xA0
>         btfsc In, 4
>         xorlw 0xA0      ;w  = hgfebcda  (hgfe-c-a)
> 
>         btfsc In, 2
>         xorlw 0x0A
>         btfsc In, 0
>         xorlw 0x0A      ;w  = hgfedcba
> 
> ;14 instructions so far
> ;1 more to copy w to Out
>         movwf Out

That looks like it'll work. Here's what I had in mind:

; In  = abcdefgh
; Out = hgfedcba
    rlf     In,w
    rlf     In,w     ; bcdefgha  (---e---a)

    btfsc   In,0     ; swap b and h (magnetism?)
     xorlw  0x82
    btfsc   In,6
     xorlw  0x82     ; hcdefgba  (h--e--ba)

    btfsc   In,1     ; swap c and g
     xorlw  0x44
    btfsc   In,5
     xorlw  0x44     ; hgdefcba  (hg-e-cba)

    btfsc   In,2     ; swap d and f
     xorlw  0x28
    btfsc   In,4
     xorlw  0x28     ; hgfedcba

On an 18cxxx, you can shave a cycle by:

; In  = abcdefgh
; Out = hgfedcba
    rlfnc   In,w     ; bcdefgha

Scott