including assembly language routines in c

R

Robert Cloud

Is it possible to include assembly language routines in
C if I'm using a compiler which has an assembler such as gcc? could I
include them in a main function or would I have to write a seperate
function? what kind of privilege level would be required to execute
a program with assembly in it? how would I denote that the following
code is assembly and thus shouldn't be checked for syntax errors?
 
T

Tim Prince

Robert said:
Is it possible to include assembly language routines in
C if I'm using a compiler which has an assembler such as gcc? could I
include them in a main function or would I have to write a seperate
function? what kind of privilege level would be required to execute
a program with assembly in it? how would I denote that the following
code is assembly and thus shouldn't be checked for syntax errors?

maybe difficult to find out, if your search facility is broken.

http://tldp.org/HOWTO/Assembly-HOWTO/gcc.html
et al.
Also, consider things like SSE intrinsics, or equivalent for your targets.
Then, if you have questions, sign up for gcc-help mailing list.
 
I

Ian Collins

jacob navia wrote:

Have you time travelled?

From: jacob navia <[email protected]>
Newsgroups: comp.lang.c
Subject: Re: including assembly language routines in c
Date: Sun, 20 Jul 2008 22:18:57 +0200
 
J

jacob navia

Ian said:
jacob navia wrote:

Have you time travelled?

From: jacob navia <[email protected]>
Newsgroups: comp.lang.c
Subject: Re: including assembly language routines in c
Date: Sun, 20 Jul 2008 22:18:57 +0200
Changed my motherboard... MMMMM
I think I just forgot something :)
 
W

Walter Roberson

Do not forget that C is translated
directly into assembly in most cases.

What is "most cases" ??

It's been a couple of decades since I used a C compiler that translated
directly into assembly. My experience since then has been with
compilers that translate into a language-independant intermediate code
representation, the results of which were optimized. Assembly was
generated after that stage for some of the (older) compilers but
optimized intermediate code directly to object code is what I see these
days.

Working with manipulated C as an intermediate form is rather clumsy,
and by the time you have reached assembly you have lost a lot of
the opportunities for intra-procedural optimization, so an
intermediate form is common, especially in companies that make
multiple compilers, the more so if the compilers can be targetted
to multiple architectures. For example gcc goes through GENERIC and
GIMPLE on its way to RTL (Register Transfer Language).
 
C

CBFalconer

Robert said:
Is it possible to include assembly language routines in C if
I'm using a compiler which has an assembler such as gcc? could
I include them in a main function or would I have to write a
seperate function? what kind of privilege level would be
required to execute a program with assembly in it? how would I
denote that the following code is assembly and thus shouldn't
be checked for syntax errors?

Remember that the C language runs on many different machines, cpus,
operating systems, etc. However assembly code is automatically cpu
(and system) specific, and thus is not compatible. Some compilers
have an 'asm' instruction to make generating cpu/system specific
code easy, but this is NOT portable. Therefore you should ignore
anyone who tells you just to use it.

The appropriate mechanism is to have a separate module, which is to
be compiled (or assembled) for the actual destination system. This
isolates the non-portable code, and makes porting to another system
easier (just write the appropriate module). Some systems include
such a module written in portable C (and thus probably considerably
slower) so that the program can be installed anywhere, and then
optimized.
 
C

CBFalconer

jacob said:
Yes. You have just to know exactly the calling conventions of

No. The results will not be C, but something cpu/system specific.

However, the point of this answer is to correct your clock. Your
message was dated 2008-07-20 22:18 +0200 and was received here on
2008-07-18 before 0600 -0400. I suspect you have am/pm misset.
 
J

jacob navia

Walter said:
What is "most cases" ??

It's been a couple of decades since I used a C compiler that translated
directly into assembly. My experience since then has been with
compilers that translate into a language-independant intermediate code
representation, the results of which were optimized. Assembly was
generated after that stage for some of the (older) compilers but
optimized intermediate code directly to object code is what I see these
days.

Working with manipulated C as an intermediate form is rather clumsy,
and by the time you have reached assembly you have lost a lot of
the opportunities for intra-procedural optimization, so an
intermediate form is common, especially in companies that make
multiple compilers, the more so if the compilers can be targetted
to multiple architectures. For example gcc goes through GENERIC and
GIMPLE on its way to RTL (Register Transfer Language).

Of course, with "directly" I mean that the *result* of the compilation
is a stream of opcodes, and not an interpreter, even if C interpreters
exist (that is why I said in most cases and not always)
 
K

Keith Thompson

