1

In C, if I have

int x = 55;
int y = x % 10; // How can I do this in ARM? 

How can I perform these two lines in ARM? I don't want to use any MOD operator (in case there does exist such instruction in arm). I'm still very new to assembly

Thanks in advance

Timal Peramune
  • 309
  • 1
  • 7
  • 19
  • You can repeatedly subtract 10 until the result is smaller than 10. There is a magic multiplication based approach, too. – fuz Mar 06 '19 at 03:23
  • Could you elaborate on both methods? – Timal Peramune Mar 06 '19 at 03:25
  • This answer shows how most compilers optimize [divide by constant](https://stackoverflow.com/questions/41183935/why-does-gcc-use-multiplication-by-a-strange-number-in-implementing-integer-divi/41224096#41224096) by using multiply, then using the upper bits of the product as the quotient. The dividend - divisor*quotient = remainder. – rcgldr Mar 06 '19 at 03:33
  • 1
    Ask a C compiler to compile `unsigned mod10(unsigned x) { return x % 10; }` for ARM, e.g. with `-O3 -mcpu=cortex-a15`, and you'll see how it uses a fixed-point multiplicative inverse for division, and then `x - (x/10)*10` for the remainder. – Peter Cordes Mar 06 '19 at 04:08
  • If you're asking about remainder with a non-constant divisor, that's a harder question if you want to do it efficiently (not with a repeated-subtraction loop). – Peter Cordes Mar 06 '19 at 04:11

0 Answers0