簡體   English   中英

如何使用1種尋址模式通過裝載加載兩個存儲值?

[英]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上的添加僅在累加器中完成。

  • 將第一個數字加載到累加器中
  • 將第二個數字添加到累加器
  • 將累加器存儲在內存中

Z = X + Y

lda x
add y
sta z  ;Leaves x and y unchanged

X = X + Y

lda x
add y
sta x

X = Y + X

你的程序做了什么產生了與我的第二個片段相同的結果。 你添加的順序並不重要(對於減法來說並不重要!)請注意,用y加載累加器只是為了在y之后直接重新加載它是沒用的!

lda x   ;This is useless
lda y
add x
sta x

正如其他人所評論的那樣 ,6502指令集中沒有add指令,也沒有sub指令。 但是我願意給你懷疑的好處,因為為了避免每次你開始一個新的加法或減法時必須寫clcsec完全可以定義一些宏。

使用現代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

其中xy可以是例如絕對地址。

注意進位標志! 如果您無法知道進位狀態,則首先需要一個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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM