我收到此错误:
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 输出。您应该编译一个小程序,调用 fopen
、fread
和 fclose
来查看堆栈做了什么。每个函数参数 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/