Home > Enterprise >  Assembly 32bits sum of elements of two vectors
Assembly 32bits sum of elements of two vectors

Time:06-01

I have a problem with sum of elements of two vectors type double which are the same size. Code always returns 0.

#include <iostream>
using namespace std;
int main()
{
    int n = 5;
    double* tab = new double[n];
    double* tab3 = new double[n];
    for (size_t i = 0; i < n; i  )
    {
        tab[i] = 1;
        tab3[i] = 1;
    }
    double sum;
    __asm {
        mov eax, n; //vector size
        mov edi, tab; //first vector
        mov esi, tab3; //second vector
        fldz;
    l:
        fadd[edi   8 * eax - 8];
        fadd[esi   8 * eax - 8];
        dec eax;
        jnz l;
        fstp sum;
    }
    cout << sum;
}

CodePudding user response:

Sadly i am not on windows, so i had to modify the code to use g instead of msvc, but i used intel syntax assembly too. During debugging it turned out that fadd instructions had no effect. I fixed it by adding qword ptr before the [edi 8 * eax - 8] and [esi 8 * eax - 8] to tell assembler to use pointers to an 8 byte value (since you are using double instead of float):

fadd qword ptr [edi   8 * eax - 8];
fadd qword ptr [esi   8 * eax - 8];
  • Related