C ++ ASM Inline how to use boolean?

Say I have something like this.

bool isPatched; 

I have another GUI 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 } } 

Can I use the bool operator in an inline assembly?

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

+4
source share
1 answer

Do you want 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 problem is one of the scope, isPatched not the scope when using ASM, so it is assumed that it is a DWORD , and then cannot find the memory label (symbol name) for it when generating addresses. You also need to use the correct operand size for bool .

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 pin is 1 when b is true , or 0 when b is false .

+4
source

Source: https://habr.com/ru/post/1392909/


All Articles