News: 11 March 2016 - Forum Rules

Author Topic: A couple of 6502 ASM questions pertaining to opcodes  (Read 1890 times)

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
A couple of 6502 ASM questions pertaining to opcodes
« on: September 21, 2015, 06:48:56 pm »
When you Transfer Accumulator to X (TAX), is the value that you transferred to X still in the accumulator?  And if not, what is in it?

Also, when you Push the Accumulator to Stack (PHA), same question.  When you put the value on the stack, what is then in the Accumulator?

I'm not sure if when you Transfer or Push or Pull, if it's simply copying the value to that destination, or transferring it, making the accumulator a previous value or something else.

And when you Push or Pull to/from the Stack, does it start at the end of the stack ($1FF), or at the beginning?  Because I noticed that when I use the earlier stack addresses ($100 - $10F for instance), that in some of the hacks I did, those earlier stack addresses are never used.

Thank you for your time.

Disch

  • Hero Member
  • *****
  • Posts: 2814
  • NES Junkie
    • View Profile
Re: A couple of 6502 ASM questions pertaining to opcodes
« Reply #1 on: September 21, 2015, 07:53:58 pm »
When you Transfer Accumulator to X (TAX), is the value that you transferred to X still in the accumulator?

Yes

Quote
Also, when you Push the Accumulator to Stack (PHA), same question.  When you put the value on the stack, what is then in the Accumulator?

A does not change when you push it.  Nor does status change when you PHP it.

Quote
I'm not sure if when you Transfer or Push or Pull, if it's simply copying the value to that destination, or transferring it, making the accumulator a previous value or something else.

It's always a copy.

Quote
And when you Push or Pull to/from the Stack, does it start at the end of the stack ($1FF), or at the beginning?  Because I noticed that when I use the earlier stack addresses ($100 - $10F for instance), that in some of the hacks I did, those earlier stack addresses are never used.

The stack grows down.  Typically this means that $01FF is the bottom of the stack, and each value preceding it is one space higher.  You can think of the stack as being "upside-down".  It's a bit confusing, but it makes sense because it makes indexing easier:

Code: [Select]
TSX  ; put stack pointer in X
LDA $0101,X  ; get value at top of stack
LDA $0102,X  ; get value in 2nd position

As you can see in this example, the lower X/S are, the more has been pushed to the stack.

Typically games will reset SP to $FF in their startup code, making the stack start at $01FF

RetroRain

  • Sr. Member
  • ****
  • Posts: 287
    • View Profile
Re: A couple of 6502 ASM questions pertaining to opcodes
« Reply #2 on: September 21, 2015, 09:00:59 pm »
Thanks Disch!  As usual, your expertise is welcomed and appreciated. :)

Dr. Floppy

  • Restricted Access
  • Hero Member
  • *
  • Posts: 970
  • Make America GREAT Again!
    • View Profile
    • BaddestHacks.net
Re: A couple of 6502 ASM questions pertaining to opcodes
« Reply #3 on: September 22, 2015, 02:38:09 am »
As Mr. Disch stated, "Transfer" is more accurately thought of as "Copy". (Fortunately, "Shift" and "Rotate" are exactly what they impart.)

One more thing to watch out for- the TXS/TSX commands are not X-reg's special versions of PHA/PLA. TXS defines the low-byte of the Stack Pointer (the high byte is always #01) as whatever value is in X-reg at that time (X-reg is unaffected). TSX places the Stack Pointer's current low-byte into X-reg (both the Stack Pointer and the Stack itself are unaffected).

This is why A2-FF-9A is so prevalent in NES Initialization routines (usually right after 78-D8). It defines the Stack Pointer as $01FF, allowing it to grow downward ($1FE, $1FD, $1FC, etc.) from there.

STARWIN

  • Sr. Member
  • ****
  • Posts: 454
    • View Profile
Re: A couple of 6502 ASM questions pertaining to opcodes
« Reply #4 on: September 22, 2015, 11:07:40 am »
blurp never mind .. you can also think of SP as a one byte pointer, given that it always refers to page 01 in the address space.
« Last Edit: September 22, 2015, 11:22:34 am by STARWIN »