Searching \ for 'Efficient way to reverse a byte?' 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=efficient+way+reverse
Search entire site for: 'Efficient way to reverse a byte?'.

Truncated match.
PICList Thread
'Efficient way to reverse a byte?'
1999\10\29@095055 by Jim Main

flavicon
face
I'm sure I saw an easy way to reverse a byte some time ago in this list, but
I've forgotten it..

I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0 b1 b2 b3 b4 b5 b6 b7.

Quickest & most code efficient??

Jim
---

1999\10\29@100127 by Wagner Lipnharski

picon face
A nibble table look ahead, just 16 bytes, or 8 if you accept to deal
with 4 bits tables.
For top possible speed at cost of some memory, a 256 bytes look ahead
table.
Wagner

Jim Main wrote:
{Quote hidden}

1999\10\29@110228 by Adam Bryant

picon face
Jim,
Your choices are likely to be fast execution and big code usage OR slow
execution and small code usage.

As Wagner suggested, fastest would be a 256 byte lookup table (but it
uses up 256 bytes for  the lookup table).

A small, slower approach is to rotate the source byte right one bit
(putting the rightmost bit in the carry flag), then rotate the
destination byte left one bit (putting the carry flag in the rightmost
bit of your destination register).  Repeat this cycle 8 times.  Your
destination byte will have to be a temp register, then when done move the
temp register back to the original source register.  I would estimate
this could be done in 10 to 15 bytes of code and 50 to 70 instruction
cycles.

Obviously I am thinking like the C programmer I am and someone like
Dimitry could probably give you a method for accomplishing the same thing
in 5 bytes of code and 10 cycles. :-)

Hope this helps,
Adam

On Fri, 29 Oct 1999 14:48:54 +0100 Jim Main <spam_OUTj.mainTakeThisOuTspamNET.NTL.COM> writes:
{Quote hidden}

Adam Bryant (age 0x23)
.....abryantKILLspamspam@spam@peaktech.com (work)
adamdbspamKILLspamjuno.com (home)
Parker, CO, USA
Robotics, RC Airplanes, anything using a PIC

___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: dl.http://www.juno.com/dynoget/tagj.

1999\10\29@111304 by Douglas Burkett

flavicon
face
If I'm not mistaken you could use a 16 byte lookup for the nybbles and then
use the efficency of the swap instruction, quicker I believe than rotating
all 8 bits and less code space than the full lookup table.

Doug

{Original Message removed}

1999\10\29@112521 by Dan Larson

flavicon
face
I colleague of mine has extoled the virtues of
using lookup tables as a means of improving
speed for as long as I can remember.  Just about
every time I have ever asked him, "How can I make
this faster", he usually replies: "use a table".

Ever wonder how they got so many of those video
games on the 1Mhz "pre-Nintendo-era" game units
to animate smoothly?

I tend to agree that this technique is often
underused.  But, Of course, there are plenty of
situations where the use of a table may not be
practical.

Just my $.02

Dan

On Fri, 29 Oct 1999 17:12:13 +0200, Douglas Burkett wrote:

>If I'm not mistaken you could use a 16 byte lookup for the nybbles and then
>use the efficency of the swap instruction, quicker I believe than rotating
>all 8 bits and less code space than the full lookup table.
>
>Doug
>
>{Original Message removed}

1999\10\29@113138 by Wagner Lipnharski

picon face
Adam Bryant wrote:
> A small, slower approach is to rotate the source byte right one bit
> (putting the rightmost bit in the carry flag), then rotate the
> destination byte left one bit (putting the carry flag in the rightmost
> bit of your destination register).  Repeat this cycle 8 times.  Your
> destination byte will have to be a temp register, then when done move the
> temp register back to the original source register.  I would estimate
> this could be done in 10 to 15 bytes of code and 50 to 70 instruction
> cycles.
>
> Obviously I am thinking like the C programmer I am and someone like
> Dimitry could probably give you a method for accomplishing the same thing
> in 5 bytes of code and 10 cycles. :-)

