我试图用beq,add,nand创建一个乘法程序。这段代码可以工作,但我不明白这段代码是如何退出循环的,因为测试值总是将它添加为self。谢谢你的建议。(我使用goto和label,因为稍后我会将这些代码放入mips程序集中)。
unsigned mult(unsigned x, unsigned y)
{
unsigned test = 1, ans = 0;
next:
if ((test & x) == 0) goto skip;
ans += y;
skip:
y += y;
test += test;
if (test != 0) goto next;
return ans;
}
发布于 2016-12-09 03:54:26
test
将滚动,然后值为0。
test
将接受1, 2, 4, 8, 16, 32, 64 ,.... 2147483648 , 0
值
代码的逻辑很有趣,
它检查LSbit of x
是否为1,如果是,则将y
添加到答案中。
如果下一个LSbit of x
,它将在答案中添加2*y
。
对于每一点x
,即1,它将添加2^n*y
,给出最终答案。
https://stackoverflow.com/questions/41059760
复制