如何阅读寄存器:RAX,RBX,RCX,RDX,RSP。 RBP,RSI,RDI在C或C ++? [英] How to read registers: RAX, RBX, RCX, RDX, RSP. RBP, RSI, RDI in C or C++?

查看:3932
本文介绍了如何阅读寄存器:RAX,RBX,RCX,RDX,RSP。 RBP,RSI,RDI在C或C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说,我想从这些寄存器(和pretty全部完蛋了)双核x64 CPU读取值。我怎样才能做到这一点?我可以简单的写是这样的:

Lets say I want to read values from those registers (and pretty all thats it) on dual core x64 CPU. How can I do this? Can I simply write something like:

uint64_t rax = 0, rbx = 0;
__asm__ __volatile__ (
    /* read value from rbx into rbx */
    "movq %%rdx, %0;\n"
    /* read value from rax into rax*/
    "movq %%rax, %1;\n"
    /* output args */
    : "=r" (rbx), "=r" (rax)
    : /* no input */
    /* clear both rdx and rax */
    : "%rdx", "%rax"
);

然后只需打印出 RAX RBX ?干杯

推荐答案

用gcc来做到这一点,正确的方法是用寄存器约束上:

The right way to do this with gcc is with register contraints:

uint64_t rax = 0, rbx = 0;
__asm__("" : "=a"(rax), "=b"(rbx) ::); /* make rax and rbx take on the current values in those registers */

请注意,你不需要任何实际的指示 - 约束告诉GCC下,什么​​都不做,该值RAX将在RAX和RBX的价值将在RBX

Note that you don't need any actual instructions -- the constraints tell gcc that after doing nothing, the value rax will be in rax and the value of rbx will be in rbx.

您可以使用约束 A B C D 取值 D (中后两者是RSI%和%RDI)。您还可以使用 YZ 为%XMM0。不幸的是,似乎没有对其他特定寄存器约束

You can use the constraints a, b, c, d, S, and D (the latter two are for %rsi and %rdi). You can also use Yz for %xmm0. Unfortunately, there don't seem to be constraints for other specific registers.

这篇关于如何阅读寄存器:RAX,RBX,RCX,RDX,RSP。 RBP,RSI,RDI在C或C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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