10.9 Reversing a Bit String
Another common programming project instructors 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 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.