如何在ARM汇编中进行模运算? [英] How to do the modulo operation in ARM assembly?
问题描述
我正在尝试将两个寄存器中的值相加,然后对它们取模 8.所以,在 C 代码中,它会是这样的
I'm trying to add the values in two registers, and modulo them by 8. So, in C code, it would be like this
a = a + b;
c = a % 8;
如何在 ARM 汇编中执行上述操作.
how to do this above operation in ARM assembly.
推荐答案
并非所有 ARM 处理器都有直接的除法或取模指令,因此在大多数情况下,对取模运算的调用最终会作为对例如___modsi3
.
Not all ARM processors have a direct instruction for division or modulo, so in most cases, a call to the modulo operation would end up as a function call to e.g. ___modsi3
.
在这种特殊情况下,当对 8 取模时,如果可以假设这些值是非负的,则可以将 % 8
部分作为 &7
.在这种情况下,您的案例的程序集将是:
In this particular case, when doing modulo for 8, if the values can be assumed to be nonnegative, you can do the % 8
part as & 7
. In that case, the assembly for your case would be:
add rA, rA, rB
and rC, rA, #7
这篇关于如何在ARM汇编中进行模运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!