装配和模板类 [英] Assembly and template classes

查看:36
本文介绍了装配和模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个小型项目,并试图获取一些硬编码值以内联汇编.为此,我正在使用模板.我创建了一个代码片段以显示所看到的内容

I'm working on small project and trying to get some hardcoded values to inline assembly. For doing that I'm using templates. I have created a code snip to show what I'm seeing

      #include <iostream>

      template <size_t T>
      struct MyClass
      {
          size_t myValue = T;

          void doSomething()
          {
              size_t value = T;
              __asm
              {
                  mov eax, [T]
                  mov [value], eax
              }
              std::cout << value << std::endl;
          }
      };

   int main()
   {
       auto o = new MyClass<999>();

       o->doSomething();
       return 0;
   }

事实证明,对于汇编代码,它正在尝试使用数据段,而不是直接在此处粘贴数字"

It turns out that for the assembly code it is trying to use the data segment instead "pasting the number there directly"

        ; 25   : {

            push    ebp
            mov ebp, esp
            push    ecx

        ; 26   :     auto o = new MyClass<999>();

            push    4
            call    ??2@YAPAXI@Z                ; operator new
            add esp, 4

        ; 14   :         size_t value = T;

            mov DWORD PTR _value$2[ebp], 999        ; 000003e7H

        ; 26   :     auto o = new MyClass<999>();

            mov DWORD PTR [eax], 0
            mov DWORD PTR [eax], 999            ; 000003e7H

        ; 15   :         __asm
        ; 16   :         {
        ; 17   :             mov eax, [T]

            mov eax, DWORD PTR ds:0

        ; 18   :             mov [value], eax

            mov DWORD PTR _value$2[ebp], eax

        ; 19   :         }
        ; 20   :         std::cout << value << std::endl;

我正在使用Visual Studio2015.还有其他方法可以实现这一目标.

I'm using Visual Studio 2015. Is there any other way to achieve this.

预先感谢

Tritron.

推荐答案

嗯,这是一个可爱而又曲折的问题!

Ahh, what a lovely and twisted question!

我尝试了用T初始化的constexpr变量.结果是相同的-从内存加载的值.宏可用于将文字传递给内联汇编,但是它们不能与模板很好地混合使用.

I tried a constexpr variable initialized with T. The result was the same - value loaded from memory. Macros can be used to pass literals to inline assembly, but they don't mix well with templates.

使用T初始化类内的枚举在理论上应该起作用( https://msdn.microsoft.com/zh-cn/library/ydwz5zc6.aspx 提到枚举可用于内联汇编),但在内联汇编中使用该枚举会导致Visual Studio 2015编译器崩溃:-).

Initializing an enum inside the class using T should in theory work (https://msdn.microsoft.com/en-us/library/ydwz5zc6.aspx mentions enums can be used in inline assembly), but using that in the inline assembly crashes the visual studio 2015 compiler :-).

似乎有用的是一个函数模板,该函数模板使用template参数声明一个枚举,然后在内联汇编中使用该枚举.如果必须在模板化类中使用它,则可以在该类内部实例化模板函数,如下所示:

What seems to work is a function template that declares an enum using the template parameter, and then using that enum in the inline assembly. If you must have it in a templated class, you can instantiate the template function inside the class like this:

    #include <iostream>

    template <size_t T> void dosomething() {
      enum { LOCALENUM = T };
      size_t value = 0;
      __asm
      {
        mov eax, LOCALENUM
        mov[value], eax
      }
      std::cout << value << std::endl;
    }


    template <size_t T>
    struct MyClass
    {
      size_t myValue = T;
      void doSomething()
      {
        ::dosomething<T>();
      }
    };

    int main()
    {
      //dosomething<999>();
      auto o = new MyClass<999>();
      o->doSomething();
      return 0;
    }

这将导致以下汇编代码:

This results in the following assembly code:

     auto o = new MyClass<999>();
     001B1015  mov         dword ptr [eax],0  
     001B101B  mov         dword ptr [eax],3E7h  
      o->doSomething();
     001B1021  mov         eax,3E7h    <--- Victory!
     001B1026  mov         dword ptr [ebp-4],eax  
     001B1029  mov         ecx,dword ptr            [_imp_?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A (01B2048h)]  
     001B102F  push        offset std::endl<char,std::char_traits<char> > (01B1050h)  
     001B1034  push        dword ptr [ebp-4]  
     001B1037  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (01B2044h)]  

这篇关于装配和模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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