Thread: gcc asm code syntax

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    Thumbs up gcc asm code syntax

    Hi,
    Is there any way to make gcc accept any other assembler syntax than the AT&T
    syntax with the C asm macro?
    Code:
     asm void foo(){}
    Or C asm string statement?
    Code:
     asm("asm-string")
    I would like to compile asm code with this syntax:
    Code:
     move.l  #0,D0
    Wich is code for a motorola microprocessor. Gcc does'nt seem to accept any
    asm with this syntax and as far as I have understood this is because gcc uses
    AT&T asm syntax. The problem is that I have a lot of code written in this asm style
    wich was compiled with the diab compiler but now I attempt to switch to gcc...

    Any tips is helpfull

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well that's the price you pay for mixing C and assembler. Change the compiler, and you're hosed.

    Besides, even if gcc accepted the syntax, the way gcc inline asm accesses local variables is also different.

    The first question is why you have so much assembly code?
    If it was to address some performance problem, when did you last assess the performance, and can you now get away with having more of the asm code written in C.

    The performance profile is going to change anyway by changing the compiler, so I'd suggest you start with a fresh baseline of all the asm code written as 'C', then figure out what really needs to be converted back to asm. In particular, gcc has a vast array of optimisation flags to play with. With any luck, you'll save yourself a lot of work in not maintaining assembler code.

    If there is some essential asm - like messing with certain processor registers which are physically inaccessible to C, then wrap them up in very small C functions which perform only the necessary asm instructions. Collect all such functions into one or two source files. This limits the scope of your future re-work, say changing processors next time.

    AFAIK, there is only flexible asm notation for Intel x86, presumably because there were enough people to whine about it to get something done about it.
    Code:
    #include<stdio.h>       
    #include<stdlib.h>
    int foo ( void ) {
        int result = 0;
    #ifdef USE_INTEL
        asm (".intel_syntax noprefix\n\t"
             "mov   %%eax,0x12345678\n\t"
             "mov   %0,%%eax\n"
            :"=r"(result)
            :
            :"%eax"
        );
    #else
        asm ("movl  $0x12345678,%%eax\n\t"
             "movl  %%eax,%0\n"
            :"=r"(result)
            :
            :"%eax"
        );
    #endif
        return result;
    }
    int main ( ) {
        printf("%x\n",foo());
        return 0;
    }
    
    $ gcc foo.c && ./a.exe
    12345678
    $ gcc -masm=intel -DUSE_INTEL foo.c && ./a.exe
    12345678
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Ok, thanks for the tips. It seems there is more work to it then I thought to change compiler...

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Ok, thanks for the tips. It seems there is more work to it then I thought to change compiler...
    You plan to find a compiler that supports that syntax of asm? Read Salem's post again...

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    You plan to find a compiler that supports that syntax of asm? Read Salem's post again...
    No, what I ment is that if I'm going to change the compiler there is a lot of asm-code wich need to be rewritten.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Machine code, asm, poop
    By tim545666 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 04-02-2002, 01:27 AM