Even or odd, count the set bits, multiply by shifting
Four questions about one number answered with a bit test, a shift, a mask and a loop that reads the carry flag.
Four questions about one number, none of them answered with arithmetic. Is 182 odd, what is it times
eight, what are its bottom four bits, and how many of its 32 bits are ones. The answers land in d1
to d4.
The instructions of the Example before this one treat a register as a number. These four treat the same register as 32 bits side by side, which is the other way to read one and often the cheaper way.
You need to know: the "Arithmetic, logic and bits" lecture. What is new here is the carry flag as
a way out of a register, lsr drops the bit that falls off the bottom into C and a branch reads
it.
btst #0, d0 is C's n & 1 without building the mask, and it sets Z from the bit it found: Z
goes to 1 when the bit was 0, which is backwards from what you expect the first time. sne d1
reads it the other way round again and leaves $FF when the bit was a 1, so d1 is 00000000 here
because 182 is even.
Shifting left by three multiplies by eight, since every place a bit moves left doubles what it is
worth. d2 comes out at 000005B0, which is 1456. A shift by a constant takes a count from 1 to 8
and no more; past that you put the count in a register, lsl.l d1, d2.
andi.l #$0F, d3 keeps the four bits the mask has set and clears everything else, so d3 is 6, the
6 of $B6. That is how any field is taken out of a packed value: mask what you want, then shift it
down to the bottom if it was not there already.
The loop runs 32 times, once per bit, and does C's count += n & 1; n >>= 1; with the & 1 done by
the shift itself. lsr.l #1, d5 moves every bit one place down and the bit that falls off the bottom
lands in C, so bcc skips the addq when it was a zero. d4 comes out at 5, which is the number
of ones in 10110110.
Try changing move.l #182, d0 to move.l #183, d0, one more. d1 becomes 000000FF because the
number is now odd, d3 becomes 7, and d4 becomes 6.