Inline Assembler Macros?

This is a discussion on Inline Assembler Macros? within the C++ Programming forums, part of the General Programming Boards category; Hi, all I am trying to write a high performance library by using inline assembler but I have encountered a ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Inline Assembler Macros?

    Hi, all

    I am trying to write a high performance library by using inline assembler but I have encountered a irritating problem. It seems that VC++ refuses to accept multi-instructions within the inline asm section for the macro. For example, I can write a inline assembler macro with the single instruction “mov eax, 0” but that’s it, VC++ refuses to accept anymore instructions. The following code might give a better understanding:

    Code:
    #define TestMacro1(x) _asm{ mov eax, 0 } // This works
    #define TestMacro2(x) _asm{ mov eax, 0; mov ebx, 0 } // This doesn’t
    So what I am trying to do here is to add more then one instruction into the macro, but I don’t know how. Any ideas on how to solve this are highly appreciated.

    Thanks in advance.
    Last edited by Aidman; 07-14-2003 at 09:13 PM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    hm i thought you didnt need the semi-colon for asm (?)
    anyways im sure a simple board search could find some examples
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    hm i thought you didnt need the semi-colon for asm (?)
    Yes the semi-colon is unnecessary, but I used it because the macro had to be in a single line.


    anyways im sure a simple board search could find some examples
    I searched the board before posting, but I didn't find any examples on inline assembler macros. I also tried google but I didn't find anything there either. If you could find any examples on inline assembler macro, I would be grateful
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    but did you try eliminating the semicolon all together?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I believe ';' s a comment in asm.

    Just break the statements onto new lines:
    Code:
    #define TestMacro2(x)  \
    _asm{ \
       mov eax, 0; \
       mov ebx, 0 \
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Right...of course I knew that....then inline asm has never worked for me...anyone else have this problem (asm doesn't work) with Dev C++?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I've had it work in the past. What problems are you having with it?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If I use _asm i get "_asm undeclared" with __asm or just asm (and then { //some asm } ) i get "parse error before '{' "
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    ˇAmo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Thanks golfingguy, but that didn't help me (tried everything there) and I have dev-c++, I assume it has something to do with the compiler
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    If you want to get some assemler into a #define, take a look at this - http://msdn.microsoft.com/library/de...s_c_macros.asp


    The point made about inline asm working against the compilers optimisations is a good one.......if you are totally sure that you can get that extra boost with assembler, then your best course of action is to build an asm procedure in MASM.....assemble it and link the resulting .obj file into your vc++ project

  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    but did you try eliminating the semicolon all together?
    Yes, but that also didn't work


    I believe ';' s a comment in asm.
    Yes, but not in inline asm for VC++. There you use the normal "//", "*/" and "/*".


    Code:
    #define TestMacro2(x)  \
    _asm{ \
       mov eax, 0; \
       mov ebx, 0 \
    }
    I can't seem to compile this in VC++, I get the following error "inline assembler syntax error in 'opcode'; found 'constant'".


    If you want to get some assemler into a #define, take a look at this - http://msdn.microsoft.com/library/d...as_c_macros.asp
    Thanks alot, that worked


    As for the optimizations issues, I still belive that hand optimized asm code is the best. And the reason for using macros and not a precompiled library is becouse I wanto gain the extra performance, by not calling procedures.

    Anyways thanks for all your replys
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  13. #13
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Thanks that also worked
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  14. #14
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Originally posted by Aidman
    As for the optimizations issues, I still belive that hand optimized asm code is the best. And the reason for using macros and not a precompiled library is becouse I wanto gain the extra performance, by not calling procedures.

    Anyways thanks for all your replys
    Youd be suprised at how good modern compilers are at optimising.....quite often you can put all your efforts into optimising and the compiler will still kick your butt....

    And as to the macro vs call dilemma, I dont agree with you......as stated, the appearence of inlined code will often stop the compiler optimising within a block (even if you forced it not to do this, you would probably have to play safe and save the states of the registers you use as well as the eflags register).......one problem often associated with a call is that the compiler builds a new stack frame for the procedure....if you are writing the procedure in asm, you might be able to forego this overhead - you still empty the prefetch queue with a call instruction, but if your asm code restrictes the amount of calls and jumps when you have entered it then it might not be much of a problem....

  15. #15
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    After the many replies on optimization, I decided to verify my own statement. I couldn’t just let go of the idea that “hand optimized assembler is the best” without a fight . So I wrote several performance tests on loops, calculations and memory readings. After many builds I realized the compiler was complete superior in optimization, sometimes up to 10 times. I must say, this is a real wake up call. I had heard that modern compilers where better at optimization, but I refused to believe that it was true, hopping that my assembler knowledge still was in handy. I am still a bit unhappy about the situation, I want my assembler knowledge to be a huge performance benefit, but it seems that high level programming is becoming complete superior. I would hate to see the day when VB or C# compilers would kick C++. Anyway thanks for pointing out my blinded mind, else I would have spent useful time writing code that was a complete performance disadvantage. Peace out
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

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. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  3. inline assembler as alternative to int86()
    By Bigbio2002 in forum C Programming
    Replies: 3
    Last Post: 11-12-2004, 03:57 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Watcom C++ inline assembler
    By r0x in forum C++ Programming
    Replies: 0
    Last Post: 06-03-2002, 11:21 AM

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