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?
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.
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
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.)
Note also that in mov edi, dword ptr storage
the dword ptr
part is completely optional and makes no difference whatsoever.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With