Reading the value of a variable register with a single asm command

How can I read the register value for a variable with a single built-in assembler command? I am using gcc on the old freeBSD system (v2.1 i386).

I have a code like this:

static volatile unsigned long r_eax, r_ebx; asm ("movl %%eax, %0\n" :"=r"(r_eax)); asm ("movl %%ebx, %0\n" :"=r"(r_ebx)); 

As a result, I get the following:

 mov %eax,%eax mov %eax,0x1944b8 mov 0x1944b8,%eax mov %ebx,%eax mov %eax,0x1944bc mov 0x1944bc,%eax 

But I just need to:

 mov %eax,0x1944b8 mov %ebx,0x1944bc 

How can I achieve this result?

+4
source share
2 answers

This does it for me (as long as r_eax / r_ebx are static)

 asm ("movl %%eax, %0\n" "movl %%ebx, %1\n" : "=m"(r_eax), "=m"(r_ebx)); 

Beware that if you do not specify assembly language instructions in the same bracket asm() , the compiler may decide to make all kinds of “interesting optimizations” between them, including modifications to these regs.

+3
source

Note that you are using restrictions instructing gcc to put the result in a register. Thus, it cannot directly put it into memory. Since you want to store values ​​only from registers, you don’t even need any instructions, just restrictions, for example:

 __asm__ __volatile__ ("" : "=a" (r_eax), "=b" (r_ebx)); 
+2
source

Source: https://habr.com/ru/post/1340302/


All Articles