All you're really doing is a hard coded 16 bit addition
or subtraction of 1
Code: Select all
clc
lda #1
adc (ptr1),y
sta (ptr1),y
bne *+9
ldy #1
sec
adc (ptr1),y
sta (ptr1),y
You don't explicitly set y to begin, but presumably y is 0
so you can iny instead of ldy #1 and save a byte
If you start with 1 in the accumulator, the only way to end up
with 0 in the accumulator after an adc is to add FF and that will
also result in the carry being set, so the sec is redundant.
Your code becomes:
Code: Select all
clc
lda #1
adc (ptr1),y
sta (ptr1),y
bne *+7
iny
adc (ptr1),y
sta (ptr1),y
personally, I'd code it like this:
Code: Select all
clc
lda (ptr1),y
adc #1
sta (ptr1),y
bcc *+7
iny
adc (ptr1),y
sta (ptr1),y
Just because it's a little easier (for me, any way)
to see what's going on.
Dec can be roughly the same.
0 is the only thing you can start with in the accumulator
and subtract 1 from and end up with a barrow, ie
with the carry clear, and that will leave FF in the
accumulator. Adding FF to something is the same
as subtracting 1 (as far as the resulting byte and
ignoring flags)
Code: Select all
sec
lda (ptr1),y
sbc #1
sta (ptr1),y
bcs *+7
iny
adc (ptr1),y
sta (ptr1),y
The last three instructions are the same so they
can probably be shared if that seems desriable,
but I'll leave that as an 'exercise for the reader'
