Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing an array with inline assembly using AT&T syntax

I want to assign an array using inline assembly using the AT&T syntax. I want to achieve something like the following. Note that rsp here is the %rsp register.

long saved_sp[N];
long new_sp[N];

void some_function( unsigned int tid, ... )
{
 // These two lines should be in assembly
 saved_sp[tid] = rsp; 
 rsp = new_sp[tid];   
 ......
}
like image 583
MetallicPriest Avatar asked Jul 25 '26 03:07

MetallicPriest


1 Answers

I'm sure I don't need to warn you...

__asm__ __volatile__ (

    "movq %%rsp, (%0, %2, 8)\n\t"
    "movq (%1, %2, 8), %%rsp\n\t"

    : : "r" (saved_sp), "r" (new_sp), "r" ((long) tid));

Perhaps "memory" should be added as a clobber, but it seems kind of redundant. Wherever you go after this, remember that the frame pointer "%rbp" will be invalidated.

like image 86
Brett Hale Avatar answered Jul 27 '26 19:07

Brett Hale



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!