简体   繁体   English

C ++内联汇编:如何处理引用?

[英]C++ inline assembly: how to deal with references?

How to deal with references in function from inline 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 is still 0 after function execution, however this one works fine 但是在执行函数后x仍为0,但是这个工作正常

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

How to solve that? 怎么解决?

Thanks. 谢谢。

A reference is a pointer with value semantics - in assembly language these semantics are irrelevant, 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, optimisations, etc. all the usual stuff when using inline assembly.) (当然,YMMV取决于编译器,版本,优化等,使用内联汇编时的所有常用内容。)

Reference is essentially a pointer, an address of the value, not the value itself. 引用本质上是一个指针,一个值的地址,而不是值本身。 So this works for example: 所以这适用于例如:

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

Output: 输出:

10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM