简体   繁体   English

如何使用移位和加法在 MIPS 程序集中执行乘法?

[英]How can I perform multiplication in MIPS assembly using shift and add?

Please help, I've spent over 5 hours trying to figure out this simple problem and I cannot for the life of me figure out how the hell you do it.请帮忙,我已经花了 5 多个小时试图找出这个简单的问题,但我终其一生都无法弄清楚你到底是怎么做到的。

I am supposed to do some problem like 7 * 5 and return the result in MIPS assembly, but I HAVE to do it using shift and add.我应该做一些像 7 * 5 这样的问题并在 MIPS 程序集中返回结果,但我必须使用 shift 和 add 来做。 I cannot figure out how to use the hint we were given and turn it into MIPS code.我不知道如何使用我们得到的提示并将其转换为 MIPS 代码。 Like I understand 100% how the picture below works and the logic behind it, but I CANNOT turn it into MIPS assembly code, thats what I've spent over 5 hours trying to do.就像我 100% 了解下图的工作原理及其背后的逻辑一样,但我无法将其转换为 MIPS 汇编代码,这就是我花了 5 多个小时尝试做的事情。

here is what we were given as a hint这是我们得到的提示

I understand how shifting works, I understand how adding works in MIPS, I just don't know how I can do what is shown in that image, in MIPS assembly language.我了解移位是如何工作的,我了解在 MIPS 中添加是如何工作的,我只是不知道如何使用 MIPS 汇编语言执行该图像中显示的内容。 (I don't know how to turn the english into code that does that stuff. MIPS is too barebones for this!) (我不知道如何将英语变成可以做这些事情的代码。MIPS 对这个来说太准了!)

I SHOULD CLARIFY!: Both numbers are VARIABLES.我应该澄清!:两个数字都是变量。 I don't know how to write the code that takes two differing numbers each time and gets the result, using shift and add.我不知道如何编写每次使用两个不同数字并获得结果的代码,使用 shift 和 add。 7*5, 6*9, 17*3, etc. Whatever the user enters for those two numbers as long as they are non-negative, the code must get the result. 7*5, 6*9, 17*3 等等。只要这两个数字是非负数,无论用户输入什么,代码都必须得到结果。

I grade school we learned that a * (b + c) = ab + ac right?我小学时我们知道 a * (b + c) = ab + ac 对吗? So 7 * 5 = 7 * (4 + 1) = 7*4 + 7*1 which are both powers of 2...(2^2 and 2^0).所以 7 * 5 = 7 * (4 + 1) = 7*4 + 7*1 它们都是 2 的幂...(2^2 和 2^0)。

If not a constant does it matter?如果不是一个常数有关系吗?

     abcd
*    efgh
==========
     abcd  * h
    abcd   * g
   abcd    * f
+ abcd     * e
=============

same stuff we learned in grade school except much simpler as efgh are either zero or one.我们在小学学到的东西相同,只是更简单,因为 efgh 要么是零要么是一。 So if the h bit (bit 0) then we add abcd<<0, if the g bit is set (bit 1) then add abcd<<1 if f is set (bit 2) then add abcd<<2 and so on.因此,如果 h 位(位 0)那么我们添加 abcd<<0,如果设置了 g 位(位 1)然后添加 abcd<<1 如果设置了 f(位 2)然后添加 abcd<<2 等等.

So actually you do/did know how to do it just not how to apply it.所以实际上你知道/确实知道如何去做,而不是如何应用它。 Pencil and paper just like grade school, no reason to spend 5 hours...铅笔和纸就像小学一样,没有理由花5个小时......

EDIT编辑

unsigned int a,b,x,y;
for(y=0,x=0;x<32;x++) if(a&(1<<x)) y+=b<<x;


for(y=0,x=1,z=b;x;x<<=1,z<<=1) if(x&a) y+=z;

For the effort shown, I will put here major spoiler, but you will probably regret it, once you will realise how you were running in circles around solution.对于所展示的努力,我将在此处剧透,但一旦您意识到自己是如何围绕解决方案兜圈子的,您可能会后悔。

s0 = a, s1 = b, s2 = 0 (will be result)
multiply_loop:
  if (s1 == 0) goto output_result
  t1 = s1&1
  if (t1 == 0) goto skip_add
  s2 += s0
skip_add:
  s0 <<= 1;  multiply "a" by 2
  s1 >>= 1;  shift bits of "b" to right by 1 for s1&1 test
  goto multiply_loop

output_result:
  s2 is result of multiplication

Of course this will overflow for big "a"/"b" values, etc.. Try it decipher on paper first, with binary values, how it works internally.当然,对于大的“a”/“b”值等,这会溢出。首先尝试在纸上破译,使用二进制值,它在内部是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM