Search code examples
armmultiplication

Multiplying a constant with the value of a register in ARM without the MUL instruction


I need to multiply the value of a register(r1) by 16385 and store the result in the same register without using the MUL instruction in a single line of code.

I know that using LSL instruction will multiply a value by a power of 2 but since I am trying to multiply by an odd number I don't see how that would work. I saw another similar question where someone was asking how to multiply a*17 and the answer that was provided was ADD r1, r1, r1 LSL #4. I don't understand why this would result in multiplying the value by 17, and how I would know I am multiplying by the correct value.


Solution

  • add r1, r1, r1, lsl #14

    It's equivalent to r1 = r1 + (r1<<14);

    Also, add r1, r1, r1, lsl #4 is equivalent to r1 = r1 + (r1<<4); which again, is equivalent to r1 = 16*r1 + r1;