Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to compile C and Assembly code together ?

Status
Not open for further replies.

agnivesh

Member level 2
Joined
Apr 24, 2004
Messages
45
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Location
UK
Activity points
549
I have following queries regarding ( embedded systems ) programming in C & Assembly. Please, try to answer as much as you can and give related online references wherever possible in your reply.

1. Basics of combining C & Assembly source codes either
(a) by inline assembly code in C source file, or
(b) by linking separate C and assembly object files etc.

Answer to this query should explain some concepts like details of compiler instructions used i.e, how these are compiled and linked togather, how parameters are passed between different modules and how parameters are stored, accessed & modified in the stack, what are different ways of passing parameters, scopes of variables in different modules etc. ?

2. How C or Assembly code libraries are made (.lib , .h etc.), say if I have some personal source code in C (.c) and assembly (.asm) containing some functions, how can I make library out of that and use it in my source codes ? Can I make a single library out of C and assembly code or do I need to make two different libraries for C and Assembly code ?

thanks in advance
agnivesh.
 

Have u observed the option -B in tcc (if u r using turbo c) its for compiling assembly language with C.
there is one preprocess which will tell the compiler that asmbly lang is incluede that is
#pragma inline
here with different compilers...

If you have an assembly function that you want to call from C:

_myasmfun:

You need to add to the assembly code

global _myasmfun

and add to the C code:

extern int myasmfun (int, int, int);

calling it like

int retval;
retval = myasmfun(1, 2, 3);


Compiling C and assembly code together:

Assume you have mycfile.c and myafile.asm. (It's useful if they don't have the same name.)

1. Assemble the assembly file:
nasm -f coff myafile.asm
This creates an object file, myafile.o.
2. Compile the C file:
gcc -c mycfile.c
This creates another object file, mycfile.o. (Now you should see why you don't want the two source files to have the same name.)
3. Link your object files together:
gcc -o myfile.exe myafile.o mycfile.o
 

agnivesh, some compilers allow you to combine C and assembler. Some don't. Every compiler works differently. There is no general method. You must refer to your compiler's user manual.

sathishkrishna has kindly provided instructions for one specific compiler.
 

I did C and assembly combined programming in Visual C++. If you are using VC++ I can dig up the details in my files and post them.
 

i use borland c++ builder
this programda used where the asm code begin write asm {
and end }

and complie it
 

agnivesh,

Writing inline assembler will depend on your compiler. There are lots of different ways this is done.

Writing a function in assembler is a better way to go. You have to write the function in assembler, taking into account the runtime model of your compiler if you want to pass parameters or a return value. Once you have written it you need to assemble the function. ensuring the assmebler you use can create an object file your linker can link.
Then define it as an extern function in a .h file in your c code.

Creating a library is also fairly compiler dependant, but if your compiler allows you to mix c and assembaly you sould be able to do this in the lib as well.

Maui
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top