Dean
Dean

Reputation: 517

inline asm, error with

I'm getting this error:

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

I don't know how to solve it, can anyone help me?

My code is:

#include "common.h"

char* file = "c:\\town.las";
char* file_mode = "r";


#pragma pack(1)
struct LASHEADER
{
    char LASF[4];
};


void main()
{
    LASHEADER lasHeader;
    FILE* of;

    __asm
    {
            push ebp       


            mov     eax, file_mode
            push    eax
            push    file
            call    fopen
            mov of, eax

            mov esi, esp
            mov eax, DWORD PTR of
            push eax
            push 1
            push 4 // this should be sizeof LASHEADER

            lea ecx, DWORD PTR lasHeader
            push ecx

            call DWORD PTR fread
            add esp, 16
            cmp esi, esp



            mov eax, of
            push eax
            call fclose


    }
}

how can i do what it asked? i tried to do push ebp and pop at the end with no luck.

Upvotes: 2

Views: 551

Answers (1)

Gene
Gene

Reputation: 46960

The error says exactly what's wrong. You are not consistently restoring the stack pointer after function calls. This looks like VC output. You should compile a small program that calls fopen, fread, and fclose to see what's done with the stack. Every function parameter push must be matched by 4 bytes added to esp before returning.

Here's a guess at what will work:

        push ebp       

        push    file_mode  ; 1 word
        push    file       ; 2 words
        call    fopen
        mov of, eax        ; this could be wrong depending on compiler

        mov esi, esp
        mov eax, DWORD PTR of
        push eax ; 3 words
        push 1 ; 4 words
        push 4 ; 5 words

        lea ecx, DWORD PTR lasHeader
        push ecx ; 6 words

        call DWORD PTR fread

        mov eax, of ; again could be wrong depending on compiler
        push eax  ; 7 words
        call fclose

        add esp, 28 ; REMOVE 7 words from the stack

        pop ebp

Upvotes: 2

Related Questions