0

I am asked on HW to reverse bits, like mirror flip them so for example 1011 0100 becomes 0010 1101, using shift and rotate combination. I understand how those commands work but I can't think of a way to flip them. Thanks. I need to do it using SAL assembly language.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
user1078719
  • 195
  • 2
  • 3
  • 15

1 Answers1

1

If you need to flip a b-bit word using only shifts, you could emulate a stack:

b times{
  right shift on the input register, setting a carry flag.
  left shift on the output register, reading the carry flag.
}

Note that x86 has the "rotate through carry" instructions - they serve both purposes (or use rotation without carry on the input register to preserve the input). If left shift from carry is not available but right shift from carry is, reverse the words "left" and "right" in the previous algorithm. If no shift from carry is available, you need to emulate by an "ordinary" logical shift followed by setting the correct bit, but...

If you can use AND and OR as well and b is known ahead and is a power of two, there is a faster way. Reverse two bits within each pair, then two pairs within each nibble, then two nibbles within each byte, then two bytes within each word...

for 8-bit x:

                                    //1234 5678
x = (0x55 & x)<< 1 | (0xAA & x)>> 1 //2143 6587
x = (0x33 & x)<< 2 | (0xCC & x)>> 2 //4321 8765
x = (0x0F & x)<< 4 | (0xF0 & x)>> 4 //8765 4321
John Dvorak
  • 26,799
  • 13
  • 69
  • 83