I am just beginning 6502 assembly, and I am trying to figure out the basics. I'm using a JavaScript emulator on my phone to assemble the code. I'm trying to get it to increment p (my memory location) and store A in both addresses (as a prelude to a loop later). But, when run, it's only storing A to $0205 and not $0206 (which is what I am trying to get it to do). Any help appreciated.
LDA #$01
define p $0205
STA p
INC p
STA p
The code you have written doesn't do what you think it does. p
is just a defined name for the number $205
. In effect, your program is
LDA #$01
; define p $0205
STA $0205
INC $0205
STA $0205
The INC
instruction increments the contents of location $0205
but then it is immediately overwritten by the second STA
.
There are several ways to do what you want. Firstly, if it is always the location p
and the next one, depending on your assembler, you should be able to write
LDA #$01
define p $0205
STA p
STA p+1
That will put 1 in $0205
and $0206
. An alternative, if you want to do the incrementing at run time is to use an index register, of which there are two X
and Y
.
LDA #$01
define p $0205
LDY #0
STA p,Y
INY
STA p,Y
That's not really better than the previous, but it can be used in a loop.
define p $0205
define count 10
LDA #$01
LDY #count
loop:
DEY ; Decrement Y by 1 setting the zero flag if zero
STA p,Y ; Store the result in p + Y (flags are unaffected)
BNE loop ; Go round the loop if the zero flag is not set
The above will fill the locations from p to p + 9 with the constant 1. Note that it does it by going downwards through memory.
If p
is unknown until runtime, you can do the same thing but using a zero page indirect address.
define p $0205
define count 10
define ptr $50 ; A random zero page location
LDA #$01
LDY #count
; First load the address in the zero page location
LDA <p ; < is a common notation for the low byte of a 16 bit number ($05 in this case)
STA ptr
LDA >p ; and > is for the high byte ($02 in this case)
STA ptr+1
; Now a similar loop to before
loop:
DEY ; Decrement Y by 1 setting the zero flag if zero
STA (ptr),Y; Store the result in p + Y (flags are unaffected)
BNE loop ; Go round the loop if the zero flag is not set