11.10. 11.10 Reversing a Bit String

Another common programming project instructions assign, and a useful function in its own right, is a program that reverses the bits in an operand. That is, it swaps the L.O. bit with the H.O. bit, bit 1 with the next-to-H.O. bit, and so on. The typical solution an instructor probably expects for this assignment is the following:

// Reverse the 32-bits in EAX, leaving the result in EBX:

                  mov( 32, cl );
RvsLoop:          shr( 1, eax );   // Move current bit in EAX to the carry flag.
                  rcl( 1, ebx );   // Shift the bit back into EBX, backwards.
                  dec( cl );
                  jnz RvsLoop

As with the previous examples, this code suffers from the fact that it repeats the loop 32 times for a grand total of 129 instructions. By unrolling the loop you can ...

Get Art of Assembly Language, 1st 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.