Brian Schlenker
Brian Schlenker

Reputation: 5426

Confused about wrapping in 6502 indirect x and y

I'm not sure exactly how wrapping works with the indexed x and y address modes. The documentation that I've found is clear that the indexing part is wrapped around within the zero page, but what about the actual dereferenced 16bit address, is that constrained to the zero page as well?

Let's say I have this code:

LDX #$00
LDA #$05
STA $ff
LDA #$07
STA $0100
LDA #$08
STA $00
LDY #$0a
STY $0705
LDY #$0b
STY $0805
LDA ($ff,X)

Will A be loaded with $0a, or $0b?

It is obvious that if X were loaded with $01, then the address would be looked up from $00 ($ff + $01 == $00). But in this case, we're going to read the 16bit address lo byte from $ff - is the hi byte read from $0100, or from $00?

Similarly for indirect y, let's say I have this code:

LDY #$00
LDA #$30
STA $ff
LDA #$04
STA $00
LDA #$05
STA $0100
LDX #$0a
STX $0430
LDX #$0b
STX $0530
LDA ($ff),Y

We read a 16bit address from $ff, is the hi byte read from $0100 or from $00?

And lastly, also with indirect y:

LDY #$01
LDA #$ff
STA $01
STA $02
LDX #$0a
STX $00
LDA ($01),Y

We dereference $01 and get $ffff, then add $01, does this mean we load from $00 and get $0a in A?

Upvotes: 2

Views: 1051

Answers (1)

Michael
Michael

Reputation: 58427

The address within the () is an 8-bit address. The actual effective address is a 16-bit address.

So if you have LDA ($FF,X) with X=0, then the 16-bit effective address will be loaded from $FF (LSB) and $00 (MSB), and A is loaded from that address.

If you have LDA ($FF),Y then a 16-bit address will be loaded from $FF and $00, then Y is added to that 16-bit address, and A is loaded from the resulting address. The addition of Y will not wrap around at the 8-bit boundary (but it will wrap at the 16-bit boundary, i.e. $00FF + 1 = $0100 but $FFFF + 1 = $0000).

Upvotes: 7

Related Questions