Search code examples
assemblydinline-assemblymasm

Inline assembly equivalent of .data MASM directive


I am curious as to what the equivalent of (for example)

        .data
x       dword 42
        .code
main    proc
        ; Do stuff here.
        ret
main    endp
        end

would be, if I were writing inline assembly (in D). I understand I could do something like

int main()
{
    int x = 42;
    asm
    {
        // Do stuff here.
    }

    return 0;
}

, but the point of the exercise is not to 'cheat' by using D itself.


Solution

  • D's inline assembler does not have the ability to set which section the code or data will be emitted to. It will always go into the same section as the code for the function it is embedded in.

    But you can insert data into D's data segment using ordinary D declarations:

    __gshared int x = 42;
    

    Note that if you don't use __gshared, x will wind up in the thread local storage blocks, and will require specially generated code to access. Also, if the code is compiled with -fPIC, specially generated code will be required to access all data segment data.