[英]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.