Converting C++ inline ASM from GCC to VS

This is a discussion on Converting C++ inline ASM from GCC to VS within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm attempting to convert an opensource project from GCC to Visual Studio Express C++. I have almost everything compiling, ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Converting C++ inline ASM from GCC to VS

    Hi,
    I'm attempting to convert an opensource project from GCC to Visual Studio Express C++. I have almost everything compiling, except the inline asm code. I am a beginner in C++, so some of my questions may be "duh!". I have spent time Googling and reading tutorials, and still have not solved all the compile errors.

    In GCC C++ inline ASM, the code is contained within

    Code:
    asm(" ");
    In VS C++ it is

    Code:
    __asm{ }
    Then there is the issue of line returns '\n' being required for GCC but apparently not for VS...

    So... when using a header '#define' statment that incorporates an asm statement, how should it be converted?

    Original:
    Code:
    #define GOrgueMemset(where, what, howmany) asm("\n\tcld\n\trep\n\tstosl\n" : : "D" (where), "a" (what), "c" (howmany))
    Converted?
    Code:
    #define GOrgueMemset(where, what, howmany) __asm{ tcld trep tstosl : : "D" (where), "a" (what), "c" (howmany)}
    But then there is this error:
    error C2400: inline assembler syntax error in 'opcode'; found 'trep'
    So something in there is not right.

    Next issue, another #define from the header, this later gets called from another __asm statement - and I have no clue what to remove.
    Code:
    #define ASM_DECODE_SU(z) "                    \n\
        movq (%0,%1),%%mm3                         \n\
        addl $8,%1                                             \n\
        movq %%mm3,%%mm1                         \n\
        psubsw %%mm2,%%mm3                     \n\
        movq %%mm1,%%mm2                         \n\
    "
    Any comments, help, links to read, etc. welcome.

    GrahamG
    Johannesburg, South Africa
    Last edited by ggoodesa; 01-23-2010 at 06:54 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Well you also need to replace all the \t with a tab (or a space)

    > tcld trep tstosl
    Should be
    cld rep stosl

    > GOrgueMemset
    I have no idea what this is going to achieve over using the normal memset.
    Other Builtins - Using the GNU Compiler Collection (GCC)
    Given that gcc will inline memset() to begin with, this just seems bone-headed to me.
    Start with
    Code:
    #define GOrgueMemset(where, what, howmany)   memset(where,what,howmany)
    The first step would be to figure out the actual C++ code that corresponds to the assembler, then actually try it to see if it works.

    Because going on the memset horror, the author has used assembler "because they could" rather than for any good reason.

    One thing is for sure, if there is a lot of use of the : syntax in the GCC assembler, then this will be hard to translate literally into VS syntax.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Hi Salem,

    The original developer of this project is long gone, so I'm dealing with old code that we're trying to update. I'm a beginner programmer, and haven't touched asm, so much of this is black magic at the moment.

    Thanks for the input on the memset statement, I'll update the code with your suggestions.

    Do you know of any resources that might be helpful in getting me better aquainted with asm in VS?

    Thanks again,
    GrahamG

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    I don't think there is a lot more than what you know already about the asm keyword.
    All it tells the compiler to do is copy/paste the text into the output, and hope the assembler can understand it.

    The rest is really figuring out what the instructions are all about, and for that you need an op-code list.

    This seems like a nice list (to a point)
    The Intel 8086 / 8088/ 80186 / 80286 / 80386 / 80486 Instruction Set
    but I see that your second snippet seems to use some MMX/SSE instructions (or later...)

    Can you post the URL of the project, maybe others here can take a stab at explaining a few bits for you along the way.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    3
    Thanks for the link, I'll start reading it through...

    The opensource project is a sample based virtual pipe organ program that was initially released as 'MyOrgan'. The project is now called 'GrandOrgue' and can be found at GrandOrgue | Get GrandOrgue at SourceForge.net

    The asm is in MMXSound.h and GOrgueSoundCallback.h

    The code available via svn is GCC Codeblocks based, as the VS 2008 C++ Express project that I'm creating has not been added (as it doesn't compile yet). If any one wants the minor changes that I have made so far I can create a branch and upload.

    Thanks for your interest.
    GrahamG

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. gcc inline asm: illegal instruction (core dump)
    By Sargnagel in forum C Programming
    Replies: 4
    Last Post: 10-28-2003, 12:41 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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