LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-25-2012, 04:31 AM   #1
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
Perverse mixing of C and assembly


I've been finding lately that it is really convenient to write a whole bunch of assembly and then glue it together with a little bit of C. However, there is one issue I've been wondering about: is there some way to use an undefined symbol in a file of C code?

Say, as my primary example, I create assembly file data.S that contains a .data section, which contains object "specialdat" consisting of 64 bytes of various data. I can use that symbol in various other assembly files, because the assembler (gas) allows me to have undefined symbols in individual files which are resolved later. But what if I want to use specialdat in the C file (say, to pass it to a function that takes a void pointer to refer to a block of memory). Is there a way I can do that? GCC will allow me to use the functions from the assembly files in the C file without declaring them, so one would think there would be a way to do it with a symbol that simply points to data.

Of course, I could create the data block in the C file and then reference it from the assembly files, but lets say that I preferred to create the data in an assembly file instead.
 
Old 02-25-2012, 04:42 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I am not sure I understand what you mean, but if you use a variable in a c file, it should be declared. If it is defined in a different compilation unit, you should declare it in a c file where you use it like extern void* specialdat or something like that. Also, the symbol in your assembly file should be exported so the linker can see it (.globl or something like that on x86)
 
1 members found this post helpful.
Old 02-25-2012, 04:47 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, it is 'extern char specialdat [64]' in the C program. Or perhaps (for advanced coders):
Code:
typedef SpecialType {
...
} SpecialType;
extern SpecialType specialdat;
 
1 members found this post helpful.
Old 02-25-2012, 01:05 PM   #4
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467

Original Poster
Blog Entries: 60

Rep: Reputation: 51
I tried that, and it seems to work fine:

Code:
$ cat main.c 
extern char mydata[8];

int main() {
  puts(mydata);
  return 0;
}
$ cat mydata.S 
.globl 	mydata
.data
.align 8
.type	mydata, @object
mydata:
	.byte 'h
	.byte 'e
	.byte 'l
	.byte 'l
	.byte 'o
	.byte '!
	.byte '!
	.byte 0
$ gcc -O2 mydata.S main.c -o test
$ ./test 
hello!!
Thanks!
 
  


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
Dj mixing B-Boy Linux - Software 2 06-10-2008 02:41 AM
Mixing repositories saywot Fedora 2 11-13-2006 01:56 PM
mixing repositories hdo781 Debian 1 03-22-2005 09:51 AM
Mixing Suse 9.1 and Xp Charleyau Linux - Networking 2 01-16-2005 09:38 PM
Mixing C and C++ Cruelpeace Programming 1 02-01-2003 10:41 PM

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

All times are GMT -5. The time now is 08:19 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
Open Source Consulting | Domain Registration