Why double elements from arrays don't load to FP registers?

104 Views Asked by At

So, I have functions like this using C++:

double fun_w22(double* xx, double* z, int N) {
    double result= 0.0;

    for (int i = 0; i < N; i++) {
        result+= xx[i] * z[i];
    }

    return result;
}

And the same function using FPU x86:

double fun_w22_asm(double* xx, double* z, int N) {
    double result= 0.0;
    __asm {
        mov ecx, N
        mov esi, xx
        mov edi, z 
        fld [esi]
        fld [edi]
        fmul [esi]  
        dec ecx 
    petla:
        add esi, 8
        add edi, 8
        fld[esi]
        fmul[edi]
        fadd
        dec ecx
    jnz petla
        fstp result
    }

    return result;
}

The problem is that elements of vectors does not load to registers so assembly function does not show right result. Here is main function:

int main() {

    int N=3;
    
    double xx[] = { 1.0, 2.0, 3.0 }; // Sample array x
    double z[] = { 4.0, 5.0, 6.0 }; // Sample array z

    // Call the function to calculate the result
    double result = fun_w22(xx, z, N);
    double result1 = fun_w22_asm(xx, z, N);
    // Display the result
    std::cout << "Result: " << result << std::endl;
    std::cout << "Result ASM: " << result1 << std::endl;

    return 0;
}

I was expecting the same result but the assembly code runs and writes out 0 as a result. When I turn on disassebly in VS, I see random numbers in registers, not numbers as should be. I don't know if I should have someting extra in my code or turned on in my VS. I really need to understand this.

0

There are 0 best solutions below