Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy content of C variable into a register (GCC)

Since I'm very new to GCC, I'm facing a problem in inline assembly code. The problem is that I'm not able to figure out how to copy the contents of a C variable (which is of type UINT32) into the register eax. I have tried the below code:

__asm__
(
    // If the LSB of src is a 0, use ~src.  Otherwise, use src.
    "mov     $src1, %eax;"
    "and     $1,%eax;"
    "dec     %eax;"
    "xor     $src2,%eax;"

    // Find the number of zeros before the most significant one.
    "mov     $0x3F,%ecx;"
    "bsr     %eax, %eax;"
    "cmove   %ecx, %eax;"
    "xor     $0x1F,%eax;"
);

However mov $src1, %eax; doesn't work.

Could someone suggest a solution to this?


1 Answers

I guess what you are looking for is extended assembly e.g.:

    int a=10, b;
    asm ("movl %1, %%eax;   /* eax = a */
          movl %%eax, %0;" /* b = eax */
         :"=r"(b)         /* output */
         :"r"(a)         /* input */
         :"%eax"        /* clobbered register */
         );        

In the example above, we made the value of b equal to that of a using assembly instructions and eax register:

int a = 10, b;
b = a;

Please see the inline comments.

note:

mov $4, %eax          // AT&T notation

mov eax, 4            // Intel notation

A good read about inline assembly in GCC environment.

like image 139
0x90 Avatar answered Oct 23 '25 15:10

0x90



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!