Search code examples
c++intmasmaddition

Simplifying this masm to make it faster?


I am trying to make the c++ code of adding 512 bit (big int) converted into masm inline assembler in visual studio. Carry is very important as the C++ shown in the link below.I do need to represent the c++ into masm inline assembler in visual studio

I tried to do this in masm but it is slow it took mearly 700 ms for my masm code

C++ code Here. C++ Takes 300 ms for addition

Masm code


_asm {
         mov edx, summand
         mov eax, [edx]
         mov ebx, this
         add eax, [ebx]
         mov [ebx], eax

mov ecx, 4 mov eax, [edx + ecx] adc eax, [ebx + ecx] mov [ebx + ecx], eax mov ecx, 8 mov eax, [edx + ecx] adc eax, [ebx + ecx] mov [ebx + ecx], eax mov ecx, 12 mov eax, [edx + ecx] adc eax, [ebx + ecx] mov [ebx + ecx], eax }

Solution

  • It's probably faster to use

     mov eax, [edx + 4]
     adc eax, [ebx + 4]
     mov [ebx + 4], eax
    

    instead of

     mov ecx, 4
     mov eax, [edx + ecx]
     adc eax, [ebx + ecx]
     mov [ebx + ecx], eax
    

    and the same for 8 and 12. But I would be surprised if your asm code is really slower than the C++ code in your link. It may be that using an asm block disables some optimisations in another part of the function. You will have to look at the generated assembler code for the whole function to find that out. (And what is your 700ms?)