将Visual C / C ++内联汇编代码转换为GCC等效代码

时间:2014-01-21 18:59:53

标签: c++ c gcc inline-assembly

我找到了以下函数here,并希望在MinGW下编译它:

size_t stackavail()
{
    static unsigned StackPtr;
    __asm mov [StackPtr],esp
    static MEMORY_BASIC_INFORMATION mbi;
    VirtualQuery((PVOID)StackPtr,&mbi,sizeof(mbi));
    return StackPtr-(unsigned)mbi.AllocationBase;
}

我尝试修改上面的代码并设法编译并运行它:

size_t stackavail()
{
    static unsigned StackPtr;

    __asm__ ("movl %%esp, %0\n\t" : "=r" (StackPtr) );
    MEMORY_BASIC_INFORMATION mbi;
    VirtualQuery((PVOID)StackPtr, &mbi, sizeof(mbi));
    return StackPtr-(unsigned)mbi.AllocationBase;
}

但我有疑问。这是对的吗?还有其他办法吗?

1 个答案:

答案 0 :(得分:0)

您可以摆脱内联ASM,使用

void* StackPtr = &StackPtr;