When performing division on ARM, this is the code snippet that I encountered.

   0x83d8 <main+12>:    mov r3, #10
   0x83dc <main+16>:    str r3, [r11, #-8]
   0x83e0 <main+20>:    ldr r3, [r11, #-8]
=> 0x83e4 <main+24>:    ldr r2, [pc, #40]   ;; 0x8414 <main+72>
   0x83e8 <main+28>:    smull   r1, r2, r2, r3
   0x83ec <main+32>:    asr r3, r3, #31
   0x83f0 <main+36>:    rsb r3, r3, r2
   0x83f4 <main+40>:    str r3, [r11, #-8]

In the original program, I store the value 10 to a variable, divide it by 3 and store it in the same variable.

[r11, #-8] in the above example has the value 0xa. After 0x83e4, r2 is loaded up as 0x55555556. My doubts are as follows :-

  1. Is this a common way of performing division without the div instruction?
  2. What are the other ways you have encountered in which division is performed without using an instruction that performs division?

Optimizing compilers will typically use the method above for compiling division by a constant.

You can read more about it at the following links:

| improve this answer | |