Take this loop for example:
Code: Select all
clearmem:
ldx #5
lda #$00
sta $200,x
dex
bne clearmem
What's going to happen? You're going to load 5 into X. Then you're going to store 0 into $205 ($200 + X). Then X decreases by 1 to equal 4. Then this happens: bne clearmem. If X is not equal to 0, it will jump to the beginning of the loop. Since X is 4, it will jump to the beginning of the loop. What's at the beginning of the loop? "ldx #5". You are constantly telling X to be 5 every time the code goes through the loop. Since they only way to get out of the loop is if X is 0, the code will never stop looping. It will keep storing 0 into $205 forever and ever.
But if you did this:
Code: Select all
ldx #5
clearmem:
lda #$00
sta $200,x
dex
bne clearmem
X would keep decreasing every time the code goes through the loop, and it would successfully clear $201-$205. There's another problem. Go through the loop in your mind with X being 1. It will clear $201, and then decrease X to equal 0. When that happens, it breaks out of the loop. It misses $200, which I'm assuming you want to have cleared. So you would have to make it this:
Code: Select all
ldx #6
clearmem:
lda #$00
sta $1FF,x
dex
bne clearmem
Because by the time X equals zero, it won't clear $1FF. This will Clear $200-$205 successfully. Just go through the loop in your mind a couple of times.
Also, you don't NEED to put the lda #$00 out of the loop, it's just wise because it takes the CPU less time to do the loop. So this:
Code: Select all
ldx #6
lda #$00
clearmem:
sta $1FF,x
dex
bne clearmem
Is how you'd want to do it.