I'm in a microprocessors class and we are using assembly language in Freescale CodeWarrior to program a 68HCS12 micro controller. Our assignment this week is to revers a byte, so if the byte was 00000001, the output would be 10000000, or 00101011 to 11010100. We have to use assembly language, and were told we could use rotates and shifts (but not limited to!) to accomplish this task. I'm really at a loss as to where I should start.
相关问题
- Index of single bit in long integer (in C) [duplic
- Null-terminated string, opening file for reading
- What's the difference between 0 and dword 0?
- Translate the following machine language code (0x2
- Where can the code be more efficient for checking
相关文章
- How to generate assembly code with gcc that can be
- Select unique/deduplication in SSE/AVX
- Optimising this C (AVR) code
- Why does the latency of the sqrtsd instruction cha
- Difference in ABI between x86_64 Linux functions a
- x86 instruction encoding tables
- Rounded division by power of 2
- Why doesn't there exists a subi opcode for MIP
For example, if you have in al the byte number the easiest way is
we put 8 in ecx for loop
In bl we will havee the result, we will make ebx, only to see what happens better
In carry flag now you have the last bit from left
now you add in bl what you have in carry
and that's all
Hints: If you do a shift, one bit gets shifted out and a zero (probably) gets shifted in. Where does that shifted out bit go to? You need to shift that in to the other end of the destination register or memory address.
I'm sure that 25 years ago I could do this in Z80 machine code without an assembler :)
FIrst of all work out the algorithm for doing what you need to do. Express it as pseudo code or C or plain English or diagrams or whatever you are comfortable with. Once you have cleared this conceptual hurdle the actual implementation should be quite simple.
Your CPU probably has instructions which let you shift and/or rotate a register, perhaps including the carry flag as an additional bit. These instructions will be very useful.
This was a comment but I thought WTH!
To save space over the 256 byte table you can have a 16 byte table containing the values for four bits (nibbles) at a time. The algorithm then would be
If I were a prof I'd certainly like the two parts where one shift is in the indexing and the other outside.
I had also to program this bit reverse for the university (for 8 bits). Here is how I did:
I didn't commented it so here is how it works: DH is a
1
which travels in the byte like first time:00000001
; second time00000010
and so on. When you make anAND
with the AL you get0
or something like100
or10000
you have to shift that to the right to get it as0
or1
. Then, put it in BH and shift to the desired position which is7
for byte0
,6
for byte1
and so on. ThenOR
to our final result andINC
andDEC
what is necessary. Do not forget the conditional jumps and to popAX
for the next loop :)Result will be in CH.
When you do a right shift, what was the least significant bit goes into the carry flag.
When you do a rotate, the carry flag is used to fill in the vacated bit of the result (LSB for a ROL, MSB for a ROR).