barrym95838 wrote:
That looks quite nice, but you should replace the LDX #$01 with LDX #$02 to avoid smashing your stack, and you should be able to save one byte and a few cycles by replacing the LDX $02 with INX.
It never even occurred to me that X and $02 would have the exact same values.
It seems screamingly obvious in hindsight, but I'm just so used to only thinking about the values in the Zero Page and kinda got stuck thinking of the Registers as just middle men to be used and forgotten once the bytes are in the right locations.
BigEd wrote:
One unexpected thing for me on this code snippet, as it has developed: my first thought was that the use of X to load and compare a value was not idiomatic - it rather looked like the writer was using all the registers just because they are distinct 'variables' and that's how you'd write something in a high level language. But as it turned out on second reading is that X is used well: Y must be used to index because that's the only addressing mode which fits the bill, and therefore A must be used for the constant value to be written, and therefore X is the right register to use as a working register for the comparison.
Using the right register at the right time is one of the marks of the well-crafted 6502 code... another being good use of the persistence of the flag values.
Honestly, I keep catching myself with the exact
opposite problem. I have an annoying habit of thinking of the Zero Page and the Flags so much, that tend to forget that there are still values in the Register that I can still technically use until something changes them.
I often think of registers as "Robot Arms" that grab packets of data and place them in the right memory "Bucket". I often forget that the "Arm" will continue to grip what you asked it to grab until you ask it to grab something else.
I work in a factory as my day job, so it helps me to think of code in assembly line terms. lol
EDIT:
hoglet wrote:
You can shave off one more byte, and a few cycles:
Code:
LDA #$01
LDX #$02 ;; corrected
LDY #$00
STX $02
STY $01
LDX #$04 ;; number of pages to copy
main:
STA ($01),y
INY
BNE main
INC $02
DEX
BNE main
I make that 22 bytes.
Dave
Oh, that's a cool idea. It lets me eliminate the Compare command completely! As well as the need to load the high byte into X (like how Barry suggested swapping LDX $02 with simply INX.)
Now I can cut out the LDX
and the CPX!