How to use assembler (as) in UNIX? [I got errors using assembler]


 
Thread Tools Search this Thread
Top Forums Programming How to use assembler (as) in UNIX? [I got errors using assembler]
# 1  
Old 03-17-2007
Question How to use assembler (as) in UNIX? [I got errors using assembler]

Hi, folks,

I have a simple program main.c. The program is very simple, just for testing purpose. The program was proven correct by using "gcc".

Now I would compile it step by step from main.c to main.o. Here is what I did:

cpp main.c main.i <This step succeeded>
cc main.i -o main.s <This step also succeeded>
as -o main.o main.s
<OOPS Smilie , I got a huge list of errors at this step. Most of them are like
"main.s Error: junk at end of line, first unrecognized character valued 0x..."
or "main.s:1: Error: invalid character (0x1) in mnemonic"

Anybody has a clue what's happening here? Thank you!

Last edited by meili100; 03-17-2007 at 10:30 PM..
# 2  
Old 03-18-2007
cc main.i -o main.s <This step also succeeded>

Maybe, but main.s is probably an executable type of file despite the name. Look at it with an editer. You need some flag to tell the compiler to stop after generating assembler.
# 3  
Old 03-18-2007
Aha, I tried with cc -S main.i main.s
Now the main.s contains assemble instructions.

But after as -o main.o main.s

I use ld to link it: ld -o main main.o
the system reports:
$ ld -o main main.o
ld: warning: cannot find entry symbol _mainCRTStartup; defaulting to 00401000
main.o(.text+0x15):main.c: undefined reference to `_alloca'
main.o(.text+0x1a):main.c: undefined reference to `__main'
main.o(.text+0x26):main.c: undefined reference to `malloc'
main.o(.text+0x5d):main.c: undefined reference to `printf'


That's wierd. Any clue?

Quote:
Originally Posted by Perderabo
cc main.i -o main.s <This step also succeeded>

Maybe, but main.s is probably an executable type of file despite the name. Look at it with an editer. You need some flag to tell the compiler to stop after generating assembler.
# 4  
Old 03-18-2007
You are making a lot of extra work for yourself, you should just let the compiler do the work. You don't say what OS you are using, etc. But in general. when cc invokes ld it tends to do two more things...
1) it includes some some stub to call main... this is often called crt0.o or something like that.
2) it includes some standard libraries
# 5  
Old 03-15-2008
Quote:
Originally Posted by meili100
Aha, I tried with cc -S main.i main.s
Now the main.s contains assemble instructions.

But after as -o main.o main.s

I use ld to link it: ld -o main main.o
the system reports:
$ ld -o main main.o
ld: warning: cannot find entry symbol _mainCRTStartup; defaulting to 00401000
main.o(.text+0x15):main.c: undefined reference to `_alloca'
main.o(.text+0x1a):main.c: undefined reference to `__main'
main.o(.text+0x26):main.c: undefined reference to `malloc'
main.o(.text+0x5d):main.c: undefined reference to `printf'


That's wierd. Any clue?
This sounds like a link library issue, as a result of troubled gcc installation on cygwin.
_alloca is defined in libgcc.a.
__main is defined in libc.a

So, to solve these linker errors, you would need to explicity specify the link libraries such as libgcc.a and libc.a

This is how you do it :
In your makefile define variable LIBS and LIBDIR. Set the LIBDIR to point to where the library files are.

LIBS = $(LIBDIR)/libgcc.a $(LIBDIR)/libstdc++.a /lib/libc.a

Now include the $(LIBS) in the linker command.

$(PROJECT).bin: $(OBJECTS)
$(LD) -M -nostartfiles -nodefaultlibs -T $(LINK_FILE) -o \
$(PROJECT).bin $(OBJECTS) $(LIBS) > $(PROJECT).map


This should resolve the issue.
# 6  
Old 03-15-2008
If you are really interested in knowing howto use the assembler on UNIX you should write a small piece of code in assembly instead of in C. What you are doing essentially is breaking down the steps taken by the compiler to produce object code. The C source code is first translated into assembly by the compiler. The assembly listing is then translated down into object code by the assembler. cc is much like a wrapper that bundles both the C compiler [on AIX it's called ccom] and the assembler [as] into one software package.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading hex data from assembler

Hi, I have files that has got ebcdic character set and also, there are fields like binary and hex fields. is there a way to convert this to normal ascii data by taking care of comp & comp-3 fields? Many Thanks!! (10 Replies)
Discussion started by: ahmedwaseem2000
10 Replies

2. Programming

Does the assembler output differ between operating systems ?

The assembly code generated by assembler, from a C-source code depends on the CPU architecture underlying it, eg x-86 . Then does the assembler output of a simple C-source code (containing common function-calls of both windows and linux) differ between Operating Systems ? (1 Reply)
Discussion started by: vishwamitra
1 Replies

3. Solaris

Assembler for Solaris

Is there any assembler for Solaris ---------- Post updated at 10:41 AM ---------- Previous update was at 10:40 AM ---------- found please delete topic (2 Replies)
Discussion started by: microbot
2 Replies

4. Shell Programming and Scripting

assembler error message

assembler messages for reading open no such file or directory :) (1 Reply)
Discussion started by: sanjyotdk
1 Replies

5. UNIX for Dummies Questions & Answers

Assembler compiler

Hi All, Now recently semi-retired and needing some mental stimulation. As an ex VMS person I used to use the assembly language compiler that came with VMS. I know that my flavour of Unix (True64 / Digital Unix) comes with a C compiler but is there an assembler lurking in there? Have checked the... (1 Reply)
Discussion started by: Roslynlea
1 Replies

6. Shell Programming and Scripting

mainframe assembler file into Unix

Hi, I have received a mainframe file ( VSAM file ) . I would like to handle that file in Unix. i.e I would like to take the last record of the file. I have given wc -l <file_name> , it gives 0 lines. Even though It has some lines , it is not giving count exactly. When I gave file <file_name>... (1 Reply)
Discussion started by: thambi
1 Replies
Login or Register to Ask a Question