Adam is correct about the options, but even in assembler:

1) A direct flow routine:
   8 instructions shifting bits out
   8 instructions shifting bits in
   1 instruction swapping bytes
       Total Program Instructions:  17
       Total Instructions Executed: 17

2) A loop routine:
   1 instruction setting loop count = 8
   1 instruction shifting bit out
   1 instruction shifting bit in
   1 instruction decrementing count and looping back
   1 instruction to swap bytes
       Total Program Instructions:  5
       Total Instructions Executed: 26

3) Lookahead table (16 bytes of nibbles):
   1 instruction to set the lookahead base address
   1 instruction to save the original byte
   1 instruction to AND the original byte lower nibble
   1 instruction to get the table nibble
   1 instruction to save this table byte
   1 instruction to retrieve the original byte
   1 instruction to swap original byte nibbles
   1 instruction to AND the original byte lower nibble
   1 instruction to get the table nibble
   1 instruction to OR with previous table nibble
   1 instruction to swap final byte nibbles
        Total Program Instructions: 11 + 16 (table)
        Total Instructions Executed: 11

4) Lookahead table (256 bytes):
   1 instruction to set the lookahead base address
   1 instruction to get the table byte
        Total Program Instructions: 2 + 256
        Total Instructions Executed: 2

As you can see, execution speed cost a lot, always.

1999\10\29@115303 by Jim Main

flavicon
face
> 3) Lookahead table (16 bytes of nibbles):
>     1 instruction to set the lookahead base address
>     1 instruction to save the original byte
>     1 instruction to AND the original byte lower nibble
>     1 instruction to get the table nibble
>     1 instruction to save this table byte
>     1 instruction to retrieve the original byte
>     1 instruction to swap original byte nibbles
>     1 instruction to AND the original byte lower nibble
>     1 instruction to get the table nibble
>     1 instruction to OR with previous table nibble
>     1 instruction to swap final byte nibbles
>          Total Program Instructions: 11 + 16 (table)
>          Total Instructions Executed: 11

I made it  Total Program Instructions: 7 + 19 (table)
             &Total Instructions executed: 7

eg.

       movf    original,w
       call      rev_nibble
       movwf result
       swapf     result,f
       swapf     original,w
       call      rev_nibble
       iorwf    result,f

 rev_nibble
       andlw    0x0F
       addwf    pcl,f
       retlw       00
       retlw       08

           ....etc...

Thanks for all the suggestions..

jim

1999\10\29@135151 by Dwayne Reid

flavicon
face
>I'm sure I saw an easy way to reverse a byte some time ago in this list, but
>I've forgotten it..
>
>I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0 b1 b2 b3 b4 b5 b6 b7.

I've seen a lot of replies, but to me, the simple 'brute force and
ignorance' technique seems to make the most sense.

   clrf    dest
   btfsc   source,0
     bsf   dest,7
   btfsc   source,1
     bsf   dest,6
   btfsc   source,2
     bsf   dest,5
   btfsc   source,3
     bsf   dest,4
   btfsc   source,4
     bsf   dest,3
   btfsc   source,5
     bsf   dest,2
   btfsc   source,6
     bsf   dest,1
   btfsc   source,7
     bsf   dest,0

17 instructions, 17 cycles

dwayne


Dwayne Reid   <.....dwaynerKILLspamspam.....planet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 15 years of Engineering Innovation (1984 - 1999)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Do NOT send unsolicited commercial email to this email address.
My posting messages to Usenet neither grants consent to receive
unsolicited commercial email nor is intended to solicit commercial
email.

1999\10\29@142252 by Terry A. Steen

flavicon
face
How about:

    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in
    rlf arg1,1  ;shift out a bit
    rrf arg2,1    ;shift it in

