C ++ inline assembly: how to handle links?

How to work with links in functions from the built-in assembler? I'm trying this

void foo(int& x)
{
    __asm mov x, 10
}

int main()
{
    int x = 0;
    foo(x);
    std::cout << x << std::endl;
}

but x after executing the function is still 0, however this mode works fine

int x = 0;
__asm mov x, 10
std::cout << x << std::endl;

How to solve this?

Thank.

+3
source share
2 answers

A link is a pointer with semantics meaning - in assembly language, these semantics do not matter, so you are left with a pointer:

void foo(int& x)
{
    __asm { 
        mov eax, x
        mov DWORD PTR [eax], 10
    }
}

(Of course, YMMV depending on the compiler, version, optimizations, etc. are all normal things when using the built-in assembly.)

+4
source

A reference is essentially a pointer, the address of a value, and not the value itself. So this works, for example:

void foo(int& x)
{
    __asm mov eax, x
    __asm mov dword ptr [eax], 10
}

Conclusion:

10
+2
source

Source: https://habr.com/ru/post/1791446/


All Articles