Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 16th, 2012, 8:35 PM   #1
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 6 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
Old Apr 16th, 2012, 9:12 PM   #2
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 6 Troncoso is on a distinguished road
Re: Reversing bits

I really do apologize for the double post, but I can't find the "edit" button anywhere.

Either way, I forgot to mention, the LC-3 ISA only includes AND, ADD, and NOT operations.
Troncoso is offline   Reply With Quote
Old Apr 17th, 2012, 1:09 AM   #3
The Dark
Programming Guru

 
Join Date: Jun 2005
Posts: 1,631
Rep Power: 14 The Dark will become famous soon enoughThe Dark will become famous soon enough
Re: Reversing bits

You could have another variable (or register) - call it bitVal - that starts at 1 and doubles (using add) each time around your i loop. bitVal would be the value of the current bit you want to add. Then if your isolated bit is non-zero, add the bitVal to your result register.
something like:
word = 16 bit word
bitVal = 1
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
        if (word != 0)
           ADD bitVal to register
        ADD bitVal to itself to shift bits left by 1
}
The Dark is offline   Reply With Quote
Old Apr 17th, 2012, 2:25 AM   #4
Troncoso
Hobbyist Programmer
 
Join Date: Oct 2011
Posts: 127
Rep Power: 6 Troncoso is on a distinguished road
Re: Reversing bits

Dude! Thanks a lot. You gave me the idea that got my program working. It's basically what you did, except I put your

 ADD bitVal to itself to shift bits left by 1

In the inner loop

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

Still, I wouldn't have thought of that without you. Thanks again!
Troncoso is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Data caching for vector of bits vs bytes? iProgrammer C++ 7 Mar 15th, 2010 2:06 PM
Problem with permuting bits kolosick.m188 C 1 Nov 20th, 2009 7:41 AM
String of bits to byte[] hbe02 Java 1 Mar 5th, 2008 10:07 AM
MySQL Datatypes: 2 Bits? Sane PHP 1 Jul 27th, 2006 2:08 AM
Reversing the order of the numbers... ReggaetonKing Java 7 Feb 28th, 2006 5:13 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC