如何使用MUL装配的所有其他指令来实现? [英] How to implement MUL using all the other instructions in assembly?

查看:184
本文介绍了如何使用MUL装配的所有其他指令来实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我已经实现了所有的添加,并在组装机器SHF,跳跃,BR,LDW,六味地黄丸(负载字加载字节...)........除MUL(多重)指令。现在我想编写汇编code来操作MUL指令这需要存储在0x4000的(a)和0x4001的(B)(各8位),并计算出他们的产品,然后将结果储存在0x4002(16位数据)。我们希望尽量减少指令步骤。

Say I have implemented all the ADD, AND, SHF, JUMP, BR, LDW, LDB(load word load byte...) ........except MUL (multiple) instructions in an assembly machine. Now I want to write assembly code to operate a MUL instruction which takes in data stored in 0x4000(a) and 0x4001(b) (each of 8 bits ) and calculate their product, and then store the result in 0x4002 (16bits). And we want to minimize the instruction steps.

我能想到的:


  1. A + A + A + A + ...... A(B倍),最简单的,但显然是最差的一个

  1. a+a+a+a+.....a(b times) the easiest but obviously the worst one

Leftshift(一,7)+ leftshift(A,6)+ ... + A(移位,并根据对B加)

Leftshift( a, 7) + leftshift( a, 6)+.....+a (shift and add according the b)

我想知道是否有更好的解决这个问题。
谢谢!

I want to know if there is better solution to this question. Thanks!

推荐答案

总的想法是,你(应该有)在学校里学到的,当你做长乘,除了我们在二进制,而不是十进制做到这一点是相同的。考虑下面的两个例子:

The general idea is the same as you (should have) learned in school when you did "long multiplication", except we do it in binary instead of decimal. Consider the two examples below:

      1010        1234
    x 1100      x 2121
----------   ---------
      0000        1234
     0000        2468
    1010        1234
 + 1010      + 2468
 ---------   ---------
   1111000     2617314  

右边的例子是碱-10(十进制)和左边的例子是二进制的。注意,只有数字,你必须是乘上系数为0或1乘以零是很容易,答案永远是零,你甚至不必担心并称英寸乘以一个是也容易,它只是知道怎么远远超过了来接班的问题。但是,这很容易,因为它远远超过了,你不得不看,检查一下。

The example on the right is base-10 (decimal) and the example on the left is binary. Observe that the only digits you must multiply the top factor by is either 0 or 1. Multiplying by zero is easy, the answer is always zero, you don't even have to worry about adding that in. Multiplying by one is also easy, it just a matter of knowing "how far over to shift it". But that is easy, it as far over as you had to look to check that bit.

开始与你的号码的16位工作拷贝,和一个16位累加器设置为零。在按住Shift键上面的数字,并随时有一个在最右边的数字一个一个你的工作副本添加到累加器。无论是否有一个或零,则需要在工作副本向左移动一位。当顶得为零你知道你做答案是在累加器。

Start with a 16-bit working copy of your number, and a 16-bit accumulator set to zero. Shift the top number over and any time there is a one in the right-most digit you add the "working copy" to the accumulator. Whether or not there is a one or zero, you need to shift the "working copy" to the left one bit. When the "top" gets to zero you know you are done and the answer is in the accumulator.

有一些优化你可以使用,这样你就不需要那么多的16位寄存器(或8位寄存器对),但我会离开你的工作的细节。

There are some optimizations you can use so that you don't need as many 16-bit registers (or 8-bit register pairs), but I'll leave you to work out the details.

这篇关于如何使用MUL装配的所有其他指令来实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