At 10:50 AM 29/10/97 -0500, you wrote: >There hasn't been a "programming challenge" lately. Here's a problem I'm >looking at: I have a 7 bit ASCII character which was transmitted >serially LSB first, but received MSB first. Thus, I need to reverse the >order of the bits (I can't change the receiver, since other parts of the >data are transmitted MSB first). For example, 0011010 should come out as >0101100. The routine should be a subroutine which gets the input value >in W and returns the result in W. It is OK to assume the high bit of W >will be 0, and the high bit of the result must be 0 (the routine works >only on the low 7 bits) > >The obvious answer is a lookup table of 128 entries, but this is a big >portion of the code space in an '84. Speed is not important, so I'm >going to replace the lookup table with an "analytical" method. Here's a >completely arbitrary scoring scheme which favors using less ROM and RAM >(low score is better): > >Each ROM location used : 8 points >Each RAM location used : 30 points >Each instruction cycle (to convert once): 1 point >Uses less than 25 instruction cycles: -40 points a real dirty solution: revbits movwf tmp andlw 0x08 btfsc tmp,0 iorlw 0x40 btfsc tmp,1 iorlw 0x20 btfsc tmp,2 iorlw 0x10 btfsc tmp,4 iorlw 0x04 btfsc tmp,5 iorlw 0x02 btfsc tmp,6 iorlw 0x01 return RAM 1 * 30 = 30 ROM 15 * 8 = 120 Cycle 16 * 1 = 16 < 25 -40 ================== SCORE: 126 should be Correct now :) antti http://avrbasic.com -- AVR Basic Compiler http://sistudio.com/bswfe -- Basic Stamp Windows Front End