At 12:58 AM 3/19/00 -0500, you wrote: This is what I use for my 17Cxx programmer that also has the i/o lines swapped. This is optimized for space but is considerably slower than other solutions. However it is up to you to decide your priorities. ;************************************** ;SPIN ;enter byte to reverse in abuff. ;exit with reversed byte in bbuff ;KnownZero must be zero of course. Code will leave knownZero as 0 ;(Do NOT use with interrupt driven code that may use KnownZero!) bsf KnownZero,3 ;count = 8 yyr6 rrf abuff rlf bbuff decfsz KnownZero goto yyr6 Jim >Nickolei's solution edited out for brevity. > >> >> On Friday, March 17, 2000 Andy Baker wrote: >> > Dear All, >> >> > Another one of those algorithm challenges (apologies if this is a well known >> > one): >> >> > How do I reverse the bits in an 8 bit register in as few instructions as >> > possible? >> >> > i.e. you start with a byte 'abcdefgh' and end up with 'hgfedcba'. >> >> > The reason I'd like to know is that I've got my data lines to an LCD display >> > reversed on my PCB,, but don't want to redesgin the PCB as it is otherwise >> > OK. >> >> > Cheers, >> >> > Andy > >Actually Andy your specification is somewhat vague. Does the few instructions >refer to actual instructions executed or total instruction size of the code. > >For example the absolute shortest execution time is simply to have a 256 >byte lookup table and jump to the appropriate entry. No more than 6 >instructions executed taking 10 cycles but the total code cost is 260 >instructions in memory. > >Let's see if I can come up with a quick compromise that utilizes the >swapf instruction and a 16 byte lookup table: > >swapbyte: > movf byte,W ; get the low nybble > andlw 15 ; And mask it off > call reverse ; reverse the nybble > movwf out ; Save it > swapf out,F ; Swap to high position > swapf byte,W ; Get the high nybble > andlw 15 ; Mask > call reverse ; reverse > iorwf out,F ; Merge with other nybble > return > >reverse: > addwf PCL,F ; jumptable > retlw 15 ; reverse of 0 > retlw 14 ; reverse of 1 > ... > retlw 0 ; reverse of 15 > >Let's see the mainline code takes 14 cycles. The reverse takes 4. So >Nickolei's code is faster. > >BAJ > Regards, Jim Robertson NEWFOUND ELECTRONICS ________________________________________ Email: newfound@pipeline.com.au http://www.new-elect.com MPLAB compatible PIC programmers. ________________________________________