如何在没有"\ t \ n"的情况下在asm()中编写多个汇编语句使用GCC分隔每一行? [英] How to write multiple assembly statements within asm() without "\t\n" separating each line using GCC?

查看:337
本文介绍了如何在没有"\ t \ n"的情况下在asm()中编写多个汇编语句使用GCC分隔每一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用GCC在asm()中编写多个汇编语句而不用"\ t \ n"分隔每一行?

How to write multiple assembly statements within asm() without "\t\n" separating each line using GCC?

我已经看到一些教科书在asm()中以如下形式编写多个汇编语句:

I've seen some textbooks write multiple assembly statements within asm() as:

asm("
movl $4, %eax
movl $2, %ebx
addl %eax, %ebx
...
");

但是,我的编译器(GCC)无法识别此语法.相反,我必须依靠"\t\n"分隔每行或使用多个asm():

However, my compiler (GCC) doesn't recognize this syntax. Instead, I must rely on "\t\n" separating each line or using multiple asm():

asm(
"movl $4, %eax\t\n"
"movl $2, %ebx\t\n"
"addl %eax, %ebx\t\n"
...);

asm("movl $4, %eax");
asm("movl $2, %ebx");
asm("addl %eax, %ebx");
...

如何在没有"\t\n"或重复asm()的情况下启用干净"语法?

How do I enable the "clean" syntax with no "\t\n" or repeated asm()?

推荐答案

GCC

不建议您使用内联汇编程序,因为您在不通知编译器的情况下更改了寄存器.您应使用 GCC的扩展内联汇编程序,并具有适当的输入和输出约束.使用内联汇编程序应作为最后的手段,并且您应该确切地了解自己在做什么. GCC 的内联汇编非常难以理解,因为似乎有效的代码甚至可能都不正确.

GCC

Your inline assembly is ill advised since you alter registers without informing the compiler. You should use GCC's extended inline assembler with proper input and output constraints. Using inline assembler should be used as a last resort and you should understand exactly what you are doing. GCC's inline assembly is very unforgiving, as code that seems to work may not even be correct.

这样说来,每个字符串以\n\t结尾使生成的汇编代码看起来更简洁.您可以通过使用-S参数进行编译以生成相应的汇编代码来查看此代码.您可以选择使用;(分号).这将分隔每条指令,但将在同一行上输出所有指令.

With that being said ending each string with \n\t makes the generated assembler code look cleaner. You can see this by compiling with the -S parameter to generate the corresponding assembly code. You do have the option of using a ; (semicolon). This will separate each instruction but will output all of the instructions on the same line.

另一种选择是使用 C 行继续符\(反斜杠).尽管以下内容将在生成汇编代码时生成过多的空白,但仍将按预期进行编译:

Another option is to use C line continuation character \ (backslash). Although the following will generate excessive white space in generate assembly code it will compile as expected:

int main()
{
    __asm__("movl $4, %eax; \
             movl $2, %ebx; \
             addl %eax, %ebx");
}

尽管这是一种实现方式,但我并不建议这是一种很好的形式.对于在第二个示例中使用的不带行连续字符的\n\t格式,我有一个偏爱.

Although this is a way of doing it, I'm not suggesting that this is good form. I have a preference for the form you use in your second example using \n\t without line continuation characters.

关于将多个指令拆分为单独的 ASM 语句:

Regarding splitting up multiple instructions into separate ASM statements:

asm("movl $4, %eax");
asm("movl $2, %ebx");
asm("addl %eax, %ebx");

这是有问题的.编译器可以相互重新排序,因为它们是基本的汇编程序,没有依赖性.编译器可能会生成以下代码:

This is problematic. The compiler can reorder these relative to one another since they are basic assembler with no dependencies. It is possible for a compiler to generate this code:

movl $4, %eax
addl %eax, %ebx
movl $2, %ebx

这当然不会产生您期望的结果.当您将所有指令放在单个 ASM 语句中时,它们将按照您指定的顺序生成.

This of course would not generate the result you expect. When you place all the instructions in a single ASM statement they will be generated in the order you specify.

32位Microsoft C和C ++编译器支持对该语言的扩展,该扩展使您可以在__asm {}之间放置多行内联汇编.使用这种机制,您无需将内联程序集放置在 C 字符串中;不需要使用行继续;无需以;(分号)结尾的语句.

32-bit Microsoft C and C++ compilers support an extension to the language that allows you to place multi-line inline assembly between __asm { and }. Using this mechanism you don't place the inline assembly in a C string; don't need to use line continuation; and no need to end a statement with with a ; (semicolon).

例如:

__asm {
    mov eax, 4
    mov ebx, 2
    add ebx, eax
}

这篇关于如何在没有"\ t \ n"的情况下在asm()中编写多个汇编语句使用GCC分隔每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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