内联汇编GCD不起作用

时间:2012-10-16 10:45:32

标签: c++ visual-c++ assembly x86 inline-assembly

我一直在编写一个简单的c ++程序,它使用Assembly来获取2个数字的GCD并输出它们作为我观看的教程中使用的示例。我理解它在做什么,但我不明白为什么它不起作用。 编辑:应该在运行时添加它,它根本不输出任何内容。

#include <iostream>
using namespace std;

int gcd(int a, int b)
{
int result;
_asm
{
    push ebp
    mov ebp, esp
    mov eax, a
    mov ebx, b
looptop:
    cmp eax, 0
    je goback
    cmp eax, ebx
    jge modulo
    xchg eax, ebx
modulo:
    idiv ebx
    mov eax, edx
    jmp looptop
goback:
    mov eax, ebx
    mov esp, ebp
    pop ebp

    mov result, edx
}

return result;
}

int main()
{
cout << gcd(46,90) << endl;
    return 0;
}

我在32位Windows系统上运行它,任何帮助将不胜感激。编译时,我得到4个错误:

warning C4731: 'gcd' : frame pointer register 'ebp' modified by inline assembly code
warning C4731: 'gcd' : frame pointer register 'ebp' modified by inline assembly code
warning C4731: 'main' : frame pointer register 'ebp' modified by inline assembly code
warning C4731: 'main' : frame pointer register 'ebp' modified by inline assembly code

1 个答案:

答案 0 :(得分:3)

编译器将在函数的开头和结尾为您插入这些或等效的指令:

push ebp
mov ebp, esp
...
mov esp, ebp
pop ebp

如果手动添加它们,您将无法通过ebp访问函数的参数,这就是编译器发出警告的原因。

删除这4条说明。

此外,开始使用调试器。今天。