10.11 Extracting Bit Strings
Of course, we can easily accomplish the converse of merging two bit streams; that is, we can extract and distribute bits in a bit string among multiple destinations. The following code takes the 32-bit value in EAX and distributes alternate bits among the BX and DX registers:
mov( 16, cl ); // Count the loop iterations. ExtractLp: shr( 1, eax ); // Extract even bits to (e)bx. rcr( 1, ebx ); shr( 1, eax ); // Extract odd bits to (e)dx. rcr( 1, edx ); dec( cl ); // Repeat 16 times. jnz ExtractLp; shr( 16, ebx ); // Need to move the results from the H.O. shr( 16, edx ); // bytes of ebx/edx to the L.O. bytes.
This sequence executes 99 instructions. This isn't terrible, but we can probably do a little better by using an algorithm ...
Get The Art of Assembly Language, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.