如何使用 1 寻址模式通过加载在汇编中添加两个存储值?

Muh*_*lam 2 assembly 6502

我想添加两个存储的值 x 和 y 但我只想使用 1 寻址模式。
以下是示例:

lda x
sta x
// store the final result in x


lda y
sta y
// store the final result in y

//now I want to add x and y like x+y. Is the following pattern correct? whats wrong? 
lda x
lda y
add x
sta x
Run Code Online (Sandbox Code Playgroud)

Fif*_*nik 5

6502 上的加法仅在累加器中完成。

  • 将第一个数字加载到累加器中
  • 将第二个数字添加到累加器
  • 将累加器存储在内存中

Z=X+Y

lda x
add y
sta z  ;Leaves x and y unchanged
Run Code Online (Sandbox Code Playgroud)

X=X+Y

lda x
add y
sta x
Run Code Online (Sandbox Code Playgroud)

X=Y+X

您的程序所做的结果与我的第二个片段相同。添加的顺序并不重要(对于减法来说更不重要!)请注意,仅使用 x 加载累加器然后直接使用 y 重新加载它是没有用的!

lda x   ;This is useless
lda y
add x
sta x
Run Code Online (Sandbox Code Playgroud)

正如其他人评论的那样,6502指令集中没有add指令,也没有sub指令。不过,我愿意给您带来怀疑,因为定义一些宏是完全可以的,以避免每次开始新的加法或减法时都需要clc编写sec

add使用现代 FASM 语法的示例:

macro add op
 {
  clc
  adc op
 }
Run Code Online (Sandbox Code Playgroud)

使用非常古老的 METACOMCO 语法的示例sub

sub MACRO
    sec
    sbc \1
    ENDM
Run Code Online (Sandbox Code Playgroud)


Jer*_*myP 5

以下模式是否正确?怎么了?

不。

  • 加法总是用累加器完成。
  • 没有ADD指令只ADC加进位
  • 您需要管理进位标志。

如果您有两个内存位置 x 和 y 并且您想将结果存储在 x 中,那么执行此操作的规范方法是:

; x = x + y
LDA x      ; Load one operand into the accumulator.
CLC        ; Clear the carry flag so it does not get added into the result
ADC y      ; Add the other operand
STA x      ; Store the operand back to x
Run Code Online (Sandbox Code Playgroud)

除了立即数之外,您可以对 y 使用任何地址模式,对 x 使用任何地址模式。

如果 x 和 y 是 16 位值的地址,则 16 位相加将如下所示:

LDA x      ; Load one operand into the accumulator.
CLC        ; Clear the carry flag so it does not get added into the result
ADC y      ; Add the other operand
STA x      ; Store the operand back to x
LDA x+1    ; Now do the high byte
ADC y+1    ; Note we do not clear the carry this time.
STA x+1
Run Code Online (Sandbox Code Playgroud)