Bit-reverse a byte on 68HC12

前端 未结 9 1596
挽巷
挽巷 2021-01-18 02:03

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 by

相关标签:
9条回答
  • 2021-01-18 02:36

    I had also to program this bit reverse for the university (for 8 bits). Here is how I did:

    MOV AL, 10001011B ;set the value to test
    MOV CL, 7
    MOV DH, 1
    MOV DL, 0
    
    loop1: PUSH AX
    AND AL, DH 
    PUSH CX
    MOV CL, DL
    SHR AL, CL
    POP CX
    MOV BH, AL
    SHL BH,CL
    OR CH,BH
    DEC CL
    INC DL
    SHL DH, 1
    POP AX
    CMP DL, 8
    JE END
    JMP LOOP1
    
    END:
    

    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 time 00000010and so on. When you make an AND with the AL you get 0 or something like 100or 10000 you have to shift that to the right to get it as 0 or 1. Then, put it in BH and shift to the desired position which is 7for byte 0, 6for byte 1and so on. Then OR to our final result and INC and DEC what is necessary. Do not forget the conditional jumps and to pop AX for the next loop :)

    Result will be in CH.

    0 讨论(0)
  • 2021-01-18 02:37

    The following code make use of rotations and shift. I use Intel x86 syntax, see explanations on the right side:

        mov cx, 8           ; we will reverse the 8 bits contained in one byte
    loop:                   ; while loop
        ror di              ; rotate `di` (containing value of the first argument of callee function) to the Right in a non-destructive manner
        adc ax, ax          ; shift `ax` left and add the carry, the carry is equal to 1 if one bit was rotated from 0b1 to MSB from previous operation
        dec cx              ; Decrement cx
        jnz short loop      ; Jump if cx register Not equal to Zero else end loop and return ax
    

    I use dec instruction instead of sub, because it takes one byte only while sub takes 3 bytes. On top of that compilers seem to always optimize by choosing dec over sub.

    edit: Also note that rcl ax (3 bytes), while equivalent of adc ax, 0 (2 bytes) followed by shl ax (2 bytes) is less efficient. See comments below, many thanks to Peter Cordes for his insights.

    0 讨论(0)
  • 2021-01-18 02:51

    Consider two registers as stacks of bits. What happens if you move one bit at a time from one to another?

    0 讨论(0)
  • 2021-01-18 02:57

    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 :)

    0 讨论(0)
  • 2021-01-18 02:57

    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.

    0 讨论(0)
  • 2021-01-18 02:58

    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).

    0 讨论(0)
提交回复
热议问题