LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-28-2004, 03:22 PM   #1
LongName
Member
 
Registered: Aug 2004
Posts: 33

Rep: Reputation: 15
How to use C functions in asm?


Im using nasm as an asm compiler and gcc/g++ for c/c++ compiling.

I have a C function in file1.c and some code in asm in file2.asm. How may I call the function in file1.c from file2.asm?

Thanks in advance!
 
Old 08-28-2004, 03:58 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
Code:
[n00b@localho.outernet] cat test.c
int foo()
{
    printf("foobar\n");
}
[n00b@localho.outernet] cat test.s

.text

.global foo

.global _start

_start:

    call foo
    movl $1, %eax
    int $0x80
[n00b@localho.outernet] gcc test.c test.s -nostartfiles
[n00b@localho.outernet] ./a.out 
foobar
 
Old 08-28-2004, 06:13 PM   #3
LongName
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks very much!

When linking using ld, I get an error stating that there is an undefined reference to 'printf', I have included <stdio.h> in file1.c. What may/am I (be) missing?

Thanks in advance!
 
Old 08-28-2004, 07:56 PM   #4
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you need to link with libc. read the man page for 'ld' and it will tell you how to link with libraries on the command line.
 
Old 08-29-2004, 12:25 AM   #5
LongName
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
Thanks very much!

Everything works now
 
  


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
Have you seen such asm code? snowing Programming 2 07-07-2005 05:13 AM
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
Linux 32 ASM introuble Programming 2 05-10-2005 06:56 AM
asm question karlan Programming 6 07-16-2004 11:54 AM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

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

All times are GMT -5. The time now is 11:59 PM.

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
Open Source Consulting | Domain Registration