How to read registers: RAX, RBX, RCX, RDX, RSP. RBP, RSI, RDI in C or C ++?

Suppose I want to read values ​​from these registers (and almost all of this) on a dual-core x64 processor. How can i do this? Can I just 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"
);

and then just print raxand rbx? Greetings

+4
source share
1 answer

The correct way to do this with gcc is with registers:

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

Note that you do not need any actual instructions - the restrictions tell gcc that after doing nothing, the rax value will be in rax and the rbx value will be in rbx.

a, b, c, d, S d ( % rsi % rdi). Yz % xmm0. , .

+4

All Articles