Using assembly in C

A

alice

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
 
R

Robert Gamble

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
 
T

Thomas Matthews

alice said:
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
 
E

E. Robert Tisdale

alice said:
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.
 
E

Eric Sosman

alice said:
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.
 
J

James McIninch

<posted & mailed>
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.
 
M

Mike Wahler

alice said:
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
 
J

Jonathan Bartlett

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top