views:

219

answers:

1

I have the following code:

void * storage = malloc( 4 );

__asm
{
    //assume the interger 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?

+5  A: 

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.

AndreyT
my bad, I think you're right.I didn't have the time to check if the first answer worked.It reminded me that things like "dword" existed, so I assumed it would solve the issue.
Emil D