ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have written a C program that calls a function written in Assembly and is found in another file. Here is the code for the C program in the file prog.c
Code:
#include <stdio.h>
/*Function Header(s)*/
extern int countbits(unsigned int);
/*Start of Main*/
main()
{
/*Variable declarations*/
extern int n;
/*Do While loop which will read in the integers*/
/*from a file, call the countbits function*/
/*and print the integer along with the number*/
/*of "ON" bits to the screen. This loop will*/
/*repeat until a value of 0 has been processed*/
do
{
scanf("%d", &n);
printf("%d has ", n);
printf("%d ", countbits(n));
printf("bits on\n");
}while(n!=0);
}
Here is the function in Assembly in the file countbits.s
Code:
.global countbits
countbits: !count in o1
mov %g0, %o1 !set count = 0
top:
andcc %o0, 1
bz skip !branch to skip if %o0 is zero
nop
add %o1, 1, %o1 !increment count by 1
skip:
srl %o1, 1, %o1 !Logical right shift
tst %o0 !compare with 0
bnz top !branch to top when not 0
nop
mov %o1, %o0 !return count
retl
nop
My question is, in prog.c (the C file), how do I prototype the countbits.s function, and how do I include the countbits.s file in the prog.c file so that I can compile them and then run the program? Basically what I need to know how to do is define and use functions written OUTSIDE of the C program. I have no idea how to do it, as this is my first encounter with this situation, and the professor told me to figure it out on my own. I have searched google for 2 hours now, looking for examples, but I have come up with nothing. Could someone PLEASE tell me how to do this? Thanks.
Last edited by Matrix_Prime; 10-31-2004 at 07:26 PM.
Well, I don't have a sparc machine at hand to test this,
but at the idea level it goes like:
1. Compile the assember source.
eg. something like "gas -c countbits.s -o countbits.o"
2. Fix some C headers for your countbits methdod, either to a
separate file (countbits.h) or inside your prog.c
What I would put there would be
extern int countbits(int);
3. Compile the C code, eg.
gcc -c prog.c -o prog.o
4. Link them together:
gcc prog.o countbits.o -o prog
thank you for the reply, I appreciate it. However, I am still at a loss. I am pretty sure both the C and the Assemble code is right, it looks right to me, and I am compiling/linking the two together with the following command: gcc -prog.c countbits.s
and I get an a.out, but when I try to run the program (using dat for input):
a.out < dat
the program runs but nothing happens, nothing is printed to the screen. Any idea on what I am not doing correctly? I also fixed the C code in what I pasted above to reflect the added Header that you suggested.
Last edited by Matrix_Prime; 10-31-2004 at 07:32 PM.
Try doing the thing in two steps (eg. first compiling and then linking).
You can also use nm and objdump to see if the .o -files look correct
(the other object file should refer to countbits as an undefined symbol
and other should define it).
I also tried compiling each seperately and then linking. I then tried running the program as described above and it still just hangs there and doesn't do anything. I have to exit with CTRL C
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.