Kind of long, but this should reverge arg1 and store it in arg2. At the
same time, it will also reverse arg2 and store it in arg1.

this is untried... give it a go egh?

Terry

{Quote hidden}

-----------------------------------------------------------------
Sent from the desk of:
Terry Allen Steen, EE              engineeringspamspam_OUTmarinapower.com
332 McLaws Circle, Ste 111         757-258-8800 (Voice)
Williamsburg, Va   23185           757-258-8805 (FAX)
-----------------------------------------------------------------
!I AM A WHALE MAIL USER! If you have a large file to send, goto
   http://www.whalemail.com    send them to:   mplengineer
       Give me your account and I will use it also
-----------------------------------------------------------------
Visit our web-site: http://www.marinapower.com
-----------------------------------------------------------------

1999\10\29@150614 by Tracy Smith

picon face
>I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0
b1 b2 b3 b4 b5 b6 b7.
>
>Quickest & most code efficient??

How about this... (John Payson)

       movwf   source
       btfsc   source,0
        xorlw  h'41'
       btfsc   source,6
        xorlw  h'41'
       btfsc   source,1
        xorlw  h'22'
       btfsc   source,5
        xorlw  h'22'
       btfsc   source,2
        xorlw  h'14'
       btfsc   source,4
        xorlw  h'14'

Why waste time on lookup tables? Each access is a 6
cycle hit.

.lo

=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

1999\10\29@171106 by paulb

flavicon
face
Tracy Smith wrote:

> How about this... (John Payson)

       movwf   source      a   b   c   d   e   f   g   h
       btfsc   source,0
        xorlw  h'41'       a  b+h  c   d   e   f   g   0
       btfsc   source,6
        xorlw  h'41'       a   h   c   d   e   f   g   b
       btfsc   source,1
        xorlw  h'22'       a   h  c+g  d   e   f   0   b
       btfsc   source,5
        xorlw  h'22'       a   h   g   d   e   f   c   b
       btfsc   source,2
        xorlw  h'14'       a   h   g  d+f  e   0   c   b
       btfsc   source,4
        xorlw  h'14'       a   h   g   f   e   d   c   b

 Cute, but you haven't completed the reverse - a rotate *without*
carry is required.  That's going to take two more cycles at least.
--
 Cheers,
       Paul B.

1999\10\29@174236 by Edson Brusque

face
flavicon
face
John Payson,

   Are you sure this works? I've tried it with CC5X and haven't sucess.

       uns8 source;
       MOVLW (0b.1101.1001); // this value is just for test
       MOVWF (source);
       BTFSC (source.0);
       XORLW (0x41); // 0100.0001
       BTFSC (source.6);
       XORLW (0x41); // 0100.0001
       BTFSC (source.1);
       XORLW (0x22); // 0010.0001
       BTFSC (source.5);
       XORLW (0x22); // 0010.0001
       BTFSC (source.2);
       XORLW (0x14); // 0001.0100
       BTFSC (source.4);
       XORLW (0x14); // 0001.0100

   It is very ingenious, but seens to have a bug somewhere.

   Regards,

   Brusque
___________________________________________________________________________
| | || |\| | || || |\|\ Edson Brusque :-^= (@spam@brusqueKILLspamspamflynet.com.br)
| | || ||| | || || |||| Musician, Tech Consultant, Programmer, Developer
| |_||_||| |_||_||_|||| Blumenau / SC / Brazil / Earth / Solar Syst /
Milk...
| \_\\_\|| \_\\_\\_\||| Giro In'Italia homepage: http://www.flynet.com.br/giro
|  |  |  |  |  |  |  || C.I.Tronics Lighting Designers: http://www.citronics.com.br
|__|__|__|__|__|__|__||----------------- ICQ# 15937748 ---------------------
\__\__\__\__\__\__\__\|    The SoundFont Users Group Mailing List is at
----------------------|  www.geocities.com/SiliconValley/Port/6619/
----------------------------------------------------------------------------

