Save CPU registers to variables in GCC

前端 未结 1 905
野趣味
野趣味 2021-01-07 00:50

I want get the values in EAX/EBX/ESP/EIP etc. and save them in C variables. For example:

int cEax;
asm(\"mov cEax,%eax\"); ...
相关标签:
1条回答
  • 2021-01-07 01:24

    You can use this

    register int eax asm("eax");
    register int eax asm("ebx");
    register int eax asm("esp");
    //...
    int cEax = eax;
    int cEbx = ebx;
    int cEsp = esp;
    //...
    

    You can also work with those registers in an expression just as any other variables or just use that register's value directly without assigning to another variable.

    It's more tricky to get eip without inline assembly but in gcc you can get it with __builtin_return_address or the label as values extension.

    void* getEIP()
    {
        return __builtin_return_address(0);
    }
    
    void *currentInstruction = getEIP();
    currentAddr: void *nextInstruction = &&currentAddr;
    

    If you want inline assembly you can use the way in this page

    0 讨论(0)
提交回复
热议问题