11.11 Merging Bit Strings


11.11 Merging Bit Strings

Another common bit string operation is producing a single bit string by merging, or interleaving, bits from two different sources. The following example code sequence creates a 32-bit string by merging alternate bits from two 16-bit strings:

 // Merge two 16-bit strings into a single 32-bit string. // AX - Source for even numbered bits. // BX - Source for odd numbered bits. // CL - Scratch register. // EDX- Destination register.               mov( 16, cl ); MergeLp:      shrd( 1, eax, edx );        // Shift a bit from EAX into EDX.               shrd( 1, ebx, edx );        // Shift a bit from EBX into EDX.               dec( cl );               jne MergeLp; 

This particular example merged two 16-bit values together, alternating their bits in the result value. For a faster implementation of this code, unrolling the loop is probably your best bet because this eliminates half the instructions.

With a few slight modifications, we could also have merged four 8-bit values together, or we could have generated the result using other bit sequences; for example, the following code copies bits 0..5 from EAX, then bits 0..4 from EBX, then bits 6..11 from EAX, then bits 5..15 from EBX, and finally bits 12..15 from EAX:

              shrd( 6, eax, edx );              shrd( 5, ebx, edx );              shrd( 6, eax, edx );              shrd( 11, ebx, edx );              shrd( 4, eax, edx ); 




The Art of Assembly Language
The Art of Assembly Language
ISBN: 1593272073
EAN: 2147483647
Year: 2005
Pages: 246
Authors: Randall Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net