我在 6502 中遇到了另一个加法问题....
我正在尝试将两个 n 字节整数相加以产生 n 字节结果。我不完全确定我是否对 6502 芯片有足够的了解,因此对我当前代码的任何反馈都会非常有帮助。
我知道我应该使用 INX(递增 x 寄存器)和 DEY(递减 y 寄存器),但我不确定操作码的位置。
说明: 使用绝对索引寻址将两个 n 字节整数相加
Adding two n-byte integers using absolute indexed addressing
The addends start at memory locations $xxxx, $yyyy, answer is at $zzzz
Byte length of the integers is at $AAAA (¢—>256)
START = $0500
CLC
____
loop LDA $0400, x
ADC $0410, x
STA $0412, x
____
BNE loop
BRK
LDA、ADC、STA在循环外(第一次在汇编中使用循环)
编辑:
Variables
A1 = $0600
B1 = $0700
B2 = $0800
Z1 = $0900
[START] = $0500
CLC 18
LDX AE
LDY A1 AC
loop: LDA B1, x BD
ADC B2, x 7D
STA Z1, x 9D
INX E8
DEY 88
BNE loop D0
;Adding two n-byte integers using absolute indexed addressing
;The addends start at memory locations $xxxx, $yyyy, answer is at $zzzz
;Byte length of the integers is at $AAAA (¢—>256)
CLC
LDX #0 ; start at the beginning
LDY $AAAA ; load length into Y
loop: LDA $xxxx, X ; load first operand
ADC $yyyy, x ; add second operand
STA $zzzz, x ; store result
INX ; go on to next byte
DEY ; count how many are left
BNE loop ; if more, do more
;------------------------------------------------------;
; Subroutine for multibyte addition ;
;------------------------------------------------------;
; All words in Little Endian format
; First word for addition in $A0
; Second word for addition in $B0
; Result word in $C0
; word length in $D0
; Note! push 'A' register to stack before calling subroutine
; Provided below is an example
.org $0000
LDA #$41 ; First word: 779AAA819541
STA $A0
LDA #$95
STA $A1
LDA #$81
STA $A2
LDA #$AA
STA $A3
LDA #$9A
STA $A4
LDA #$77
STA $A5
LDA #$F4 ; Second word: 5ED594552DF4
STA $B0
LDA #$2D
STA $B1
LDA #$55
STA $B2
LDA #$94
STA $B3
LDA #$D5
STA $B4
LDA #$5E
STA $B5
LDA #$06 ; Result: D6703ED6C335
STA $D0
JSR AddBytes
BRK
AddBytes:
LDX #00
CLC
PHP
Addloop:
PLP
LDA $A0,X
ADC $B0,X
STA $C0,X
PHP
INX
CPX $D0
BNE Addloop
RTS