Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do the modulo operation in ARM assembly?

Tags:

arm

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;

how to do this above operation in ARM assembly.

like image 398
Shrey Avatar asked Oct 27 '25 10:10

Shrey


1 Answers

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.

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
like image 64
mstorsjo Avatar answered Oct 30 '25 15:10

mstorsjo