assemblybit-manipulation68000easy68k

How can I turn an odd number even, or vice versa, in Motorola 68000 Assembly?


Basically if I had a number in D1, and wanted it to ALWAYS be even, how would I make sure it is never odd?

I know it has something to do with the AND instruction. But when I tried that, it would always subtract 1. So it would turn odd numbers even and even numbers odd.

How can I basically do if n is odd, sub 1


Solution

  • and your number with -2.

    In 2's complement representation -2 is a number with all bits set to one except the lowest one (11111...110), so, used as a mask, it always kills just the low bit of your number. This forces it to be even (it works correctly even for negative numbers).


    As for the "viceversa" in the title: to do the opposite (=force every even number to the next odd), just or with 1. This sets the low bit to be 1, which obtains the required effect.