« Kernel Debugging - OSX | Main | Short Circuit - No Disassemble! »

Short Circuit - No Disassemble (Final part)

Examples of extended inline assembly -

 

void mycfunc ( )

{

    int a =10, b = 0;

    asm ("movl %1, %%eax; \

             movl %%eax, %0;"

           :"=r" (b)      /*output constraint, first mention of a variable b so b is %0, = sign mean target var  */

           : "r" (a)       /*Input constraint, a var is now %1, no = sign means it is not output or target */

            : "%eax"   /* we clobbering eax register, so gcc will preserve eax's previous content  */

     )

}

-- Watch that we need to refer to registers using %%, instead of %.

-- If we use registers, we should make GCC aware about them, so it preserves the values before using them.

 

Compiler toolchains are smart these days, so depending on the analysis, GCC could delete the whole code section or a part or it can move code around for optimization. There may be situation where we want the compiler to stop doing those optimization, and place the code as is ( i.e. in place ). For that in systems code we almost always uses __volatile__ keyword, like the following example.

From <asm-i386/atomic.h> of Linux source -

statoc __inline__ void atomic_inc(atomic_t *v)

{

   __asm__ __volatile__ (

        LOCK "incl %0"

        :"=m" (v->counter)

        : "m" (v->counter)

   );

}

For the detail syntax, and semantics, please use the GNU compiler connection manual. It is available on line. 

 

Posted on Sunday, March 1, 2015 at 06:47AM by Registered CommenterProkash Sinha | CommentsPost a Comment | References2 References

References (2)

References allow you to track sources for this article, as well as articles that were written in response to this article.

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.