Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > assembly in a C language program

Reply
Thread Tools

assembly in a C language program

 
 
candy_init@yahoo.com
Guest
Posts: n/a
 
      02-24-2005
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

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      02-24-2005


http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
 
 
 
infobahn
Guest
Posts: n/a
 
      02-24-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      02-24-2005
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assembly language program CK C Programming 2 07-18-2005 01:38 PM
Assembly's manifest definition does not match the assembly reference. Horatiu Margavan via .NET 247 ASP .Net 0 08-30-2004 04:14 PM
ASP.NET 2.0: What is the namespace and assembly name of generated assembly SA ASP .Net 0 08-09-2004 05:09 PM
Referencing assembly from GAC using @assembly fails Brent ASP .Net 1 01-23-2004 08:23 PM
can a strongly named assembly reference a regular assembly? Prasanna Padmanabhan ASP .Net 1 11-19-2003 06:21 AM



Advertisments