如何在内联汇编中访问C / C ++变量?

时间:2015-10-20 01:59:24

标签: c++ assembly x86-64

以下是我尝试的一些代码:

#include <iostream>
using namespace std;
int a;
int main(){
asm("\nmov dword [rsp],70\n");
cout << a;}

在上面的代码中,我尝试通过写入esp指向的位置(堆栈指针)来写入我的整数(a)。这应该有效,因为那是变量实际所在的位置。但是当我尝试使用命令

编译程序时
g++ prog.cpp -masm=intel

我收到以下错误:

prog.cpp: Assembler messages:
prog.cpp:6: Error: ambiguous operand size for `mov'

我也尝试过以下代码:

#include <iostream>
using namespace std;
int a=5;
int main(){
asm(
"\npop rax\n"
"inc rax\n"
"push rax\n"
);
cout << a;}

此程序与上一个程序的主要区别在于,我使用esppush而不是使用pop来访问堆栈。这段代码编译。但是当我运行它时,它只返回5 - 与我们开始时的数字相同!

有人可以向我解释一下我在上述程序中做错了吗?

0 个答案:

没有答案
相关问题