0
int my_var;
void __declspec(naked) stuff()
{
    __asm
    {
        lea edx, [ecx + edi + 0x0000111]
    }
}

How to store the value from the address [ecx + edi + 0x0000111] in the c++ variable "my_var" above.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Answers [like this](https://stackoverflow.com/questions/15908835/how-to-store-a-c-variable-in-a-register) might help. – tadman Feb 21 '21 at 03:38
  • I'd probably look at the MS docs, like [here](https://learn.microsoft.com/en-us/cpp/assembler/inline/accessing-c-or-cpp-data-in-asm-blocks?view=msvc-160). – David Wohlferd Feb 21 '21 at 04:23
  • @tadman: That's the opposite problem, and not specific to MSVC's flavour of inline asm (which makes it trivial even for local vars that don't have a symbol in the object file or in stand-alone asm). – Peter Cordes Feb 21 '21 at 05:11
  • @PeterCordes That is handy. – tadman Feb 22 '21 at 02:40

1 Answers1

2

From the Microsoft docs:

An __asm block can refer to any symbols, including variable names, that are in scope where the block appears.

Therefore you could do this:

int my_var;
void __declspec(naked) stuff()
{
    __asm
    {
        lea edx, [ecx + edi + 0x0000111]
        mov my_var, edx
        ret

    }
}
kdcode
  • 524
  • 2
  • 7
  • Your answer should probably include the missing `ret` instruction this `naked` function needs to be callable without crashing. – Peter Cordes Feb 21 '21 at 05:13
  • @PeterCordes Did not noticed it. Thanks. – kdcode Feb 21 '21 at 05:15
  • I tried that before and that gave me the address instead of WHAT is in the address, so the actual value. – Emre Köycü Feb 21 '21 at 18:04
  • @EmreKöycü I assumed you wanted to have the "value" of the register. It might be address of something. To get the value you want, just define my_var as pointer of a suitable type then dereference it after the _asm block (remember to remove ret instruction) . – kdcode Feb 21 '21 at 18:38