Struggling to understand 'Zero-Page Indirect Address Indexed by Y' for the 6502 Assembly Language

162 Views Asked by At

This is specifically for the NES console which uses a slightly modified 8-bit 6502 microprocessor. Below follows the code with my comments explaining what each line does as far as my basic understanding goes (please let me know if I misunderstood any lines):

lda #$00   ; Load the accumulator (A register) with the immediate value 0x00
sta $02    ; Store the value in the accumulator into memory address $02

lda #$03   ; Load the accumulator with the immediate value 0x03
sta $03    ; Store the value in the accumulator into memory address $03

lda #$14   ; Load the accumulator with the immediate value 0x14
sta $0302  ; Store the value in the accumulator into memory address $0302

clc        ; Clear the carry flag (CLC instruction)
lda #$10   ; Load the accumulator with the immediate value 0x10
ldy #$02   ; Load the Y register with the immediate value 0x02

adc ($02),y

It seems like, before the final line adc ($02), the status of memory addresses and registers is as follows:

  • $02 = 0x00
  • $03 = 0x03
  • $0302 = 0x14
  • A = 0x10
  • Y = 0x02

Therefore, the final line to me seems to indicate the following:

  1. $02 + Y
  2. 0x00 + 0x02
  3. 0x02 with the value stored at this address being 0x00
  4. Now we add 0x00 to A register which is simply 0x10 but apparently the results should be is follows, thus I am a bit confused where I made a mistake:

A=$24 X=$00 Y=$02

Some additional notes, supposedly the following is true:

The value in memory locations $02 and $03 was previously set to specify the address $0300.

When does this happen? I specifically struggle to understand this line. Are they both specified to address $0300?

1

There are 1 best solutions below

0
On BEST ANSWER

adc ($02),y is an example of indirect indexed addressing. The parenthesis around $02 means that $02 does not contain the operand but $02 and $03 together contain a 16 bit address that, when added to y contains the operand.

This is the sequence of what happens with adc ($02),y

  1. The CPU loads the number at the address $02 which is $00.
  2. The CPU loads the address at $03 which is $03.
  3. The CPU forms a 16 bit number from the two loads it has done. The 6502 is little endian, so the number at the low address becomes the low byte. We end up with $0300. This is sometimes called the base address
  4. The CPU adds y to the base address to get $0302. This new number is used as the address in which to find the operand.
  5. The CPU loads the byte at $0302 which is $14 and adds it to the accumulator.