Thread: Inline asm with mingw

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    Inline asm with mingw

    Im trying to use inline asm with mingw and intel syntax.

    I get "undefined reference to x and y" which means its coming from the asm
    function meaning it cant see the values x,y passed to Add().

    Code:
    #include <windows.h>
    #include <iostream.h>
    using namespace std;
    
    int Add(int x, int y)
    {
        asm
        (
         ".intel_syntax noprefix\n\t"
         "mov eax,x\n\t"
         "mov ebx,y\n\t"
         "add eax,ebx\n\t"
         "mov x,eax\n\t"
         ".att_syntax\n"
        );
        return x;
    }
    
    int main ()
    {
        int x,y;
    
        cout<<"Enter first number\n";
        cin>>x; //enter first number
        cout<<"Enter second number\n";
        cin>>y; //enter second number
    
        cout<<Add(x,y)<<endl;
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I was going to say that's inline ASM like I've never seen it (strings), but after Googling it, apparently that's quite common. The only difference I can see is the asm or __asm__ function operates like printf. Which means it's not going to recognize your variables like PHP or Perl, you have to string replace, which the function provides.

    CodeProject: Using Inline Assembly in C/C++. Free source code and programming help

    int no = 100, val ;
    asm ("movl %1, %%ebx;"
    "movl %%ebx, %0;"
    : "=r" ( val ) /* output */
    : "r" ( no ) /* input */
    : "%ebx" /* clobbered register */
    );
    So you'd have to do %1 and %2 for x and y, passing multiple arguments to the function for replacement. __asm__() seems to be the proper way, versus just asm().

    I'm used to this form where you don't have to do that:

    Inline Assembler Overview (C++)
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    The .intel_syntax directive doesn't let gas use the exact same format as with an intel-compatible assembler. Registers will still need to be prefixed with a '%', and variables are referenced with placeholders. I know many will disagree with me, but if you're using gcc, then it's just better to use AT&T's assembler syntax. Your code will be considerably smaller:

    Code:
    #include <iostream>
    
    int add(int x, int y) {
       asm
          (
          "addl %1, %0"
          : "=r"(x)
          : "m"(y), "0"(x)
          );
       return x;
    }
    
    int main()
    {
       std::cout << add(10, 20) << std::endl;
    }

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    Thats great! Thank you very much Dae and Ronix!

    Yes i think you're right Ronix, seeing that
    ".intel_syntax directive doesn't let gas use the exact same format as with an intel-compatible
    assembler", it becomes too complicated, too mixed so its just better to use AT&T's assembler syntax.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  3. Inline asm
    By wavering in forum C Programming
    Replies: 2
    Last Post: 01-29-2002, 02:42 AM
  4. Inline asm - I love it!
    By wavering in forum C Programming
    Replies: 2
    Last Post: 01-08-2002, 02:19 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM