简体   繁体   中英

C++ ASM Inline how to use boolean?

Say I got something like this..

bool isPatched;

I have a few other GUI's where I set isPatched= true; and isPatched= false; , isPatched = !isPatched;

void __declspec( naked ) test(void) { //
    __asm {
        PUSHAD
        PUSHFD

        MOV EAX, isPatched
        CMP EAX, 0
        je noPatched
            MOV EAX, DWORD PTR DS:[ESI+0x77C]
            MOV John.oldA, EAX
            MOV EAX, John.A
            MOV DWORD PTR DS:[ESI+0x77C], EAX
            JMP finish
noPatched:
            PUSH EDX
            MOV DWORD PTR DS:[ESI+0x77C], EDX
        finish:
        POPFD
        POPAD

        JMP gotoAddressBack

    }
}

Is it possible to use bool operator in inline assembly?

I think it thinks isPatched is a label.. from this error message. error C2094: label 'isPatched' was undefined

You want to TEST or CMP . TEST is the easiest in this case:

XOR EAX,EAX
MOV AL,isPatched //isPatched would be a byte, hence we need correct operand sizes
TEST EAX,EAX
JE NotSet
Set:
//handle true case
JMP End
NotSet:
//handle false case
End:
//continue

Depending on other cases you can also use SUB , SETcc or MOVcc


Your issue is one of scoping, isPatched is not in scope when used by the ASM, so it assumes it to be a DWORD , and then fails to find a memory label (the symbol name) for it when generating the addresses. You also need to use the correct operand size for bool as well.

A dirty litte test for MSVC

bool b = true;
int __declspec( naked ) test(void) {
    __asm {
        xor eax,eax
        MOV al, b
        TEST eax,eax
        JE NotSet
        mov eax,1
NotSet:
        RETN

    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("%d\n", test());
    system("pause");
    return 0;
}

this outputs 1 when b is true , or 0 when b is false .

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