如何将寄存器的值存储到指针指向的内存位置?

Emi*_*l D 8 c++ x86 assembly pointers

我有以下代码:

void * storage = malloc( 4 );

__asm
{
    //assume the integer 1 is stored in eax
    mov eax, storage  //I've tried *storage as well but apparently it's illegal syntax
}
/* other code here */
free(storage);
Run Code Online (Sandbox Code Playgroud)

但是,在代码中,当我取消引用存储指针(如*(int *)storage)时,我没有得到1.那么,将寄存器的值存储到C++指针所指向的内存中的正确方法是什么?

AnT*_*AnT 5

你确定你知道你真正需要什么吗?您请求将寄存器值存储到由malloc(由指针指向)分配的内存中的代码,即*(int*) storage位置,但您接受了将值存储(或至少尝试存储)到指针本身的答案,这是完全不同的事情.

要存储eax到"由指针指向"的内存中,即*(int*) storage按照您的要求存储,您必须执行类似的操作

mov  edi, dword ptr storage
mov  dword ptr [edi], eax
Run Code Online (Sandbox Code Playgroud)

(我使用"英特尔"从右到左的语法进行汇编指令,即mov从右操作数到左操作数的复制.我不知道哪个语法 - 从右到左或从左到右 - 你的编译器正在使用.)

另请注意,mov edi, dword ptr storagedword ptr部分是完全可选的,无论如何都没有区别.