LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-31-2004, 06:48 PM   #1
Matrix_Prime
LQ Newbie
 
Registered: Oct 2004
Posts: 3

Rep: Reputation: 0
SPARC Assembly help needed


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.
 
Old 10-31-2004, 07:08 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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
 
Old 10-31-2004, 07:30 PM   #3
Matrix_Prime
LQ Newbie
 
Registered: Oct 2004
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 10-31-2004, 07:39 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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).
 
Old 10-31-2004, 07:43 PM   #5
Matrix_Prime
LQ Newbie
 
Registered: Oct 2004
Posts: 3

Original Poster
Rep: Reputation: 0
I got this when using nm:

prog.o:

[Index] Value Size Type Bind Other Shndx Name

[2] | 0| 0|SECT |LOCL |0 |3 |
[3] | 0| 0|SECT |LOCL |0 |2 |
[5] | 0| 0|NOTY |GLOB |0 |UNDEF |countbits
[7] | 0| 132|FUNC |GLOB |0 |3 |main
[6] | 0| 0|NOTY |GLOB |0 |UNDEF |printf
[1] | 0| 0|FILE |LOCL |0 |ABS |prog5.c
[4] | 0| 0|NOTY |GLOB |0 |UNDEF |scanf
example $ nm countbits.o


countbits.o:

[Index] Value Size Type Bind Other Shndx Name

[3] | 0| 0|SECT |LOCL |0 |2 |
[4] | 0| 0|NOTY |GLOB |0 |2 |countbits
[1] | 20| 0|NOTY |LOCL |0 |2 |skip
[2] | 4| 0|NOTY |LOCL |0 |2 |top
example $


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
 
  


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
assembly ? blackzone Programming 3 10-15-2004 02:36 AM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 04:40 PM
Assembly jinksys Programming 3 09-14-2003 04:33 PM
I need help for Assembly skb Programming 10 08-01-2003 04:51 PM
SPARC assembly language jclark00001 Programming 3 02-26-2003 08:52 PM

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

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