Indirect Indexed Addressing in NESASM3

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Indirect Indexed Addressing in NESASM3
by on (#198940)
Hello all, noob here.

I've been working on a program (which hopefully will be a game someday) in NESASM3 and I'm running into a problem. I'm trying to load new data into the nametable while scrolling vertically (with vertical mirroring, which may be part of the problem, but the idea is to be able to load a new nametable during the scroll so something different will show up).

I'm able to do this loading from a known address. But I am trying to index into the map stored in the program ROM indirectly and having no luck. Here's what it looks like:

Code:
  .rsset $0000
index_low  .rs 1
index_high .rs 1

...

  .org $8000
story:
  .incbin "story.txt"

...

  clc
  lda #LOW(story)
  adc index_low
  sta index_high
  lda #HIGH(story)
  adc index_high
  sta index_high

...

  ldx #0
draw_next_row:
  lda (index_low), x
  sta $2007
  inx
  cpx #32
  bne draw_next_row

...


The screen scrolls but comes up with garbage, as if it's not indexing into the data properly. Like I said, I can change "lda (index_low), x" to load from something like "lda story + $60, x" and it will work (except that it just loads the same row of data over and over and doesn't go to the next row). Is this an issue with the syntax? What am I missing?

Appreciate the help!
Re: Indirect Indexed Addressing in NESASM3
by on (#198941)
NESASM3 uses different syntax, square brackets instead of parentheses:

Code:
LDA [zp], Y
Re: Indirect Indexed Addressing in NESASM3
by on (#198942)
Thanks for the prompt reply! I guess I should have mentioned that I did some searching for an answer and came across the square bracket thing. But if I do

Code:
  lda [index_low], x


the assembler throws an error. "Incorrect addressing mode!"

So I'm still not sure how to go about this. Is there something I'm missing?
Re: Indirect Indexed Addressing in NESASM3
by on (#198943)
Nate5700 wrote:
Code:
  lda [index_low], x

the assembler throws an error. "Incorrect addressing mode!"

You can only index with Y like this: lda [index_low], y. There's an alternative mode that uses X, but it does not do the same thing: lda [index_low, x].
Re: Indirect Indexed Addressing in NESASM3
by on (#198944)
Holy crap, it totally worked. Screen scrolls and the story message comes up!

Thanks a ton! I wouldn't have even thought of trying to use Y instead. Is that a 6502 thing or just a NESASM3 peculiarity?
Re: Indirect Indexed Addressing in NESASM3
by on (#198945)
Nate5700 wrote:
Holy crap, it totally worked. Screen scrolls and the story message comes up!

Thanks a ton! I wouldn't have even thought of trying to use Y instead. Is that a 6502 thing or just a NESASM3 peculiarity?

It's a 6502 limitation.
Re: Indirect Indexed Addressing in NESASM3
by on (#198951)
Nate5700 wrote:
Is that a 6502 thing or just a NESASM3 peculiarity?

It may seem unintuitive for us humans who're used to programming using text, but only some combinations of registers and addressing modes are programmed into the chip, so even though some instructions look correct as far as syntax goes, the CPU doesn't really support them. This is my favorite reference document for 6502 instructions, and it includes 2 very useful tables for each instruction: one shows how the various flags are affected by the instruction, and the other shows which addressing modes are supported for the instruction.
Re: Indirect Indexed Addressing in NESASM3
by on (#199399)
If you wanted something to print out you could print the appendices of this pdf:
msc6500_family_programming_manuel. If you flip to Appendix B and find lda p.219 it lists everything from tokumaru's suggested site, I think. This manuel's appendix B seems to be more correct and will print out better than the one I use. :)