【发布时间】:2016-04-01 14:36:37
【问题描述】:
我正在运行 Ubuntu 64 位,并且我有以下代码:
#include <stdio.h>
int main() {
int x, y;
int z = 0;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
asm(".intel_syntax noprefix\n"
"MOV EAX, _x\n"
"MOV ECX, _y\n"
"ADD EAX, ECX\n"
"MOV _z, EAX\n"
".att_syntax\n");
printf("%d + %d = %d \n", x, y, z);
return 0;
}
根据在学校的讲座,它应该可以工作,但是当我尝试用 GCC 编译它时,我得到了这个错误:
/tmp/ccU4vNLr.o: In function `main':
Jung_79913_211.c:(.text+0x4a): undefined reference to `_x'
Jung_79913_211.c:(.text+0x51): undefined reference to `_y'
Jung_79913_211.c:(.text+0x5a): undefined reference to `_z'
collect2: error: ld returned 1 exit status
我知道 GCC 默认使用 AT&T asm 语法,但我在大学需要 Intel systax。所以问题是,我怎样才能让它工作?
【问题讨论】:
-
查看 gcc 文档以了解扩展的内联汇编语法。您必须将参数传递给汇编代码;依靠具有特定名称的当地人已经过时了 20 多年。此外,您还应该使用特定的破坏寄存器。