Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I store the value of a register into a memory location pointed to by a pointer?

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?

like image 588
Emil D Avatar asked Mar 02 '10 14:03

Emil D


1 Answers

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.

like image 111
AnT Avatar answered Oct 20 '22 09:10

AnT