3
votes

I'm looking at some code sample from this site:

http://www.6502asm.com/

And looking at it i see they have some instructions that instead of using the memory location directly they use a label, for example, in the alive.asm:

lda ypos,x

And ypos is

ypos:
dcb $00,$02,$20,$02,$40,$02,$60,$02
dcb $80,$02,$a0,$02,$c0,$02,$e0,$02
dcb $00,$03,$20,$03,$40,$03,$60,$03
dcb $80,$03,$a0,$03,$c0,$03,$e0,$03
dcb $00,$04,$20,$04,$40,$04,$60,$04
dcb $80,$04,$a0,$04,$c0,$04,$e0,$04
dcb $00,$05,$20,$05,$40,$05,$60,$05
dcb $80,$05,$a0,$05,$c0,$05,$e0,$05

I know that labels differs depending on the assembler, but i am assuming it is going through that list, but how it specificity works

1
The machine instruction loads a byte into A, from the address ypos+X. It's a register + immediate displacement addressing mode. It doesn't modify X, you'd have to do that separately.Peter Cordes
Does my answer suit you ? If yes, please accept it, otherwise please indicate what is missing.Laurent H.

1 Answers

6
votes

Here are details about the instruction lda ypos,x:

  1. If ypos is located out of zero page (equal to or more than 0x0100):

    • Opcode is 0xBD: it uses an indexed absolute addressing mode using the index register X, also called absolute,X mode
    • it computes an address by adding the content of X register to the 2-bytes address represented by the label ypos, then it loads the A register (accumulator) with the byte located at the computed address
    • its size is 3 bytes; it takes 4 CPU cycles + 1 cycle if page boundary is crossed, i.e. if the high byte of ypos is different from the high byte of the compute address ypos + X.
    • it updates only the status flags N and Z (Negative and Zero)

  2. If ypos is located inside zero page (between 0x00 and 0xFF), then it may depend on your assembler (check the opcode): either it uses the indexed absolute addressing mode and it works as previously described, or:
    • Opcode is 0xB5: it uses an indexed zero-page addressing mode using the index register X, also called zeropage,X mode
    • it computes an address by adding the content of X register to the 1-byte address represented by the label ypos, wrapping within the range 0x00-0xFF, then it loads the A register (accumulator) with the byte located at the computed address.
      Note: Be very careful, because it is probably not the expected behavior if the values defined at ypos go beyond the zero page. There might be a specific syntax in your assembler to force the absolute,X addressing mode.
    • its size is 2 bytes; it takes 4 CPU cycles
    • it updates only the status flags N and Z (Negative and Zero)