Date : Fri, 20 Nov 2009 22:26:47 +0100
From : anders.carlsson@... (Anders Carlsson)
Subject: ASL/LSR (was: Quine)
Michael Firth wrote:
> LSR = Logical Shift Right, equivalent to a divide by 2
> ASL = Arithmetic Shift Left, equivalent to a multiply by 2
While we are on the topic, another construct you sometimes want to do is
count how many bits in a byte are enabled. This is ideally done with either
of these instructions:
LDX #0
LDA data
.loop
LSR
BCC check
INX
.check
BNE loop
RTS
Perhaps this is an obvious use, but the example points out that LSR always
changes the carry flag even if the bit shifted out is a zero. It also points
out LSR affects the Z flag as well, so when the accumulator equals zero, the
BNE automatically fails without the need for an extra CMP #00. Actually a
good study of which flags each instruction will change is beneficial if you
try to write effective machine code. On a computer with plenty of memory to
use you have less reason to consider every byte but I imagine even then it
sometimes can get tight, e.g. if you try to patch an existing ROM.
Best regards
--
Anders Carlsson