1999\10\29@181113 by Keith Causey

picon face
This would probably waste too much port space but you could reverse the 'w'
register in two instructions if you wired two ports together like this:
RA0->RB7
RA1->RB6
RA2->RB5
RA3->RB4
RA4->RB3
RA5->RB2
RA6->RB1
RA7->RB0
and executed the code sequence
mov  RA,W
mov  w,RB

1999\10\29@185302 by Russell McMahon

picon face
The piece of code that Tracy posted (from John Payson) does what it was
originally meant to do BUT is NOT correct for the present problem.

Here we require
76543210 --> 01234567

John's solution is for a 7 bit swap ignoring B7.
ie x6543210 --> x0123456

As a consequence B7 & B3 is untouched and there are only 3 swaps needed
making the code shorter.
Rewriting it for an 8 bit swap as required here, produces -

       movwf   source

       btfsc   source,0
        xorlw  h'81'
       btfsc   source,7
        xorlw  h'81'
       btfsc   source,1
        xorlw  h'42'
       btfsc   source,6
        xorlw  h'42'
       btfsc   source,2
        xorlw  h'24'
       btfsc   source,5
        xorlw  h'24'
       btfsc   source,3
        xorlw  h'18'
       btfsc   source,4
        xorlw  h'18'

17 instructions, time varies depending on data but fairly quick.

Operation may not be initially obvious.
It implements
   For N = 0 to 3
       If bN = 1 then invert bits bN and b(7-N)
       If b(7-N) = 1 then invert bits bN and b(7-N)
   Next N

Worst case, if both bits in a pair are set it inverts them both twice.
Best case, if neither is set it takes no action.
This suggests that a more cunning test for "both bits set" may allow a small
reduction in run time but intuition suggests that the testing overhead would
be excessive..

John's solution is elegant but a  little hard on the brain.
I prefer Dwayne Reids more brute force -

{Quote hidden}

because it's much easier for trhe average person to see what is happening at
a glance (or a few glances :-) - YMMV).

A full 8 bit lookup table is much quicker and much much less code space
efficient.


A dual nibble lookup table is not much quicker on average and rather less
code efficient.


Lookup tables have the possible advantage of having a constant
implementation time.


     Russell McMahon
_____________________________

>From another world - http://www.easttimor.com

What can one man* do?
Help the hungry at no cost to yourself!
at  http://www.thehungersite.com/

(* - or woman, child or internet enabled intelligent entity :-))

From: Tracy Smith <KILLspamdot_loKILLspamspamYAHOO.COM>

{Quote hidden}

1999\10\29@191917 by Tracy Smith

picon face
> John Payson,

<blush>
I'm NOT John Payson
</blush>


{Quote hidden}

Well it doesn't have a bug, it was intended to reverse
seven bits and not eight. I'm no Payson, but it looks
like for one more cycle you could do this:

       rrf     source,w
       rrf     source,w
       btfsc   source,0
        xorlw  h'41'
       btfsc   source,6
        xorlw  h'41'
       btfsc   source,1
        xorlw  h'22'
       btfsc   source,5
        xorlw  h'22'
       btfsc   source,2
        xorlw  h'14'
       btfsc   source,4
        xorlw  h'14'

.lo


=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

1999\10\29@194305 by Harold M Hallikainen

picon face
On Fri, 29 Oct 1999 15:10:14 -0700 Keith Causey <RemoveMEffightTakeThisOuTspamGEOCITIES.COM>
writes:
>This would probably waste too much port space but you could reverse
>the 'w'
>register in two instructions if you wired two ports together like
>this:
>RA0->RB7
>RA1->RB6
>RA2->RB5
>RA3->RB4
>RA4->RB3
>RA5->RB2
>RA6->RB1
>RA7->RB0
>and executed the code sequence
>mov  RA,W
>mov  w,RB

       Aha!  A hardware solution!  Just like adding a multiplier to the
