Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Using assembly in C

Reply
Thread Tools

Using assembly in C

 
 
alice
Guest
Posts: n/a
 
      01-11-2005
hi all,
Can anybody please tell me that how an assembly language code can be
called from a program written in C?I mean what is the syntax for doing
this thing?

Thanks,
Alice

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      01-11-2005
On Tue, 11 Jan 2005 12:05:43 -0800, alice wrote:

> hi all,
> Can anybody please tell me that how an assembly language code can be
> called from a program written in C?I mean what is the syntax for doing
> this thing?
>
> Thanks,
> Alice


Although acknowleged in the standard as a common extension, such
functionality is not standardized and is implementation specific. You
should check your compiler's documentation for details (look for an asm
or __asm__ keyword).

Rob Gamble
 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      01-11-2005
alice wrote:
> hi all,
> Can anybody please tell me that how an assembly language code can be
> called from a program written in C?I mean what is the syntax for doing
> this thing?
>
> Thanks,
> Alice
>


Look in your compiler documentation for the protocol for
calling a function and returning, especially how parameters
are handled.

A fast method is to write a simple function and have your
compiler spit out the assembly language for the function.
Use this as a stencil for writing your own assembly language
function.

If your assembly language obeys the function calling
conventions, then calling your assembly language from C
should be no different than calling a C language function.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      01-11-2005
alice wrote:

> Can anybody please tell me that how
> an assembly language code can be called from a program written in C?
> I mean what is the syntax for doing this thing?


Once compiled, a C program can't tell whether it is calling
a function written in assembler, C or any other programming language.
You can consult the C Application Binary Interface (ABI)
for your platform but it would probably be much easier
to inspect the assembler output from your C compiler.
For example

> cat f.c

double f(double x) {
return x*x;
}

> gcc -Wall -std=c99 -pedantic -S f.c
> cat f.s

.file "f.c"
.text
.globl f
.type f, @function
f:
pushl %ebp // save frame pointer
movl %esp, %ebp
fldl 8(%ebp) // st0 <-- x
fmull 8(%ebp) // st0 <-- st0*x
popl %ebp // restore frame pointer
ret
.size f, .-f
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.1"

The calling program pushes argument x onto the stack and calls f.
The call instruction pushes the return address on the stack
and function f pushes the old frame pointer on the stack
then copies the stack pointer into the frame pointer
so that argument x is now at 8(%ebp).
fldl loads argument x into the floating-point register stack and
fmull pushes argument x onto the floating-point register stack again
so that both st0 and st1 contain the value of x then
fmull multiplies st1 and st0 together and leaves the product in st0.
Function f then pops the old frame pointer off the stack
and returns with the return value in floating-point register st0.
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-11-2005
alice wrote:
> hi all,
> Can anybody please tell me that how an assembly language code can be
> called from a program written in C?I mean what is the syntax for doing
> this thing?


There are at least two ways to interpret the question.

The C syntax for calling a function written in assembly
language is exactly the same as for calling a function written
in ordinary C: you write the name of the function followed by
an open parenthesis, followed a (possibly empty) comma-separated
list of arguments, followed by a close parenthesis. The details
of how the assembly-implemented function obtains its arguments
(if any) and returns its value (if any) and jumps through whatever
linkage hoops the platform requires (if any) are specific to the
platform in question, and usually don't work exactly the same
way on other platforms. You'll need to consult your platform's
documentation for the details; they're really not part of C.

Some C implementations allow assembly code to be intermixed
with C code, so a "C and then some" function can contain both
C and assembly source statements. If the implementation permits
such an extension, the details are implementation-specific and
not portable, and you'll need to consult your documentation --
once again, this really isn't C. It isn't really assembly,
either, but a mixed-language patois, a sort of trade argot
with little structure and less grammar but occasionally useful
nonetheless.

--


 
Reply With Quote
 
James McIninch
Guest
Posts: n/a
 
      01-12-2005
<posted & mailed>

alice wrote:

> hi all,
> Can anybody please tell me that how an assembly language code can be
> called from a program written in C?I mean what is the syntax for doing
> this thing?


C doesn't provide a syntax for it. If you have a compiler that does support
it, consult the documentation about how it may be done.


>
> Thanks,
> Alice


--
Remove '.nospam' from e-mail address to reply by e-mail
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-13-2005

"alice" <> wrote in message
news: oups.com...
> hi all,
> Can anybody please tell me that how an assembly language code can be
> called from a program written in C?I mean what is the syntax for doing
> this thing?


It's the same as for calling a C function.

However the details of how the assembly routine must
be written depend upon your compiler. Check your documentation.

-Mike


 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      01-13-2005
> hi all,
> Can anybody please tell me that how an assembly language code can be
> called from a program written in C?I mean what is the syntax for doing
> this thing?
>


It the assembly code is in its own function it's pretty easy. See the
book Programming from the Ground Up, which includes a lot of information
on C/assembly interaction (specifically in Linux):

http://www.cafeshops.com/bartlettpublish.8640017

Basically, if the assembly function is following the C calling
convention, you just need to add a header for it, call it by the right
name, and link them together. If you need help writing assembly code
that follows the C calling convention, let me know.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
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
adding assembly to windows\assembly through bat file Grant Merwitz ASP .Net 3 09-15-2005 11:40 AM
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57