Why does my code for a floating point accumulator throw an exception in C with inline assembly made on visual studio?

24 Views Asked by At

So, as a bit of context, I have to make a program that calculates the regression line for a given number of points as a school project using inline assembly.

I don't know where, but some lines throw exceptions. In order to try and solve them, I first tried to make an isolated floating point accumulator using the FLD and FSTP instructions, here shown:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NUM_OF_POINTS 1000000

int main(void) {
  
   
    float* vector = (float*)malloc(NUM_OF_POINTS * sizeof(float));
    
    for (int i = 0; i < NUM_OF_POINTS; i++)
        vector[i] = 10000;

    //Acts as the acumulator
    float sum = 0;
    //i is used as the loop counter, k as the limit
    int i = 0, k = NUM_OF_POINTS*4;
    
    _asm 
    {
    looop:;

    ;We bring the value from k to EAX to do the comparasion
        mov EAX, k;
    ;Conditional flags
        cmp i, EAX;
    ;If both values are equal que go to the end: label
        je end;

    ;We put the first element of vector on the stack
        mov ESI, vector;
        add ESI, i;
        FLD [ESI];
    ;We put the sum in the stack
        FLD sum;
    ;We add the sum and the element of the vector
        FADD;
    ;We put the calculated value in the sum
        FSTP sum;
    ;We remove the element of the vector from the stack
        add ESP, 4;
    ;We increment the loop counter by 4
        add i, 4;
    ;The loop begins once again
        jmp looop;

    end:;
    }


    printf("here you go: %f", sum);
    return 0;
}

Now, I thought it would print me the value without issue, but instead, Visual Studio tells me that the printf causes an exception (0xC0000005) due to an access infraction when trying to write in an invalid direction. Now, I assume the exception is not caused by the printf on itself, rather a mistake I made when using the stack or maybe because the sum variable suffers overflow? Maybe because I should use pop instead of adding 4 to the ESP register? Some advice would be greatly appreciated :)

0

There are 0 best solutions below