程序集:增加内存位置的问题

Assembly: problems incrementing memory location

我刚开始组装 6502,我正在尝试了解基础知识。我在 phone 上使用 JavaScript 模拟器来 assemble 代码。我试图让它增加 p (我的内存位置)并将 A 存储在两个地址中(作为稍后循环的前奏)。但是,当 运行 时,它只将 A 存储到 $0205 而不是 $0206(这是我试图让它做的)。任何帮助表示赞赏。

LDA #
define p 05
STA p
INC p
STA p

我认为您将地址 (0205) 放入索引寄存器中(我认为 6502 有两个)。将 A 存储到变址寄存器指向的地址中,递增变址寄存器,并将 A 存储到变址寄存器指向的地址中。

您编写的代码与您认为的不一样。 p 只是数字 5 的定义名称。实际上,您的程序是

LDA #
; define p 05
STA 05
INC 05
STA 05

INC指令递增位置05的内容,但随后立即被第二个STA覆盖。

有多种方法可以满足您的需求。首先,如果它总是位置 p 和下一个,这取决于你的汇编器,你应该能够写

LDA #
define p 05
STA p
STA p+1

这会将 1 放入 0506。如果要在 运行 时进行递增,另一种方法是使用变址寄存器,其中有两个 XY

LDA #
define p 05
LDY #0
STA p,Y
INY 
STA p,Y

这并没有比之前的更好,但它可以循环使用。

define p 05
define count 10

    LDA #
    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

以上将用常量 1 填充从 p 到 p + 9 的位置。请注意,它是通过向下遍历内存来完成的。

如果 p 在 运行 之前未知,您可以做同样的事情,但使用零页间接地址。

define p 05
define count 10
define ptr  ; A random zero page location

    LDA #
    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 ( in this case)
    STA ptr
    LDA >p     ; and > is for the high byte ( 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