I'm starting out with 6502 Assembly right now and have a problem wrapping my head around loops that need to deal with numbers bigger than 8 bit.
Specifically, I want to loop through some memory locations. In pseudo-c-code, I want to do this:
// Address is a pointer to memory
int* address = 0x44AD;
for(x = 0; x < 21; x++){
// Move pointer forward 40 bytes
address += 0x28;
// Set memory location to 0x01
&address = 0x01;
}
So starting at address $44AD
I want to write $01
into ram, then jump forward $28
, write $01
into that, then jump forward $28
again until I've done that 20 times (last address to write is $47A5
).
My current approach is loop unrolling which is tedious to write (even though I guess an Assembler can make that simpler):
ldy #$01
// Start from $44AD for the first row,
// then increase by $28 (40 dec) for the next 20
sty $44AD
sty $44D5
sty $44FD
[...snipped..]
sty $477D
sty $47A5
I know about absolute addressing (using the Accumulator instead of the Y register - sta $44AD, x
), but that only gives me a number between 0 and 255. What I really think I want is something like this:
lda #$01
ldx #$14 // 20 Dec
loop: sta $44AD, x * $28
dex
bne loop
Basically, start at the highest address, then loop down. Problem is that $14 * $28 = $320 or 800 dec, which is more than I can actually store in the 8-Bit X register.
Is there an elegant way to do this?