Inline assembly and compiler optimizations?

This is a discussion on Inline assembly and compiler optimizations? within the C Programming forums, part of the General Programming Boards category; I read several times that using inline assembly is considered as a bad practice, since it will interfere with the ...

  1. #1
    TmX
    TmX is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    12

    Inline assembly and compiler optimizations?

    I read several times that using inline assembly is considered as a bad practice, since it will interfere with the compiler optimization.

    And recently, I read this:
    Q: How can I reference C variables from my inline assembly code?

    A: GCC has extensive inline assembly facilities. They allow you to specify everything other compilers let you (like the registers where GCC will put specific results), but in a way that doesn't interfere with the compiler's optimizations of the C code that includes inline assembly. Because of this flexibility, the syntax of the inline assembly code is very different from the other DOS-based compilers.

    DJGPP FAQ -- Inline Assembly code with GCC
    How it's possible?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Google is really cool. I entered "gcc inline assembly" and got this link as the first result.

    Come on, search FIRST then ask questions.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The compiler takes your specific requests for optimizations, "under advisement".

    Then it does it the way it see's as best, regardless of your requests.

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Quote Originally Posted by Kennedy View Post
    Google is really cool. I entered "gcc inline assembly" and got this link as the first result.

    Come on, search FIRST then ask questions.
    Perhaps you should have read his question first and then suggested a site?

    Here's a better page, at gcc.gnu.org:
    Extended Asm

    EDIT:
    I'm pretty sure this is the one referred to when the DJGPP site tells you to type

    info gcc "C Extensions" "Extended Asm"
    END EDIT

    It's a long page, but a quick grep for "optimization" yielded a few bits of information: GCC assumes that your function has no side effects other than changing the output operands... unless, of course, you properly use the volatile keyword. Also, the modified syntax that you mentioned includes instructions that get arguments by name rather than register, that way GCC can leave arguments out of order and wherever it wants (One reason why you shouldn't pass expressions with side-effects as arguments) if that means less work.

    Beyond optimizing how memory is managed before and after the call, there's not much a compiler can do. The rest falls into the assembler's hands.
    Last edited by bernt; 03-03-2010 at 02:51 PM.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    gcc lets you specify which registers you use as inputs, which ones are used as outputs, and which ones are destroyed. It also lets you specify "a register" as opposed to giving a specific register, which gives the compiler a chance to choose a good one that doesn't interfere with its own optimizations. You can specify that a register must come from a certain register class (for instance, index registers vs. GP registers).

    Inline assembly in gcc, when done correctly, does not interfere with optimization. No other compiler is quite as flexible, that I know of.

    EDIT: However, the purpose of inline assembly isn't optimization. If you are optimizing by coding in assembly, you are writing entire functions in assembly, not just little pieces. Inline assembly is really there to allow you to make quick calls out to architecture-specific instructions.
    Last edited by brewbuck; 03-03-2010 at 03:05 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by bernt View Post
    Perhaps you should have read his question first and then suggested a site?
    You may have noted that the question "how it's possible" is QUITE ambiguous. I took the reference of "How is this possible to access a C Variable in an asm() call. The link provided gives that info.

    Now, the real deal is that the OP should not be so vague in the questions asked.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The usage of inline assembly in C source code is discouraged due to the associated side effects and compiler/assembler interference. The asm statements are translated only by the assembler while the C source is translated only by the compiler and it is extremely hard to weed out their mutual interference. For example, the compiler may optimize certain variables and store them in registers for speeding things up, while the asm statements may clear up those very registers causing the program to behave weirdly and even crash. Traditionally inline assembly was used so that the programmer could access hardware features inaccessible to C, but nowadays most compilers provide intrinsics for that very purpose. Intrinsics is the preferred way to produce assembly language code because intrinsics are function like APIs that are only for consumption by the compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help using inline assembly for keyboard input
    By sleventeen in forum C Programming
    Replies: 7
    Last Post: 05-10-2009, 01:31 AM
  2. The real stack __asm{push, pop}
    By audinue in forum C Programming
    Replies: 15
    Last Post: 07-23-2008, 01:36 PM
  3. Compiler Design... Anyone That Can Help!
    By ComputerNerd888 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2003, 09:48 AM
  4. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 02:35 AM
  5. Replies: 5
    Last Post: 09-17-2001, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21