> Right after finished my embedded systems class in college, a friend of > mine was taking it from a different prof. The professor gave them a > challenge: > > In the shortest possible time, reverse the order of the bits in a random > data register. Assume this is the time critical element of the > application, and nothing about the application is as important as > getting the order of these bits reversed as quickly as possible. > > Assume the processor is a 16F876. > > My friend came up with the fastest possible means I've yet seen. What > say you all? OK, here's the ugly brute force approach: ; Subroutine to flip the bits in REG0 and return them in W. ; glbsub flipit, noregs movlw high table ;set high address of table entry movwf pclath movf reg0, w ;get the byte value to flip movwf pcl ;jump to selected table entry, returns from FLIPIT table code However, I think there are far more elegant methods making clever use of SWAP, XOR, etc. In a normal application where this isn't a critical function, I would prefer the clearest and most undestandable approach. Usually a few extra instructions are cheaper in the long run if they decrease the chance that some green programmer three years later long after you've moved on doesn't mess it up and spend two days trying to figure out why something obscure broke. University professors don't usually appreciate this. While these kinds of brain teasers can be good mental exercises and help you think about using the instruction set in different ways, they also create the false impression that this sort of programming is valuable or even a good idea. Most of the time it's neither. That said, my first knee jerk reaction would be to shift one register one bit at a time into C, then shift the other one in from the same direction. Yes, I'm quite sure there are faster and smaller methods, but this method is both cheap to write and maintain. I can probably write this loop (including comments of course) or the unrolled version in under a minute. Most of the time it wouldn't be in my customer's interest to spend 5 minutes creating an optimized version. ***************************************************************** Embed Inc, embedded system specialists in Littleton Massachusetts (978) 742-9014, http://www.embedinc.com -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu