Procedure not returning to C correctly in asm?

1

I have a simple findAverage procedure written in assembly language that I am trying to call from a C main program. I had it working for a while, but all of a sudden it gives me this error when the procedure hits ret:

Unhandled exception at 0x0000001E in projOne.exe: 0xC0000005:
Access violation executing location 0x0000001E.v

I have looked on the internet for answers but I cant seem to find one. Here is my C file:

#include<stdio.h>
void printGrades(float grades[]){
    //Print the grades
    for (int i = 0; i < 30; i++){
        printf("Grade %d: %.2f\n", i, grades[i]);
    }
}


void  findAverage(float* grades, float* average, int count);
void  findVariance(float* grades, float* variance, float average, int count);



int main(void){
    float average = 0.0f;
    float variance = 0.0f;
    float grades[] = { 60.0f, 66.4f, 76.6f, 67.9f, 98.4f, 76.3f, 89.0f, 90.5f, 87.4f, 66.9f, 56.7f, 79.1f, 89.2f, 91.3f, 92.5f, 91.1f, 94.2f, 99.5f, 100.0f, 76.6f, 88.4f, 99.8f, 79.8f, 86.5f, 81.3f, 82.1f, 83.1f, 81.1f, 99.1f, 88.0f };
    int count = 30;
    int i = 0;

    printf("Running Program...\n");

    //print original grades
    printGrades(grades);

    //find the average by calling asm proc
    findAverage(&grades[30], &average, count);

    //print average
    printf("The average is: %.2f\n", average);

    findVariance(&grades[30], &variance, average, count);

    //print variance
    printf("The variance is: %.2f\n", variance);


    return 0;
}

And here is my .asm file:

.586
.MODEL FLAT
.DATA

nbrElts DWORD ?
one     DWORD 1
average DWORD ?

.CODE
_findAverage PROC NEAR32
    ;parameter 1 = source
    ;parameter 2 = dest
    ;parameter 3 = size of source

            push    ebp                         ;stack setup
            mov     ebp, esp
            finit                               ;initialize FPU
            fldz                                ;get ready for adding
            mov     eax, REAL4 PTR [ebp +8]     ;source
            mov     ebx, REAL4 PTR [ebp +12]    ;dest
            mov     ecx,[ebp+16]                ;size of source
            mov     nbrElts, ecx                ;count

    sumALL: ;FIND THE AVERAGE
            fadd    REAL4 PTR [eax]             ;ST += source[eax]
            add     eax, 4                      ;next element in source
            loop    sumAll              

            fidiv   nbrElts                     ;ST / count             
            fstp    REAL4 PTR[ebx]              ;dest <- ST                         

            pop     eax                         ;restore registers
            pop     ebx
            pop     ecx
            pop     ebp
            ret

_findAverage    ENDP
END

How do I properly return control to the calling program so that it can execute the next statements?

I am very new to Assembly language, so I apologize if there are any unconventional/weird ways of doing things. Feel free to correct me on those as well.

c
assembly
return
calling-convention
cdecl
asked on Stack Overflow May 7, 2016 by Dane Lowrey

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0