chip!  Here's bit reverser hardware...  I normally am squeezing
everything I can out of I/O though...

Harold


Harold Hallikainen
spamBeGoneharoldspamBeGonespamhallikainen.com
Hallikainen & Friends, Inc.
See the FCC Rules at http://hallikainen.com/FccRules and comments filed
in LPFM proceeding at http://hallikainen.com/lpfm

___________________________________________________________________
Get the Internet just the way you want it.
Free software, free e-mail, and free Internet access for a month!
Try Juno Web: dl.http://www.juno.com/dynoget/tagj.

1999\10\29@194944 by bill

flavicon
face
> John's solution is elegant but a  little hard on the brain.
> I prefer Dwayne Reids more brute force -

> because it's much easier for trhe average person to see what is happening at
> a glance (or a few glances :-) - YMMV).

The shift-out-shift-in method is also pretty easy to follow.  It is also 1
instruction shorter because you don't have to clear the destination register
at the beginning.

> Lookup tables have the possible advantage of having a constant
> implementation time.

The test-each-bit method and the shift-out-shift-in method also have constant
execute times.


---
                                       Peace,
                                       William Kitchen
                                       TakeThisOuTbillEraseMEspamspam_OUTiglobal.net

The future is ours to create.

1999\10\29@223238 by Dmitry Kiryashov

flavicon
face
Greetings everybody. ;-)

Tracy Smith wrote:
>
> >I want to change   b7 b6 b5 b4 b3 b2 b1 b0   to  b0
> b1 b2 b3 b4 b5 b6 b7.
> >
> >Quickest & most code efficient??
>
> How about this... (John Payson)
>
>         movwf   source

       movfw   source  ;otherwise you put W to source

>         btfsc   source,0
>          xorlw  h'41'
>         btfsc   source,6
>          xorlw  h'41'

ok. swap 0 with 6

>         btfsc   source,1
>          xorlw  h'22'
>         btfsc   source,5
>          xorlw  h'22'

ok. swap 1 with 5

>         btfsc   source,2
>          xorlw  h'14'
>         btfsc   source,4
>          xorlw  h'14'

ok. swap 2 with 4

bit 3 is already on right place.

Here  is another reduced version:

       swapf   source,w        ;3210.654
       btfsc   source,3        ;swap bit 3 back
        xorlw  0x44            ;bit 7 is expected = 0
       btfsc   source,6
        xorlw  0x05
       btfsc   source,4        ;
        xorlw  0x05
       btfsc   source,2
        xorlw  0x50
       btfsc   source,0
        xorlw  0x50

I believe that there is more quick and compact version of this
(without ever table lookups) but I've no time to discover that.

WBR Dmitry.

1999\10\30@065447 by gdaniel

flavicon
face
Keith Causey wrote:

> This would probably waste too much port space but you could reverse the 'w'
> register in two instructions if you wired two ports together like this:

