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

标签 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

最佳答案

Is the following pattern correct? whats wrong?

没有。

  • 加法始终通过累加器完成。
  • 没有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

您可以对 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

关于assembly - 如何使用 1 寻址模式通过在汇编中加载来添加两个存储值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47208154/

相关文章:

assembly - Commodore基本4套路

assembly - 如何在反汇编的C/C++程序中重建类型和数据结构?

c++ - 这个 GCC 优化不正确吗?

assembly - 从 6502 汇编器获取随机数

c++ - 在多个类上定义的面向对象的跳转表

6502 - 如何运行反汇编代码6502?

c - 在Assembly中打印多个值

assembly - 如何告诉 GNU 汇编器在使用未定义的标签时发出警告?

gcc - ARM 程序集 : . LANCHOR0

c - 如何设置 TomHarte CPU 测试