简体   繁体   中英

C++ ASM Inline how to use struct members in ASM?

I have the following

struct john {
    int oldA;
    int A;
} myJohn;
DWORD gotoAddressBack = 0x00401000;

void __declspec( naked ) test(void) {
    __asm {
        MOV myJohn.oldA, DWORD PTR DS:[ESI+0x77C]
        MOV DWORD PTR DS:[ESI+0x77C], myJohn.A
        JMP gotoAddressBack
    }
}

You can tell that both MOV's generate the error C2415: improper operand type.

As you can see what I want to do is store [ESI+0x77C]'s value into myJohn.oldA

Then I want to replace the same [ESI+0x77C]'s value with myJohn.A

There is no memory/memory operand for MOV instruction. You should use a register for such usages. This is something like that:

void __declspec( naked ) test(void) {
    __asm {
        MOV EAX, DWORD PTR [ESI+0x77C]
        MOV myJohn.oldA, EAX

        MOV EAX, myJohn.A
        MOV DWORD PTR [ESI+0x77C], EAX

        JMP gotoAddressBack
    }
}

BTW, I really suspect that you really have to deal with segment registers under modern OSes (due to virtual memory, ie you can use direct addresses). You should check your code after above changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM