Search code examples
c++visual-c++inline-assembly

(C/C++) Inline Assembly "add dword ptr [address]" increments the address instead of it's value


I'm trying to increment the value at an address stored by a DWORD iNumAddr using Inline Assembly and I've noticed that it increments the address instead of the value it contains. Eg.

->iNumAddr = 57D03390

->addstuff() runs..

->iNumAddr = 57D033C2

The address is correct, I've tested it.

void addstuff()
{
    _asm {
        add dword ptr [iNumAddr], 50
    }
}

Solution

  • If you declare a variable DWORD iNumAddr in a high-level language such as C++, then the symbol iNumAddr in assembly-language code takes the value of the address of the high-level language variable iNumAddr. (This address is usually assigned by the linker.)

    So the instruction add dword ptr [iNumAddr], 50 will increment the variable iNumAddr, not the value that iNumAddr points to.

    It takes two instructions to do what you want. For example:

    mov ebx,[iNumAddr]
    add dword ptr [ebx],50