组装:增加内存位置时出现问题

时间:2018-12-25 22:08:16

标签: assembly 6502

我才刚刚开始6502的组装,我想弄清楚基础知识。我在手机上使用JavaScript模拟器来汇编代码。我试图让它增加p(我的内存位置)并将A存储在两个地址中(以作为稍后循环的前奏)。但是,在运行时,它仅存储A到$ 0205,而不是$ 0206(这就是我要使其执行的操作)。任何帮助表示赞赏。

LDA #$01
define p $0205
STA p
INC p
STA p

2 个答案:

答案 0 :(得分:2)

您编写的代码无法实现您认为的功能。 p只是数字$205的定义名称。实际上,您的程序是

LDA #$01
; define p $0205
STA $0205
INC $0205
STA $0205

INC指令递增位置$0205的内容,但是第二个STA立即将其覆盖。

有几种方法可以做您想要的。首先,如果它始终是位置p,下一个位置,则取决于您的汇编程序,您应该能够编写

LDA #$01
define p $0205
STA p
STA p+1

这将在$0205$0206中放入1。如果要在运行时进行递增操作,另一种方法是使用索引寄存器,其中有两个XY

LDA #$01
define p $0205
LDY #0
STA p,Y
INY 
STA p,Y

确实没有比以前更好,但是可以循环使用。

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

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

如果p在运行时之前是未知的,则可以执行相同的操作,但使用零页间接地址。

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

答案 1 :(得分:0)

我认为您将地址(0205)放入了一个索引寄存器(我认为6502有2个)。将A存储到索引寄存器指向的地址中,递增索引寄存器,然后将A存储到索引寄存器指向的地址中。