使用Intel语法的内联汇编,链接到局部变量 [英] Inline assembly with the Intel syntax, linking to local variables

查看:154
本文介绍了使用Intel语法的内联汇编,链接到局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将现有代码从Windows移植到Linux(从Visual C ++到g ++).它包括内联汇编节.

g ++支持编译开关(-masm=intel),告诉它接受Intel语法而不是AT& T语法.到目前为止,它应该避免痛苦地重写数千行.

无论如何,在内联汇编块之外引用C ++变量都存在问题.

例如,建筑

I am porting existing code from Windows to Linux (Visual C++ to g++). It includes inline assembly sections.

g++ supports a compilation switch (-masm=intel) telling it to accept the Intel syntax instead of the AT&T one. So far so good, it should avoid painful rewriting of thousands of lines.

Anyway, there are issues with referencing the C++ variables outside the inline assembly blocks.

For instance, building

void Test()
{
  int i;
  asm("mov [i], 5");
  ...
}


生成链接器error undefined reference to `i''.而且汇编器似乎对分支标签感到困惑.

从网络搜索来看,使用Intel语法时,似乎无法从程序集引用局部变量:-(..我发现了几篇相关的文章,但没有一篇救我.

这很烦人,因为我无法将变量设置为全局变量. (这将破坏多线程的安全性,并且似乎与名称空间不兼容.)

任何提示吗?


results in the linker error undefined reference to `i''. And the assembler seems to get confused with the branching labels.

From Web searches, it seems that local variables cannot be referenced from assembly when using the Intel syntax :-(. I found several related posts, but none rescued me.

This is annoying as I can''t make my variables global. (This would break multi-thread safeness and it does not seem to be compatible with namespaces).

Any hint ?

推荐答案

您必须同时使用两种语法..


例如)
you have to use both syntax..


ex)
#define TEST(out, in) \
           asm\
            (               \
              "mov %1, 5\n\t"\
              ".intel_syntax noprefix;" \
              --------intel syntax --------
              ".att_syntax;" \
              :"=g" (out) \
              :"g" (in)\
            );\




并在没有-masm = intel




and compile without -masm=intel


的情况下进行编译面对技术难题,我放弃了使用内联汇编的想法.

由于无论如何都需要对源代码进行完全重做,因此这会打开新的选项.

我选择的选项是切换到SSE内部调用,因为它们可以在Microsoft和GNU编译器之间移植,并且似乎是矢量化的未来.使用这些内在函数进行编程要容易得多,因为您不必担心寄存器分配.此外,Visual C ++编译器不支持64位处理器的内联汇编,这迫使使用内在函数.
In front of the technical difficulties, I am giving up the idea to use inline assembly.

As in any case a full rework of the source code is required, this opens new options.

The option I have chosen is to switch to the SSE intrinsics calls, as they are portable between Microsoft and GNU compilers and appear to be the future of vectorization. Programming with those intrinsics is much easier as you needn''t worry about register allocation. In addition, the Visual C++ compilers do not support inline assembly for 64 bits processors, compelling the use of the intrinsics.


这篇关于使用Intel语法的内联汇编,链接到局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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