> Hi All, > > I browsed the PICList source library, but couldn't find anything > similar to what I was looking for. > > I have a buffer of "packed" "bytes" where each byte is less that > 8-bits in length. > > This buffer is generated by capturing a bitstream (LSB first) and > right shifting in to the LSB of a buffer. > > e.g. for bytes sized 7 bits, if the first byte is all ones, the second > is all zeros, and the third is all ones, > my buffer looks as such: > > 01111111 > 11000000 > xxx11111 > xxxxxxxx > ... > > What I would like to do is break each "byte" out of the buffer and put > it into another buffer, e.g. > > 01111111 > 00000000 > 01111111 > 0xxxxxxx > 0xxxxxxx Hi Scott, Here's an untested 18F version: lfsr 0,packedBuffer ; Source lfsr 1,packedBuffer ; Source (used as a temporary) lfsr 2,unpackedBuffer ; Destination movlw numberOfBytes movwf count loop: movf indf0,W ; Get the next packed byte andlw 0x7f movwf postinc2 ; And save it unpacked movf count,W ; Are more than 7 bytes left? sublw 7 skpc bra rotate7 ; There are more than 7 left so rotate them addwf WREG,W ; multiply (7-count) by 2 addlw low(rotate7) ; And only rotate what's remaining skpnc incf PCLATH,F movwf PCL rotate7: rlcf postinc0,F rlcf postinc0,F rlcf postinc0,F rlcf postinc0,F rlcf postinc0,F rlcf postinc0,F movf postinc1,W ; Increment FSR1 (don't care about W) movff FSR1L, FSR0L movff FSR1H, FSR0H decfsz count,F bra loop return Here's the untested C-version void unpackBuffer(unsigned char *pSrc, unsigned char *pDest, unsigned char size) { unsigned char i,j; for (i=0; i 7 ? 7 : i; // The MSB needs to be moved to the LSB of // of the next byte carryOut = (*pSrc++ & 0x80) ? 1 : 0; pTemp = pSrc; while (j) { carryIn = carryOut; // Get the carry for the next byte carryOut = (*pTemp & 0x80) ? 1 : 0; // rotate this byte and pick up carry from previous *pTemp = (*pTemp << 1) + carryIn; // Point to the next pTemp++; } } I imagine the assembly version is a little faster. Scott -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist