Save CPU registers to variables in GCC

允我心安 提交于 2019-12-01 08:05:53

问题


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:


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



来源:https://stackoverflow.com/questions/24592879/save-cpu-registers-to-variables-in-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!