Search code examples
assemblydinline-assembly

D Inline Assembler: Access Static Variable


I got some trouble accessing a static Variable with Inline Assembler in the D Programming Language. The documentation says that I have to access local variables with

mov EAX, var[EBP]; //or mov EAX, var;

and class Variables with

mov EBX, this;
mov EAX, var[EBX];

But it isnt documented how to access a static Variable. Here is my code that throws an error:

module test;

static int A = 1234;

static void SetA()
{
    asm
    {
        mov A, 5432; //compiles, but throws an error
        //tried it with "mov dword ptr [A], 5432; too
    }
}

I really need a way of some "global storage" for integers that are accessible from both, assembler and D, I would be very happy about any help with this (or an alternative way).


Solution

  • Global variables are placed in thread-local storage by default in D2. Use __gshared to declare a "classic" static variable.

    This works:

    module test;
    
    __gshared int A = 1234;
    
    void SetA()
    {
        asm
        {
            mov A, 5432;
        }
    }
    
    unittest
    {
        SetA();
        assert(A == 5432);
    }