Assembly LC3汇编中的移位乘法
我可以通过移位和加法将两个二进制数相乘:Assembly LC3汇编中的移位乘法,assembly,binary,multiplication,lc3,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 01
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
屏蔽最低有效位,并在循环中将每一位移到该位置,从而不断测试最低有效位。然而,LC3没有右移功能,也没有简单的方法,所以将掩码位向左移动比将乘法器向右移动更方便。要检测循环结束,可以使用固定计数(16位)或使用另一个掩码查看剩余位中是否有任何设置位。可能的解决方案如下所示:
.orig x3500 ; starting position
and r0, r0, #0 ; result
ld r1, m1 ; multiplicand
ld r2, m2 ; multiplier
add r3, r0, #1 ; mask for testing bit
add r4, r0, #-1 ; mask for end condition (all 1 bits)
l1 and r2, r2, r4 ; any bits remaining?
brz l4 ; no, done
and r5, r2, r3 ; test bit
brz l2 ; if bit is zero, skip addition
add r0, r0, r1 ; add multiplicand to result
l2 add r1, r1, r1 ; shift multiplicand bits left
add r3, r3, r3 ; shift test mask left
add r4, r4, r4 ; shift end mask left
brnzp l1 ; redo
l4 trap x25 ; end
m1 .fill #5
m2 .fill #3
.end
您甚至没有加载5
和3
。另外,为了测试位,您需要使用和与1
@Jester将其屏蔽掉。我已经编辑了代码,我不确定您所说的屏蔽是什么意思。我在网上找到的一些不完整的例子使用了位掩码(0001),但我不确定该怎么做。我发布的代码不断地添加(5,10,15,20,…),当它应该在15停止时。