_asm or __asm?

This is a discussion on _asm or __asm? within the A Brief History of Cprogramming.com forums, part of the Community Boards category; in MSVC, it allows you to define assembly blocks with either _asm or __asm (one underscore as opposed to two). ...

  1. #1
    ...
    ... is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    465

    _asm or __asm?

    in MSVC, it allows you to define assembly blocks with either _asm or __asm (one underscore as opposed to two). i was wondering if there is any difference in these at all, and if so, what? the docs only provide information for the double underscore version.
    I came up with a cool phrase to put down here, but i forgot it...

  2. #2
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    I have only had the need to use asm a few times so I am no expert, but I read an article somewhere a while back that basically defined the difference as ....

    _asm - simply invokes the inline assembler
    __asm - is treated like an intrinsic function call


    EDIT: Brain was moving faster than fingers

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    inline assembly is compiler dependent, and most compilers have a few different ways of doing it, in order to support other compilers' ways of doing inline assembly.

    there is no ANSI standard on inline ASM code.

    the most common inline asm statements are:

    asm
    _asm
    __asm

    and i think GCC uses a function call that takes a string, but I have not been able to get it to work so far, so anyone who is able to get it to work, tell me:

    asm ( char * );
    My Website

    "Circular logic is good because it is."

  4. #4
    Still A Registered User DISGUISED's Avatar
    Join Date
    Aug 2001
    Posts
    499
    Very true, and the article I was referring to was written about MSVC, which is the compiler in question.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    and i think GCC uses a function call that takes a string, but I have not been able to get it to work so far, so anyone who is able to get it to work, tell me:

    asm ( char * );
    I was able to get inline asm to work in Dev-C++ (which uses gcc I believe), but it's really strange, and to use intel syntax you have to add -masm=intel to compiler commands...Which doesn't seem to work if I try to call gcc directly. But anyways here's something I got to compile with Dev-C++:
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    static int x __asm__("x") = 5;
    asm(
    "mov eax, x;"
    "shr eax,1;"
    "mov x, eax;"
    );
    
    std::cout<<x;
    }
    Useless, yes, but my assembly skills suck and I couldn't think of anything better...
    "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

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    There is no difference. There is no function call for asm. It simply inserts your asm code directly into the code stream. This is not always the best way.

    Based upon research by Fordy and myself, it is best to either use a separate assembly file for your assembly functions or view your entire code in assembly so that you can see exactly what the compiler is doing.

    In many cases inline assembly is much slower due to compiler issues - you must get along with the compiler in your asm code. Best bet is use a pure assembly object module and link it in - then include external references to the assembly functions.

    But having an intrinsic call would simply negate any benefits that assembly might have. Upon a call the compiler would have to build a stack frame using ENTER <bytes>,<lexical_level> and then clean up the stack using LEAVE. This would take more cycles than needed.

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Originally posted by DISGUISED
    EDIT: Brain was moving faster than fingers
    Don't let it happen again
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

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