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

时间:2020-12-17 21:18:17

I have the following code:

我有以下代码:

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);

However, in the code, when I dereference the storage pointer ( as in *(int *)storage ), I do not get 1. So, what is the proper way of storing the value of a register into the memory pointed to by a C++ pointer?

但是,在代码中,当我取消引用存储指针(如在*(int *)存储中)时,不会得到1。那么,如何将寄存器的值存储到c++指针指向的内存中呢?

1 个解决方案

#1


5  

Are you sure you know what you really need? You requested the code that would store the register value into the memory allocated by malloc ("pointed to by a pointer"), i.e. *(int*) storage location, yet you accepted the answer that stores (or at least attempts to store) the value into the pointer itself, which is a completely different thing.

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

To store eax into the memory "pointed to by a pointer", i.e. into *(int*) storage as you requested, you'd have to do something like that

要将eax存储到“通过指针指向”的内存中,即按照您的要求存储到*(int*)存储中,您必须做一些类似的事情

mov  edi, dword ptr storage
mov  dword ptr [edi], eax

(I use the "Intel" right-to-left syntax for assembly instructions, i.e. mov copies from right operand to left operand. I don't know which syntax - right-to-left or left-to-right - your compiler is using.)

(我使用“Intel”从右到左的语法,用于装配指令,即从右操作数到左操作数的mov拷贝。我不知道你的编译器用的是哪一种语法——从右到左还是从左到右。

Note also that in mov edi, dword ptr storage the dword ptr part is completely optional and makes no difference whatsoever.

还要注意,在mov edi中,dword ptr存储的dword ptr部分是完全可选的,没有任何区别。

#1


5  

Are you sure you know what you really need? You requested the code that would store the register value into the memory allocated by malloc ("pointed to by a pointer"), i.e. *(int*) storage location, yet you accepted the answer that stores (or at least attempts to store) the value into the pointer itself, which is a completely different thing.

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

To store eax into the memory "pointed to by a pointer", i.e. into *(int*) storage as you requested, you'd have to do something like that

要将eax存储到“通过指针指向”的内存中,即按照您的要求存储到*(int*)存储中,您必须做一些类似的事情

mov  edi, dword ptr storage
mov  dword ptr [edi], eax

(I use the "Intel" right-to-left syntax for assembly instructions, i.e. mov copies from right operand to left operand. I don't know which syntax - right-to-left or left-to-right - your compiler is using.)

(我使用“Intel”从右到左的语法,用于装配指令,即从右操作数到左操作数的mov拷贝。我不知道你的编译器用的是哪一种语法——从右到左还是从左到右。

Note also that in mov edi, dword ptr storage the dword ptr part is completely optional and makes no difference whatsoever.

还要注意,在mov edi中,dword ptr存储的dword ptr部分是完全可选的,没有任何区别。