LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2010, 03:34 PM   #1
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Posts: 35

Rep: Reputation: 0
Linking assembly and C


Hi everybody;
I have a C code and assembly (nasm) code and I am not able to find how to link them each other. Here are the codes :
NASM side:
Code:
global	_maxofthree
	
	section .text
_maxofthree:
	mov	eax, [esp+4]
	mov	ecx, [esp+8]
	mov	edx, [esp+12]
	cmp	eax, ecx
	cmovl	eax, ecx
	cmp	eax, edx
	cmovl	eax, edx
	ret
C Side:
Code:
int maxofthree(int , int , int );

#include <stdio.h>

int main() {

    printf("%d\n", maxofthree(1, -4, -7));
    printf("%d\n", maxofthree(2, -6, 1));
    printf("%d\n", maxofthree(2, 3, 1));
    printf("%d\n", maxofthree(-2, 4, 3));
    printf("%d\n", maxofthree(2, -6, 5));
    printf("%d\n", maxofthree(2, 4, 6));
    return 0;
}
Original code is in http://www.cs.lmu.edu/~ray/notes/nasmexamples/ but it only mentions how to link in windows, but it did not mention linux. I've asked it to google, but no reply.
I am using ubuntu 9.10 and my NASM version 2.05.01 compiled on Nov 5 2008.

Any help is greatly appreciate.
 
Old 04-29-2010, 03:36 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
nasm <assembly>.s -o asm.o
gcc <cfile>.c -c -o cfile.o
ld cfile.o asm.o -lgcc -lc -lm -o output
 
1 members found this post helpful.
Old 04-29-2010, 03:37 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Oh and for portability sake, cmov should be replaced my a mov/jmp/cmp construct.
 
1 members found this post helpful.
Old 04-29-2010, 03:40 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by gozlemci View Post
I have a C code and assembly (nasm) code and I am not able to find how to link them each other.
It is a lot easier if you let gcc invoke the linker for you.

Code:
nasm -felf32 asm_name.asm
gcc -m32 c_name.c asm_name.o -o program_name
If you are running 32 bit Linux, the -m32 is not needed (but it shouldn't do any harm).

If you are running 64 bit Linux, the -m32 is needed and various 32 bit support files might need to be installed before it will work.

Last edited by johnsfine; 04-29-2010 at 03:47 PM.
 
1 members found this post helpful.
Old 04-29-2010, 03:46 PM   #5
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Posts: 35

Original Poster
Rep: Reputation: 0
Thank you everybody again. Another good reason to use Linux Yaşasın Linux
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Static linking vs. dynamic linking? posix_memalign Programming 13 04-18-2010 02:27 PM
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 03:49 AM
Problem linking assembly program with C lib. 95se Programming 1 03-08-2005 09:44 PM
Assembly jinksys Programming 3 09-14-2003 04:33 PM
assembly sanjay pradhan Programming 1 02-01-2002 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration