Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   assembly in a C language program (http://www.velocityreviews.com/forums/t437069-assembly-in-a-c-language-program.html)

candy_init@yahoo.com 02-24-2005 12:10 PM

assembly in a C language program
 
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


Michael Mair 02-24-2005 12:20 PM

Re: assembly in a C language program
 


candy_init@yahoo.com 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.


infobahn 02-24-2005 01:23 PM

Re: assembly in a C language program
 
candy_init@yahoo.com 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.

jacob navia 02-24-2005 08:04 PM

Re: assembly in a C language program
 
candy_init@yahoo.com 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


All times are GMT. The time now is 08:22 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.