Assembly and template classes

Viewed 936

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;

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

Thanks in advance

Tritron.

1 Answers
Related