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.
 

sathishkrishna

Newbie level 6
Joined
Sep 10, 2004
Messages
12
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,281
Activity points
339
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
 

echo47

Advanced Member level 5
Joined
Apr 7, 2002
Messages
3,942
Helped
638
Reputation
1,274
Reaction score
89
Trophy points
1,328
Location
USA
Activity points
33,176
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.
 

aryajur

Advanced Member level 3
Joined
Oct 23, 2004
Messages
793
Helped
124
Reputation
248
Reaction score
37
Trophy points
1,308
Location
San Jose, USA
Activity points
7,788
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.
 

eloeng

Member level 1
Joined
Apr 30, 2004
Messages
34
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Activity points
270
i use borland c++ builder
this programda used where the asm code begin write asm {
and end }

and complie it
 

maui

Member level 3
Joined
Nov 19, 2004
Messages
55
Helped
5
Reputation
10
Reaction score
0
Trophy points
1,286
Location
Melbourne Victoria Australia
Activity points
607
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

Top