4

I'm looking at some assembler code of a small C++ program which divides 100 by 10 however I see in the assembler that there are some strange numbers that don't make sense. I have tried looking through information on this site and a few other places to no avail.

The c++ line I'm referring to is:

 x = x / 10; //myInt is now 10

and the assembly I get is:

        mov     eax, DWORD PTR [rbp-4]
    movsx   rdx, eax
    imul    rdx, rdx, 1717986919
    shr     rdx, 32
    sar     edx, 2
    sar     eax, 31
    mov     ecx, eax
    mov     eax, edx
    sub     eax, ecx
    mov     DWORD PTR [rbp-4], eax

I'm confused by the 1717986919, the 32, 2 and 31. I'm guessing it's something to do with the sign extend in line 2 of assembly and the number now being a signed 32 bit integer. Am I on the right track or am I way off? Any help or useful resources would be greatly appreciated, thanks!

  • 2
    Yup, the `sar` stuff is a fixup to correctly round negative `x` after using the fixed-point multiplicative inverse trick. – Peter Cordes Jul 12 '21 at 21:00
  • [Why does division by 3 require a rightshift (and other oddities) on x86?](https://stackoverflow.com/q/63417818/995714) – phuclv Jul 14 '21 at 01:42

0 Answers0