Reputation: 13
I've recently begun programming in 6502 assembly and I've run into a problem. I'd like to be able to store a 16-bit memory address beginning from address $0300, then I'd like to store a value at that address.
For example storing the address $2016 would be $0300: #$20, $0301: #$16. Then I'd like to read those two bytes and store a value at $2016. I can't seem to find an addressing mode that allows this, is there anything like this or do I have to use zero paging.
Upvotes: 0
Views: 2182
Reputation: 86651
You need to find two zero page locations and index through those. Indirect addressing is only available through zero page. And, in fact, there isn't a zero page indirect mode that doesn't use an index aswell.
Assuming $02
and $03
are available. Copy the bytes to there.
; Store the address you want to access in zp memory
lda $300
sta $03 ; Note I'm swapping the bytes (see below)
lda $301
sta $02
; access the address indirectly through zero page
ldx #0
lda $data
sta ($02,x) ; x is zero so address used is $02
The reason I swapped the bytes when copying them to zero page is that you have stored the address (in your question) at $300
in big endian order i.e. the high byte at the low address. The 6502 is little endian which means it expects the low byte at the low address. You should really follow 6502 conventions and store your bytes so that $300
contains $16
and $301
contains $20
.
Upvotes: 6
Reputation: 19375
$0300: #$20, $0301: #$16. Then I'd like to read those two bytes and store a value at $2016.
Another, less advisable way exists if the code is writable: One could put the address into an absolute store instruction's operand bytes.
LDA $301
STA stins+1
LDA $300
STA stins+2
stins:
STX $FFFF ; value in X; FFFF is placeholder for address
Upvotes: 4