当被乘数在累加器中时,如何与6502中的加法和移位算法相乘?

问题描述

所以我试图通过使用加法和移位来乘法。我想将被乘数存储在累加器中,将被乘数存储在 X 寄存器中。我真的不知道为什么我的代码不起作用,但我怀疑是因为被乘数在累加器中并将乘积放在单独的寄存器中。

这是我目前的代码

      LDA #49
      LDX #8
      LDY #$8
      STA $700
      STX $704
loop:           ; if 1
      BCC  loop2    ; if 0,go to loop2
      CLC       ; clear carry for adc
      ADC $700      ; adc
loop2: ;     if 0
      ROL $700      ; left shift
      CLC
      ROR $704
      DEY
      BNE loop      ; if not 0,go to loop
      STA $700      ; store A in product register

感谢您的帮助

解决方法

这是修正后的版本。检查双分号的变化。最大的错误是在第一次循环之前忘记重置累加器和进位标志。

      LDA #49
      LDX #8
      LDY #9    ;; you need to increase your loop by 1
      STA $700
      STX $704
      LDA #$00  ;; you need to reset acc
      CLC       ;; and clear carry
loop:           ; if 1
      BCC  loop2    ; if 0,go to loop2
      CLC       ; clear carry for adc
      ADC $700      ; adc
loop2: ;     if 0

      ;ROL $700     ;; these three lines
      ;CLC          ;; are replaced with
      ;ROR $704     ;; the two lines below

      ROR        ;; this is
      ROR $704   ;; faster

      DEY
      BNE loop      ; if not 0,go to loop
      STA $700      ; store A in product register