jacob navia said:
You can call your assembly routines from anywhere. Suppose you have
just wrote an assembly file with

int myAdditionFunction(int a,int b)

You just call it with
int result = myAdditionFunction(2,2);

"int myAdditionFunction(int a,int b)" is assembly language? What
assembler are you using?

In any case, the ability to call routines written in other languages
is very common, but still system-specific.

[...]
Use the asm() pseudo instruction in the C code.

*If* your compiler happens to have that non-standard feature.
But beware... it is
not very easy because you will want to avoid destroying the assembler
code that the compiler generates, so you can't just add instructions
like that withing a function, unless you save all the registers
and restore them later before leaving.

Again, this is entirely system-specific, and it may not even be
possible. (Perhaps jacob didn't emphasize that point because he knew
the rest of us would do so.)
 
I

Ian Collins

CBFalconer said:
However, the point of this answer is to correct your clock. Your
message was dated 2008-07-20 22:18 +0200 and was received here on
2008-07-18 before 0600 -0400. I suspect you have am/pm misset.
He did that an hour before you posted, so why bother?
 
C

Chris Torek

Use the asm() pseudo instruction in the C code. But beware... it is
not very easy because you will want to avoid destroying the assembler
code that the compiler generates, so you can't just add instructions
like that withing a function, unless you save all the registers
and restore them later before leaving.

If you use asm() at all, you will depend heavily on the
specific compiler.

If you choose to depend on GCC, what Jacob says here is not quite
correct, because gcc's asm() construct includes communication
between the programmer and the compiler as to what machine registers
and other features are modified by the inline assembly.

For instance, if you are inserting inline assembly on the Glorko
GX-47, you might write:

asm volatile("frob %1,%2,%0" : "=B"(result) :
"g"(arg1), "I"(arg2) : "L");

to tell gcc:

- the instruction must not be moved or combined;
- the output must be in one of the "B" registers;
- arg1 can be in any register or memory ("g"eneral);
- arg2 must fit in an "I"ntermediate instruction field; and
- the "L"aziness register is clobbered by a "frob".

The B and L codes are specific to the Glorko machines, of course.
The letter B means something else on similar machines, and L is
not even valid. So be sure you do not attempt to use this on a
Frobozz series, which also has a frob instruction, but does not
have a laziness register.

(The above is meant as an illustrative example of just what you
are getting into when you start inserting inline assembly. Be
*very* sure this is a good idea before you attempt it.)
 
F

Flash Gordon

jacob navia wrote, On 20/07/08 00:01:
Of course, with "directly" I mean that the *result* of the compilation
is a stream of opcodes, and not an interpreter, even if C interpreters
exist (that is why I said in most cases and not always)

Having read and written assembler and also debugged at machine code
level (without the benefit of a disassembler) I can say with a fair
degree of certainty that assembly is *not* a stream of opcodes, it is a
language (or several languages) in text with its own syntax, often
macros and definitely things that don't make it in to the machine-code.

This may seem like nit-picking, but since the OP wanted to integrate
assembly and C it is not. C and assembly are different languages that
are *both* normally used to produce object files containing machine-code
and data (which is not normally used as opcodes). Assembly language
often has a lot more features than machine-code but by its very nature
you can in assembly write code that uses the same calling conventions of
whatever C compiler you happen to be using.
 
J

jacob navia

Robert said:
Is it possible to include assembly language routines in
C

Yes. You have just to know exactly the calling conventions of
your compiler and operating system. Do not forget that C is translated
directly into assembly in most cases.

if I'm using a compiler which has an assembler such as gcc?

You can use another assembler or gas, as you wish. All assemblers
should generate the same code.
could I
include them in a main function or would I have to write a seperate
function?

You can call your assembly routines from anywhere. Suppose you have
just wrote an assembly file with

int myAdditionFunction(int a,int b)

You just call it with
int result = myAdditionFunction(2,2);

In some compilers (such as gcc) you can also add
inline assembler with the asm() pseudo function.
Read your compiler documentation to know how to do
this. You will have to learn the gcc inline assembler
language something that is far beyond my failing
neurons :)

what kind of privilege level would be required to execute
a program with assembly in it?

None. Unless you use processor instructions that are privileged.
If you use those, you will have to write a device driver to
run your program.
how would I denote that the following
code is assembly and thus shouldn't be checked for syntax errors?

Use the asm() pseudo instruction in the C code. But beware... it is
not very easy because you will want to avoid destroying the assembler
code that the compiler generates, so you can't just add instructions
like that withing a function, unless you save all the registers
and restore them later before leaving.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top