LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2016, 02:17 AM   #1
micflunu
Member
 
Registered: Oct 2012
Posts: 73

Rep: Reputation: Disabled
calling a c function from a NASM assembly file


i am new to assembly and i wanna call a c function from an assembly file and i was using the following ...

test.c

#include <stdio.h>
extern void asmfunction(void);
extern int test_variable;
int main(){
printf("before asm: %d\n", test_variable);
asmfunction();
printf("after asm: %d\n",test_variable);
return 0;
}

test.S
.globl _asmfunction
;.global test_variable

_asmfunction:
movl $12345, test_variable
ret
and i compiled it using the following method

$ gcc test.c test.S
$ ./a.out

but i keep getting the error
/tmp/ccmzpUux.o: In function `main':
astest.c.text+0xa): undefined reference to `test_variable'
astest.c.text+0x1f): undefined reference to `asmfunction'
astest.c.text+0x24): undefined reference to `test_variable'
/tmp/ccsUsANm.o: In function `_asmfunction':
(.text+0x2): undefined reference to `test_variable'
collect2: error: ld returned 1 exit status

please what am i doing wrong??
 
Old 07-01-2016, 03:36 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
first: use [code]here comes your code[/code] to keep original formatting
next, you posted test.c, but looks like you tried to compile astest.c. Also I cannot find the result of your gcc command.
As far as I see you have not specified those variables, so they do not exist
what is .globl ?
 
1 members found this post helpful.
Old 07-02-2016, 07:53 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
I would also suggest the use of the C compiler to output the assembly of the main program where it actually invokes the function.

This will show what the actual names of the function and the global variable are.
 
1 members found this post helpful.
Old 07-06-2016, 08:37 AM   #4
micflunu
Member
 
Registered: Oct 2012
Posts: 73

Original Poster
Rep: Reputation: Disabled
How to write a makefile

Tnx u guys your post has been very helpful.But now i need to compile the two files in a makefile.How can i do that cld anybody help
 
Old 07-06-2016, 08:51 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
do you have a makefile already? just as a starting point
 
Old 07-07-2016, 08:06 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
The format of a Makefile is comparatively simple, and easily the best way to see how they work is to surf over to GitHub, look at almost any project, find its Makefile. Or, check out the "Automake" utility.

As for using nasm, at all, I'd candidly suggest that it's probably just a waste of your time to try to do for yourself what an optimizing compiler does so much better. CPUs today are engineered to be driven by compiler-generated code, and manufacturers work closely with compiler writers. Assembly code is ordinarily found in asm {...} blocks, and today those are few and far-between.
 
Old 07-07-2016, 08:52 AM   #7
micflunu
Member
 
Registered: Oct 2012
Posts: 73

Original Poster
Rep: Reputation: Disabled
No i don't have one but i was woundering if i could get help as to how i could write one.I want to understand how i could write a makefile that would build and run a project with a lot of c and an assembly .S file.
 
Old 07-07-2016, 09:09 AM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Use google. You would find http://www.cs.colby.edu/maxwell/cour...als/maketutor/ on how to create a Makefile.

It works the same for all languages.
 
1 members found this post helpful.
Old 07-07-2016, 01:31 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Also take a look at the Automake facility, which is a powerful tool for constructing Makefiles from a template.

When any package instructs you to run ./configure && make, they're using Automake, otherwise known as the GNU Build System.

Last edited by sundialsvcs; 07-07-2016 at 01:34 PM.
 
  


Reply



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
Greetings! Newbie recently addicted to assembly(NASM) Nilonahs LinuxQuestions.org Member Intro 2 07-30-2015 02:16 PM
[SOLVED] Python calling function from another file CincinnatiKid Programming 8 05-30-2015 08:31 PM
[SOLVED] how to traverse byte array in assembly using nasm as assembler? tushs Programming 3 06-25-2010 01:18 AM
Simple mathmatics written in Assembly code using nasm amon Programming 3 01-24-2007 12:31 PM
NASM Assembly Tutorials NCC-1701&NCC-1701-D Programming 2 06-29-2005 06:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:27 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