How to set a variable in GCC with Intel syntax inline assembly?

后端 未结 3 1562
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 09:25

Why doesn\'t this code set temp to 1? How do I actually do that?

int temp;
__asm__(
    \".intel_syntax;\"
    \"mov %0, eax;\"
    \"mov eax, %         


        
3条回答
  •  难免孤独
    2020-11-30 09:27

    You have to pass an argument to GCC assembler.

    gcc.exe -masm=intel -c Main.c
    gcc.exe Main.o -oMain.exe
    

    And you have C code like this:

    #include 
    #include 
    
    int myVar = 0;
    
    int main(int argc, char *argv[])
    {
        asm("mov eax, dword ptr fs:[0x18]");
        asm("mov eax, dword ptr ds:[eax+0x30]");
        asm("movzx eax, byte ptr ds:[eax+0x2]");
        asm("mov _myVar, eax");
    
        if(myVar == 1) printf("This program has been debugged.\r\n");
        printf("Welcome.\r\n");
        getch();
    
        return 0;
    }
    

    Don't forget to add prefix underscore (_) for every variables in asm() keyword, or it won't recognize it.

    And keyword asm() use prefix '0x' for every hexadecimal integer, not suffix 'h'.

提交回复
热议问题