Bit-reverse a byte on 68HC12

前端 未结 9 1608
挽巷
挽巷 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题