c++ - 内联汇编,错误为

标签 c++ c assembly

我收到此错误:

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.

我不知道如何解决这个问题,有人可以帮助我吗?

我的代码是:

#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


    }
}

我该如何做它要求的事情?我尝试在最后执行 push ebp 和 pop 操作,但没有成功。

最佳答案

该错误准确地说明了问题所在。在函数调用后,您并没有始终如一地恢复堆栈指针。这看起来像 VC 输出。您应该编译一个小程序,调用 fopenfreadfclose 来查看堆栈做了什么。每个函数参数 push 必须与返回前添加到 esp 的 4 个字节相匹配。

以下是对可行的猜测:

        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

关于c++ - 内联汇编,错误为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22367749/

相关文章:

c - 如何在 32 位汇编中编译?

c++ - 矩阵数据数组的Tensorflow tflite c++ api推理

c++ - 基于值(value)的向上转型

python - 如何在 c 中加载自定义 python 模块

不包含原型(prototype)的函数类型的兼容性

linux - 为什么 x86 的引导加载程序首先使用 16 位代码?

c - 如何在汇编中的for循环中找到起始值?

c++ - 如何在 C++ 中将 unicode 字符转换为大写

c++ - 制作一个接受数组输入的函数,打印2d c++的元素

c - 产生和处理软件中断