[英]How to add two stored values by load in assembly using 1 addressing mode?
我想添加两个存储的值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
6502上的添加仅在累加器中完成。
lda x
add y
sta z ;Leaves x and y unchanged
lda x
add y
sta x
你的程序做了什么产生了与我的第二个片段相同的结果。 你添加的顺序并不重要(对于减法来说并不重要!)请注意,用y加载累加器只是为了在y之后直接重新加载它是没用的!
lda x ;This is useless
lda y
add x
sta x
正如其他人所评论的那样 ,6502指令集中没有add
指令,也没有sub
指令。 但是我愿意给你怀疑的好处,因为为了避免每次你开始一个新的加法或减法时必须写clc
和sec
完全可以定义一些宏。
使用现代FASM语法add
示例:
macro add op
{
clc
adc op
}
sub
使用非常旧的METACOMCO语法的示例:
sub MACRO
sec
sbc \1
ENDM
如果这应该是6502代码,则添加的操作码是adc
,这意味着“ add with carry ”。 有没有可以不携带加法运算。
lda
用一个值加载累加器 ,所以直接序列中的两个lda
完全没用。 只需使用一个lda
后跟一个adc
(它会将你向地址添加的内容添加到累加器并添加进位标志)。
总而言之,序列看起来就像:
lda x
adc y
sta x
其中x
和y
可以是例如绝对地址。
但要注意进位标志! 如果您无法知道进位状态,则首先需要一个clc
( 清除进位 )指令。 如果您知道将设置进位,并且y
是立即值,则只需添加一个。 如果你不能这样做,你也需要一个clc
。
一般来说,总是尽量避免使用clc
(它需要一个字节和一些周期,这对于这样一个有限的芯片确实很重要),但有时你会需要它。
可以避免clc
( 双重实现的一部分)的代码的真实示例:
nts_addloop: lda nc_string,x
cmp #$5 ; value >= 5 ?
bcc nts_noadd ; after not branching here, we know C=1
adc #$2 ; then add 3 (2 + carry)
sta nc_string,x
nts_noadd: dex
需要 clc
的代码的实际示例( 长乘法实现的一部分):
mul_rorloop: ror mpm_arg1,x
dex
bpl mul_rorloop
bcc mul_noadd ; we know carry is set when not branching
ldx #($80-NUMSIZE)
clc ; clear it because we have to add a variable value
mul_addloop: lda mpm_arg2+NUMSIZE-$80,x
adc mpm_res+NUMSIZE-$80,x
sta mpm_res+NUMSIZE-$80,x
inx
bpl mul_addloop
mul_noadd: ldx #($81-NUMSIZE)
以下模式是否正确? 怎么了?
没有。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.