简体   繁体   English

在GCC中的Intel和ATT模式之间切换

[英]Switching between Intel and ATT mode in GCC

So I have this inline assembly code along with my C code, and I want to use intel syntax for this particular call to asm(), however I need to switch back to ATT syntax or else it will give a long list of errors. 因此,我拥有此内联汇编代码以及C代码,并且我想针对此对asm()的特定调用使用intel语法,但是我需要切换回ATT语法,否则它将给出一堆错误。

asm(".intel_syntax prefix");
     asm volatile (
        "add %0, $1 \n\t"
         : "=r" (dst)
         : "r" (src)); 

asm(".att_syntax prefix");

Now it gives the following error 现在给出以下错误

/tmp/ccDNa2Wk.s: Assembler messages:
/tmp/ccDNa2Wk.s:180: Error: no such instruction: `movl -16(%ebp),%eax'
/tmp/ccDNa2Wk.s:187: Error: no such instruction: `movl %eax,-12(%ebp)'

I dont understand how to fix the error, i have no call to movl in any part of my code. 我不明白如何解决该错误,我没有在我的代码的任何部分调用movl。

Since you haven't yet accepted an answer (<hint><hint>), let me add a third thought: 由于您尚未接受答案(<hint> <hint>),让我添加第三个想法:

1) Instead of having 3 asm statements, do it in 1: 1)在1中执行以下操作,而不是使用3条asm语句:

asm(".intel_syntax prefix\n\t"
    "add %0, 1 \n\t"
    ".att_syntax prefix"
     : "=r" (dst)
     : "r" (src));

2) Change your compile options to include -masm=intel and omit the 2 syntax statements. 2)更改编译选项以包括-masm=intel并省略2个语法语句。

3) It is possible to support both intel and att at the same time. 3)可以在同一时间同时支持英特尔和ATT。 This way your code works whatever value is passed for -masm: 这样,无论为-masm传递了什么值,您的代码都可以工作:

asm("{addl $1, %0 | add %0, 1}"
     : "=r" (dst)
     : "r" (src));

I should also mention that your asm may not work as expected. 我还要指出,您的asm可能无法正常工作。 Since you are updating the contents of dst (instead of overwriting it), you probably want to use "+r" instead of "=r". 由于您正在更新dst的内容(而不是覆盖它),因此您可能要使用“ + r”而不是“ = r”。 And you do realize that this code doesn't actually use src, right? 您确实意识到该代码实际上并没有使用src,对吗?

Oh, and your original asm is NOT intel format (the $1 is the give-away). 哦,您原来的asm不是intel格式的($ 1是赠品)。

I would try to do the following tests: 我会尝试做以下测试:

In some C code not containing inline assembler insert the line 在一些不包含内联汇编器的C代码中插入行

asm(".att_syntax prefix");

in multiple different locations. 在多个不同的位置。 Then compile the C code to object files and disassemble these object files (compiling to assembler won't work for this test). 然后将C代码编译为目标文件并反汇编这些目标文件(在此测试中,编译为汇编程序将无效)。

Then compare the disassembly of the original code with the disassembly of the code containing the ".att_syntax" lines. 然后将原始代码的反汇编与包含“ .att_syntax”行的代码的反汇编进行比较。

If the line ".att_syntax prefix" indeed is the correct line for switching back to AT&T mode the disassemblies must be equal AND compiling must work without any errors. 如果“ .att_syntax前缀”行确实是切换回AT&T模式的正确行,则拆卸必须相等,并且编译必须正确无误。

In the next step take your code and compile to assembler instead of object code ("-S" option of GCC). 在下一步中,将您的代码编译为汇编程序,而不是目标代码(GCC的“ -S”选项)。 Then you can look at the assembler code. 然后,您可以查看汇编代码。

My idea is the following one: 我的想法如下:

If you use data exchange in inline assembler ("=r" and "r" for example) GCC needs to insert code that is doing the data exchange: 如果您在内联汇编程序中使用数据交换(例如“ = r”和“ r”),则GCC需要插入进行数据交换的代码:

 asm(".intel_syntax prefix");
 // GCC inserts code moving "src" to "%0" here
 asm volatile (
    "add %0, $1 \n\t"
     : "=r" (dst)
     : "r" (src)); 
 // GCC inserts code moving "%0" to "dst" here
 asm(".att_syntax prefix");

This code inserted by GCC is of course in AT&T syntax. GCC插入的这段代码当然是AT&T语法。

If you want to use Intel syntax in inline assembly you have to use the ".att_syntax" and ".intel_syntax" instructions in the same inline assembly block, just like this: 如果要在嵌入式汇编中使用Intel语法,则必须在同一嵌入式汇编块中使用“ .att_syntax”和“ .intel_syntax”指令,如下所示:

 // GCC inserts code moving "src" to "%0" here
 asm volatile (
    ".intel_syntax prefix \n\t"
    "add %0, $1 \n\t"
    ".att_syntax prefix \n\t"
     : "=r" (dst)
     : "r" (src)); 
 // GCC inserts code moving "%0" to "dst" here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM