how can i read value from register using C++

不羁岁月 提交于 2019-12-13 15:31:42

问题


I am writhing code with C++ for a calculator ,but it display\read results with assembly,I want to store the value in any register for example( Al )to variable int in C++... I searched for away but I always find it with C language ...


回答1:


If you want to read value in al into an int:

GCC:

unsigned char out;
asm volatile("movb %%al, %[Var]" : [Var] "=r" (out));

Or

unsigned char out;
asm volatile("movb %%al, %0" : "=r" (out));

For MSVC:

unsigned char c;
__asm movb c, al

There's no official C++ way, it stems it from C.

EDIT

You might also want:

register unsigned char out asm("%al");

But that's GCC.




回答2:


It is compiler dependent. For Intel with GCC:

//Read value from register
int x;

asm ("mov %0, AI;"
     :"=r"(x)
    );

Reference here




回答3:


You mean like:

int read_register_eax()
{
    int ret;
    asm { mov [ret],eax }
    return ret;
}


来源:https://stackoverflow.com/questions/19895038/how-can-i-read-value-from-register-using-c

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