大会MOD算法的处理器没有除法运算符 [英] Assembly mod algorithm on processor with no division operator

查看:160
本文介绍了大会MOD算法的处理器没有除法运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现一个简单的宏,发现两个数的模上没有一个除法运算符(认为ARM)处理器。我可以用除法反复减法,但我不知道这是否是最有效最简单的,或一起工作。

I need to implement a simple macro that finds the modulo of two numbers on a processor that doesn't have a division operator (think ARM). I could use division by repeated subtraction, but I don't know if this was the most efficient or easiest to work with.

有什么建议? code甚至会更有帮助。这种特殊的类有我们使用SPARC的一个子集,所以大部分操作是这样的:添加R1,R2,rdest

Any suggestions? Code would be even more helpful. This particular class has us using a subset of SPARC, so most operations look like this: add r1, r2, rdest.

这个特殊的任务要求检查modb == 0 或除法的余数是零。所以,对一个高效的执行任何提示或建议将是最受欢迎的。

This particular assignment calls for checking that a mod b == 0 or that the remainder of the division is zero. So any hints or suggestions toward an efficient implementation would be most welcome.

推荐答案

不知道确切的行动就会受到限制,但我认为你会做长除法,这样的事情,在伪code

No idea what exact operations you are limited to, but I'd think you'd do long division, something like this, in pseudo-code:

dividend = abs(dividend)
divisor = abs(divisor)
if divisor == 0,
    barf
remainder = dividend
next_multiple = divisor

do
    multiple = next_multiple
    next_multiple = left_shift(multiple, 1)
while next_multiple <= remainder && next_multiple > multiple

while multiple >= divisor,
    if multiple <= remainder,
        remainder = remainder - multiple
    multiple = right_shift(multiple, 1)

要真正计算出商数(或至少是它的绝对值),最后一部分会是这样的:

To actually calculate the quotient (or at least its absolute value), the last part would be something like:

quotient = 0
while multiple >= divisor,
    quotient = left_shift(quotient, 1);
    if multiple <= remainder,
        remainder = remainder - multiple
        quotient = quotient + 1
    multiple = right_shift(multiple, 1)

这一切都不是测试,这可能是充满了错误。

None of this is tested, and it is probably riddled with errors.

这篇关于大会MOD算法的处理器没有除法运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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