BTST 无法按预期工作以确定数字是否为奇数

BTST not working as expected to determine if number is odd

我很困惑为什么这段代码没有按预期工作。据我了解,BTST.L #1,D1 检查 D1 中存储的值的最后一位是否为 1,在这种情况下,D1 中的值将是奇数,程序将跳转到 ADDSUM。

它似乎对某些值(9*2、8*2 或 10*10)工作正常,但当我尝试其他值时失败(11*11 给我 110,与 10*11 相同)。我相信只要乘数是奇数。

START:
              MOVE.W #MULTIPLICAND,D0  ; Set D0 equal to the multiplicand
              MOVE.W #MULTIPLIER,D1    ; Set D1 equal to the multiplier
              MOVE.L #00000000,D2      ; Clear D2 as it will be used to store the product

LOOP          BTST.L #1,D1     ; Check if D1 is odd
              BEQ ADDSUM      ; If so branch to ADDSUM

RETURN        CMP #,D1      ; Check if the multiplier is equal to 1
              BEQ END         ; If so, terminate the loop

              ASL #1,D0       ; Multiply the multiplicand by 2
              ASR #1,D1       ; Divide the multiplier by two

              BRA LOOP        ; Branch back to the top of the loop

ADDSUM        ADD.W D0,D2     ; Add D0 to D2
              BRA RETURN      ; After adding, we have to branch back to where we were in the loop

END           SIMHALT

我明白了。我想我会 post 它作为一个答案,以防万一其他人偶然发现这个。

原来我没看懂BTST.L #1,D1指令是怎么工作的。我认为原始示例中的代码应该检查 D1 中的最后一位是否等于 1。实际上,BTST 通过将 Z 条件代码标志设置为 1 来工作tested 为 0。指令中的 #1 指定它正在测试右数第二位。

为了确定even/odd,最右边的位是必须测试的。我通过将其更改为具有以下内容来修复我的代码:

LOOP          BTST.L #0,D1    ; Check if D1 is even (Z-flag will be set to 1 if bit is zero)
              BNE ADDSUM      ; BNE will branch to ADDSUM if Z-flag is not set (meaning D1 is odd)

希望这能够帮助遇到同样问题的其他人。