A few quick programming questions

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
A few quick programming questions
by on (#235239)
Hi, I'm trying to plan out some stuff for my game but I'm away from my computer this weekend so I'm doing pseudo-code on paper for now. I've got a couple of quick questions about certain opcodes that I don't 100% understand after reading the opcode reference docs.

TXS & TSX:

Do these function the same PHA & PLA, the opcode reference makes it seem like they don't but I'm not sure. I think PHA decrements the stack pointer & copies the value of A into that memory location, vice versa for pull A. Do TXS & TSX work like that or do they just copy data to/from the current stack pointer location without changing the stack pointer?

Indexed Indirect vs Indirect Indexed:

I'm not sure I understand how Indexed Indirect works. Indirect indexed I think I understand works as follows:

Let's say myPointer is at $0000,$0001 and storing the address $6000, and Y is $01.
LDA (myPointer), y
So that is then looking at memory location $6001 and loading it into A.

Let's say myPointer is at $0000,$0001, and X is $02.
LDA (myPointer, x)
Is that looking at $0002,$0003. Then treating the values stored there as a pointer, looking at wherever they point to and loading it into A? That's what the reference reads like to me, but I'm not sure if that's what's actually going on.
Re: A few quick programming questions
by on (#235241)
While PHA and PLA deal with the contents of the stack, TXS and TSX deal with the stack pointer, which is a register the indicates where the top of the stack is. As a beginner, you'll hardly ever manipulate the stack pointer besides initializing it to $FF at the beginning of the program. Stack manipulation is necessary in advanced programming techniques you shouldn't be concerned about for now.

Your understanding of Indirect indexed is correct. Indexed Indirect addressing is hardly used by anyone. Most 6502 programmers even question the reason for its existence at one point or another. You got it right, though. It lets you use a table of pointers in ZP, but the only real world example of that being useful that most NES programmers can think of is pointing to the individual tracks of a song in a music engine.
Re: A few quick programming questions
by on (#235243)
Thank you very much for the clarifications.
Re: A few quick programming questions
by on (#235351)
tokumaru wrote:
ect. Indexed Indirect addressing is hardly used by anyone. Most 6502 programmers even question the reason for its existence at one point or another. You got it right, though. It lets you use a table of pointers in ZP, but the only real world example of that being useful that most NES programmers can think of is pointing to the individual tracks of a song in a music engine.

(zp,X) is used constantly in the Forth programming language, for accessing memory whose address is on the data stack, where X is the stack pointer. The data stack, which is ZP-resident to get not only the efficiency but also the extra addressing modes, is in a sense an array. It is not a table. Although a (X) addressing mode would be interesting (as would (A) and (Y)), some of the occurrences of (DP,X) having the base address are other than 0, some being 2 and some being 4, for accessing the 2nd or 3rd cell on the stack. Top of stack is accessed by (0,X). So far, I've had no reason to go further, like (6,X) etc. like I have with DP,X (which is not indirect). The page-1 hardware stack is still used for return addresses, and, on occasion, temporary data storage too. Forth's habit of keeping the data stack separate from the return stack solves certain problems and streamlines operations.