What is the practical use of indexed indirect addressing?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
koitsu
Posts: 4201
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: What is the practical use of indexed indirect addressing

Post by koitsu »

tepples wrote:This particular bubble sort routine could have been done just as easily with lda (tab),y, seeing as Y doesn't need to be saved (the next instruction overwrites it anyway), and X=$00 isn't used farther down.
I'm glad that addresses what the OP asked. :|

RIP me doing the other examples, especially if this is going to be a point of contention. If you want to see the other examples, buy the book. :D

Signed,
The Guy Who Tries To Stay On Topic and Does What Other People Politely Ask For
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: What is the practical use of indexed indirect addressing

Post by FrankenGraphics »

Well i learned a couple of things, so thanks for the insight!

primarily a method to sorting in 6502 (never touched that before - yes i haven't done my homework :oops: ). Sorting is quite expensive in a game context, but i'd think sorting very short lists of say 4 options could be used in ocassionally called pathfinding and action priority sequencing routines for AI for example. That could perhaps allow for less rigid, more context aware priority chains.

Let's say we want to sort 1 such sequence each for a number of actors using a loaddr pointer. Wouldn't that be the perfect occasion to use lda (sequenceTable, x)?
(that also means we can't set x to 0 just yet like in the example).
Garth
Posts: 246
Joined: Wed Nov 30, 2016 4:45 pm
Location: Southern California
Contact:

Re: What is the practical use of indexed indirect addressing

Post by Garth »

truffly wrote:But why would you ever need to use indexed indirect addressing with the X register? What practical purpose does this serve? There has to be some situation where this feature would prove useful, otherwise I'd assume it wouldn't be featured in the processor design. Have you ever used indexed indirect addressing in your programs? Do you think it is particularly useful, or do you think indirect indexed addressing is a better tool?
(ZP,X) is used all the time in the Forth programming language which uses a data stack in ZP that's separate from the return stack in page 1. Keeping them separate like this solves certain problems in passing stacked data to and from nested subroutines. X is used as the stack pointer. Then suppose you want to read the content of the memory pointed to by the top stack two-byte cell. Regardless of the current depth of the ZP data stack, do LDA (0,X). (We make X point to the top of the stack, not one position past it like the S register does for the hardware stack.) If you want to read the content of memory pointed to by the second-on-stack two-byte cell, it's LDA(2,X), again regardless of the current data-stack depth. You may find benefits in doing things this way in assembly language too. This, and tons more, is discussed in detail in my treatise on 6502 stacks (plural, not just the page-1 hardware stack), at http://wilsonminesco.com/stacks/ .
http://WilsonMinesCo.com/ lots of 6502 resources
Post Reply