M68000 Write an assembly-language program to reverse the bits in a byte. Your pr
ID: 3791445 • Letter: M
Question
M68000
Write an assembly-language program to reverse the bits in a byte. Your program should start by putting a known bit pattern into the low-order byte of D0, then reverse it, and then halt (so the reversed byte should end up in the low-order byte of D0). Hand in a trace of your program as in Assignment 2, and make sure that you dump the register values into the output of your trace before you save the trace into a text file for handing in. Your program must work with any bit pattern, but for the run you hand in your test byte should be 001001112.
Your trace should not be too long, but if the algorithm you use generates a very long trace, just trace the first three or four dozen instructions and print that out. In any case, you should also hand in a listing of your program.
Explanation / Answer
Loop through all the bits of an integer if a bit at i'th position is set in the I/p no. Then set the bit at ( NO_OF_BITS_1)-I in o/p. Where ON_ OF_ BITS is number of bits reverse in the given number.
For ( I=0; I< NO_OF_BITS; I++)
{
Temp= (number&(1<<I));
If (temp)
Reverse_num|=(1<<((No_OF_BITS-1)-I));
}
Return reverse_num;
}
/* Driver function to test above function */
Intiki main( )
{
Unsigned integer X=2;
Print ("%u", reverse Bits (x));
Get char( );
}
Above program can be optimized by removing the use of variable temp see below the modified code
Unsigned int reverse Bits (unsigned int num)
{
Unsigned int No_OF_BITS = size of (num)*8;
Unsigned int reverse_num=0;
Int I;
For (I=0; I<No_OF_BITS; I++)
{
If ( num & (1<<I)))
Reverse_num|=1<<((NO_OF_BITS-1)-I);
}
Return reverse_num;
}
Time complexity :O((log n)
Space complexity :O(1)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.