如何设置gcc永久使用intel语法? [英] How to set gcc to use intel syntax permanently?

查看:333
本文介绍了如何设置gcc永久使用intel语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,可以使用gcc命令gcc ./example.c正确编译.程序本身调用函数"add_two",该函数仅将两个整数相加.为了在扩展汇编指令中使用intel语法,我需要首先切换到intel,然后再切换回AT& T.根据gcc文档,可以使用gcc -masm=intel ./exmaple完全转换为intel语法.

I have the following code which compiles fine with the gcc command gcc ./example.c. The program itself calls the function "add_two" which simply adds two integers. To use the intel syntax within the extended assembly instructions I need to switch at first to intel and than back to AT&T. According to the gcc documentation it is possible to switch to intel syntax entirely by using gcc -masm=intel ./exmaple.

每当我尝试使用开关-masm=intel进行编译时,它都不会编译,我也不明白为什么?我已经尝试删除指令.intel_syntax,但仍然无法编译.

Whenever I try to compile it with the switch -masm=intel it won't compile and I don't understand why? I already tried to delete the instruction .intel_syntax but it still don't compile.

#include <stdio.h>

int add_two(int, int);

int main(){
     int src = 3;
     int dst = 5;
     printf("summe = %d \n", add_two(src, dst));
     return 0;
}

int add_two(int src, int dst){

    int sum;

    asm (
        ".intel_syntax;"  //switch to intel syntax
        "mov %0, %1;"
        "add %0, %2;"

        ".att_syntax;"  //switch to at&t syntax
        : "=r" (sum) //output
        : "r" (src), "r" (dst) //input
    );

    return sum;
}

使用gcc -masm=intel ./example.c编译上述程序的错误消息是:

The error message by compiling the above mentioned program with gcc -masm=intel ./example.c is:

tmp/ccEQGI4U.s: Assembler messages:
/tmp/ccEQGI4U.s:55: Error: junk `PTR [rbp-4]' after expression
/tmp/ccEQGI4U.s:55: Error: too many memory references for `mov'
/tmp/ccEQGI4U.s:56: Error: too many memory references for `mov' 

推荐答案

请注意,-masm=会影响默认的内联汇编器语法:

使用选定的方言输出汇编指令.也影响 哪种方言用于基本的"asm"和扩展的"asm".支持的 选项(按方言顺序)是att或intel.默认值是att. 达尔文不支持英特尔.

Output assembly instructions using selected dialect. Also affects which dialect is used for basic "asm" and extended "asm". Supported choices (in dialect order) are att or intel. The default is att. Darwin does not support intel.

这意味着您的第一个.intel_syntax指令是多余的,而最后一个.att_syntax是错误的,因为您的GCC调用将C编译为Intel汇编代码.

That means that your first .intel_syntax directive is superfluous and the final .att_syntax is wrong because your GCC call compiles C to Intel assembler code.

IOW,要么坚持使用-masm=intel,要么将内联的英特尔汇编代码段放在.intel_syntax noprefix.att_syntax prefix指令之间-但不要同时执行.

IOW, either stick to -masm=intel or sandwich your inline Intel assembler code sections between .intel_syntax noprefix and .att_syntax prefix directives - but don't do both.

请注意,sandwich方法与所有内联汇编程序约束均不兼容-例如包含m的约束(即内存操作数)将以ATT语法插入操作数,这将产生类似``错误:表达式后为垃圾(%rbp)''的错误.在这种情况下,您必须使用-masm=intel.

Note that the sandwich method isn't compatible with all inline assembler constraints - e.g. a constraint that involves m (i.e. memory operand) would insert an operand in ATT syntax which would yield an error like 'Error: junk (%rbp) after expression'. In those cases you have to use -masm=intel.

这篇关于如何设置gcc永久使用intel语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