LC3装配中的移位乘法

时间:2015-12-08 11:59:11

标签: assembly binary multiplication lc3

我可以通过进行位移和加法来将二进制数乘以两个数字:

int multiplicand = 5; //0101
int multiplier = 3; //0011
int result = 0; //0000
for n bits in multiplier
    if n is one
        result += multiplicand
    multiplicand bit shifted left

对于上述步骤,结果按以下顺序更改:

0000 + 0101 -> 0101
0101 + 01010 -> 1111
Skip 1111 + 010100
Result is 1111 (15)

我很难设计LC3装配算法来执行上述伪码。

    .orig x3500     ; starting position
    and r0, r0, #0  ; result
    ld r1, m1       ; load multiplicand
    ld r2, m2       ; load multiplier
l1  brz l2          ; if bit is zero
    brp l3          ; if bit is one
    brn l4          ; if no more bits (???)
l2  add r1, r1, r1  ; shift multiplicand bits left
    brnzp l1        ; redo
l3  add r0, r0, r1  ; add multiplicand to result
    add r1, r1, r1  ; shift multiplicand bits left
    brnzp l1        ; redo
l4  trap x25        ; end
m1  .fill #5        ; multiplicand
m2  .fill #3        ; multiplier
    .end

这是我最好的尝试。我不确定该怎么做,说实话,LC3指令集非常有限。我对如何在LC3中使用迭代器而不是像这样使用按位移位有很好的把握。

1 个答案:

答案 0 :(得分:2)

在具有右移指令的架构上,通过使用带有and的{​​{1}}屏蔽它来继续测试最低有效位,并在循环中将每个位移到此位置。然而,LC3没有向右移动,也没有简单的方法,所以将掩模位向左移动而不是向右移动更方便。要检测循环结束,可以使用固定计数(16位)或使用另一个掩码查看其余位中是否存在任何设置位。可能的解决方案可能如下:

1
相关问题