Search code examples
assemblyconstants6502nes

How to use a predefined constant as a memory address?


I have a NES game in the making. I am defining several constants to help me with managing sprites. I have

spriteyposition = $0200
spritetile = $0201
spriteattribute = $0202
spritexposition = $0203
sprite1 = $00
sprite2 = $04
sprite3 = $08
sprite4 = $0c
sprite5 = $10
sprite6 = $14
sprite7 = $18
sprite8 = $1c

My use case is as follows:

I want to modify the y position of sprite 1

I do:

ldx sprite1
lda spriteyposition, x
adc #$8
sta spriteyposition, x

In my use case spriteyposition should be a memory pointer, but i have a feeling that the assembler is treating it as a regular number

How do I refer to spriteyposition as a memory address and not a number?


Solution

  • In my use case spriteyposition should be a memory pointer, but i have a feeling that the assembler is treating it as a regular number

    Your feeling is incorrect. This code assembles to the intended opcode BD (LDA ABS,x) -- there is no IMM,x addressing mode.

    What is incorrect is

    ldx sprite1
    

    sprite1 is defined as $00, so this ends up loading X with the value of the address $0000. What you want is ldx #sprite1.