Thread: Reversing bits
View Single Post
Old Apr 16th, 2012, 9:35 PM   #1
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 7 Troncoso is on a distinguished road
Reversing bits

I have an assignment to write a program in LC-3 assembly. I have to write several sub routines, one of them being to reverse the order of bits in a word and store the result in a register.
An example:

10010101 => 10101001

A mirror, if you will. I can think of a couple ways to do this. My issue comes when I need to store the bits back into a register. My plan is to use a nested loop:

word = 16 bit word

for (int i = 16; 16 > 0; 16--) {
	for (int j = 0; j < i; j++) {
		ADD word to itself to shift bits left by 1
	}
	AND word with x8000 to isolate high order bit
}

That's my pseudocode to get the job done. The issue comes after this
AND word with x8000 to isolate high order bit

At that point, after each inner loop completes, I have the correct bit. I just don't know how to properly add that to the register.

Sorry, if this sounds complicated. I'll really appreciate any help.
Troncoso is offline   Reply With Quote