将CPU寄存器保存到GCC中的变量 [英] Save CPU registers to variables in GCC

查看:109
本文介绍了将CPU寄存器保存到GCC中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取EAX/EBX/ESP/EIP等中的值并将其保存在C变量中.例如:

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"); ...

推荐答案

您可以使用此

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.

在没有内联汇编的情况下获得eip更为棘手,但是在gcc中,您可以使用 __builtin_return_address 或<一个href ="https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html" rel ="nofollow noreferrer"> label as values 扩展名.

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

这篇关于将CPU寄存器保存到GCC中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