c++assemblyinline-assemblyasmjit

Referring global variable with AsmJit


I need to to load an address of an existing global variable/ exernal variable to a register with a lea operation. Is this possible in AsmJit? The associated ptr function only seem to accept GpVar which needs to be created within AsmJit.


Solution

  • There are multiple ways of doing this. The most portable and recommended way would be to use mov reg, imm:

    using namespace asmjit;
    using namespace asmjit::host;
    
    // You have to initialize these...
    Compiler c;
    
    GpVar var(c, kVarTypeIntPtr);
    
    void* p = NULL;
    c.mov(var, imm_ptr(p));
    

    Or lea reg, mem having an absolute address [mem] form. This solution works as expected only in 32-bit mode; the absolute address size is always truncated to 32 bits:

    c.lea(var, ptr_abs(p));