Alternately, wait for this beast:
http://www.atmel.com/atmel/products/prod39.htm
(it's an AVR with dual UART, no ADC, FPGA on chip) looks good for custom
protocol interfacing at high speed, multiple channel, Video processing ....
I want one to play with now, it would make a beaut logic analyser, just add
SIMM & graphics LCD
regards,
Graham Daniel.

1999\10\31@073516 by w. v. ooijen / f. hanneman

picon face
Just out of my head, untested:

clrf Y
bsf Y,0
loop:
rrf X,f
rlf Y,f
skpnc
 goto loop

6 instructions, does not affect W, isosynchronous, not very fast

Wouter


'Efficient way to reverse a byte?'
1999\11\01@072955 by Andy David
flavicon
face
part 0 1626 bytes
- 16 cycles
- 16 words
- uses 1 addition byte or RAM.
- Enter with byte to be reversed in WREG, ends with reversed byte in
WREG.
- Isochronous.

               rlcf    WREG,f          ; W   = d6 d5 d4 d3 d2 d1 d0 C
{C = d7}
               rrcf    Reverse,f       ; Rev = d7 R7 R6 R5 R4 R3 R2 R1
{C = R0}
               rlcf    WREG,f          ; W   = d5 d4 d3 d2 d1 d0 C  R0
{C = d6}
               rrcf    Reverse,f       ; Rev = d6 d7 R7 R6 R5 R4 R3 R2
{C = R1}
               rlcf    WREG,f          ; W   = d4 d3 d2 d1 d0 C  R0 R1
{C = d5}
               rrcf    Reverse,f       ; Rev = d5 d6 d7 R7 R6 R5 R4 R3
{C = R2}
               rlcf    WREG,f          ; W   = d3 d2 d1 d0 C  R0 R1 R2
{C = d4}
               rrcf    Reverse,f       ; Rev = d4 d5 d6 d7 R7 R6 R5 R4
{C = R3}
               rlcf    WREG,f          ; W   = d2 d1 d0 C  R0 R1 R2 R3
{C = d3}
               rrcf    Reverse,f       ; Rev = d3 d4 d5 d6 d7 R7 R6 R5
{C = R4}
               rlcf    WREG,f          ; W   = d1 d0 C  R0 R1 R2 R3 R4
{C = d2}
               rrcf    Reverse,f       ; Rev = d2 d3 d4 d5 d6 d7 R7 R6
{C = R5}
               rlcf    WREG,f          ; W   = d0 C  R0 R1 R2 R3 R4 R5
{C = d1}
               rrcf    Reverse,f       ; Rev = d1 d2 d3 d4 d5 d6 d7 R7
{C = R6}
               rlcf    WREG,f          ; W   = C  R0 R1 R2 R3 R4 R5 R6
{C = d0}
               rrcf    Reverse,w       ; W   = d0 d1 d2 d3 d4 d5 d6 d7
{C = R7}


- Andy.
---------------------------------------------------------
Andrew David, Software Manager, Ultronics Ltd, Cheltenham
RemoveMEakdavidspamTakeThisOuTultronics.co.uk          http://www.ultronics.com


1999\11\01@113015 by Tracy Smith
picon face
--- Andy David <akdavidEraseMEspam.....ULTRONICS.CO.UK> wrote:

> - 16 cycles
> - 16 words
> - uses 1 addition byte or RAM.
> - Enter with byte to be reversed in WREG, ends with
> reversed byte in
> WREG.
> - Isochronous.

This is a variation of something Steve Hardy posted
several years ago.  Except Steve's version would
reverse two bytes and worked on the Midrange pics as
well.


IIRC:

; reverse r1 r2

  rlf  r1,f   ; get carry bit and put r1.7 into carry
  rrf  r2,f   ; get r1.7 and put r2.0 into carry

  rlf  r1,f   ; get r2.0 and put r1.6 into carry
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f
  rrf  r2,f

  rlf  r1,f    ; get r2.7 and put the original
               ; carry bit back.


17 cycles to reverse two bytes

.lo

=====

__________________________________________________
Do You Yahoo!?
Bid and sell for free at http://auctions.yahoo.com

1999\11\01@133601 by paulb

flavicon
face
Andy David wrote:

> - 16 cycles
> - 16 words

 17 if you want to reverse two bytes at once.

> - Enter with byte to be reversed in WREG, ends with reversed byte in
> WREG.

 No, it ends with the reversed byte in W, not WREG as written, or in
Reverse if you specify F as destination.  Unless you're talking of a PIC
with which I'm not familiar.

{Quote hidden}

; 17th instruction tidies everything up:
    rlf    WREG,f          ; WR  = R0 R1 R2 R3 R4 R5 R6 R7 {C = C¡}
--
 Cheers,
       Paul B.

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