assembly in a C language program

Discussion in 'C Programming' started by candy_init@yahoo.com, Feb 24, 2005.

  1. Guest

    hi
    I recently came across a C program, a snippet of which is as follows:

    unsigned long get_sp(void)
    {
    __asm__("movl %esp,%eax");
    }

    int main(int argc,char **argv)
    {

    unsigned long sp;
    sp=get_sp();
    ........
    }

    Is __asm__(...) a standard routine for calling assembly language
    instructions in a C language program?

    Thanks
    , Feb 24, 2005
    #1
    1. Advertising

  2. Michael Mair Guest

    wrote:
    > hi
    > I recently came across a C program, a snippet of which is as follows:
    >
    > unsigned long get_sp(void)
    > {
    > __asm__("movl %esp,%eax");
    > }
    >
    > int main(int argc,char **argv)
    > {
    >
    > unsigned long sp;
    > sp=get_sp();
    > ........
    > }
    >
    > Is __asm__(...) a standard routine for calling assembly language
    > instructions in a C language program?


    No, not in standard C.
    As __asm__ starts with leading underscores, it is in the
    implementation's namespace, i.e. it is an identifier you should
    not use. The implementation may do whatever non-standard things
    it needs in order to get its job done (that is, compiling your
    standard compliant code into an executable) and sometimes it
    lets you access these things, too.

    The C99 standard mentions the use of an "asm" keyword as common
    extension, so you also might encounter
    asm(....)
    instead of the above -- or anything else.

    All that is not portable and not standard C.


    Cheers
    Michae
    --
    E-Mail: Mine is a gmx dot de address.
    Michael Mair, Feb 24, 2005
    #2
    1. Advertising

  3. infobahn Guest

    wrote:
    >
    > Is __asm__(...) a standard routine for calling assembly language
    > instructions in a C language program?


    No.

    C offers no support for inline assembly language, but it permits
    implementations to offer such support as an extension. There is
    no universally recognised agreement amongst implementors about
    how, or indeed whether, to provide such an extension.
    infobahn, Feb 24, 2005
    #3
  4. jacob navia Guest

    wrote:
    > hi
    > I recently came across a C program, a snippet of which is as follows:
    >
    > unsigned long get_sp(void)
    > {
    > __asm__("movl %esp,%eax");
    > }
    >
    > int main(int argc,char **argv)
    > {
    >
    > unsigned long sp;
    > sp=get_sp();
    > ........
    > }
    >
    > Is __asm__(...) a standard routine for calling assembly language
    > instructions in a C language program?
    >
    > Thanks
    >


    This small assembly snippet takes the current value of
    the stack pointer (%esp) and moves it to the eax register.

    Since the eax register holds the result of all functions,
    this returns the value of the stack pointer as an unsigned
    long.

    This way of in-line assembly is commonly used in many
    compilers, and allows you to do things that can't be done
    in the C language. There is no standard for it but:
    _asm(literal string);
    __asm__(literal string);
    asm(literal string);
    are commonly used.

    Microsoft compilers uses also
    _asm {
    assembly statements
    }

    This extension is one of the common extensions mentioned
    in the C standard.

    jacob
    jacob navia, Feb 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prasanna Padmanabhan
    Replies:
    1
    Views:
    803
    Mattias Sjögren
    Nov 19, 2003
  2. Brent
    Replies:
    1
    Views:
    1,350
    Brent
    Jan 23, 2004
  3. SA
    Replies:
    0
    Views:
    463
  4. Horatiu Margavan via .NET 247

    Assembly's manifest definition does not match the assembly reference.

    Horatiu Margavan via .NET 247, Aug 30, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,595
    Horatiu Margavan via .NET 247
    Aug 30, 2004
  5. CK

    assembly language program

    CK, Jul 18, 2005, in forum: C Programming
    Replies:
    2
    Views:
    317
    Kenny McCormack
    Jul 18, 2005
Loading...

Share This Page