I just figured this out but instead of splitting my new question ("why?") into another question I think its best if the solution to this problem and an explanation were to be kept on the same page.
I'm writing a basic assembly program to just start and immediately quit using the kernel interrupt at int 0x80
. My current code is simply as follows:
/* Simple exit via kern-interrupt */
.globl start
start:
pushl $0x0
movl $0x1, %eax
subl $4, %esp
int $0x80
assembled with
as -arch i386 <file>.s
upon executing I get a one-line error:
Illegal instruction
It's bizzare, even commenting everything out still results in Illegal instruction
despite there being no instructions at all. Am I missing a linking
step, despite there being no other files to link to? Yes I am
EDIT: Allow me to rephrase my question, why do you need to link when there is no library or anything to link to?
You do need to link it to create an executable. By default, as
just gives you an object file, which is something you can link into an executable (either with other object files or on its own) but is not itself a valid executable. Try:
as -arch i386 -o file.o file.s
ld -o file file.o
In answer to your question:
Why do you need to link when there is no library or anything to link to?
Because the assembler doesn't know that you're not going to link with something else.
Unlike the gcc
compiler where it assumes you want a program unless told otherwise (with the -c
option), as
gives you an object file by default. From the manpage:
"as"
is primarily intended to assemble the output of the GNU C compiler"gcc"
for use by the linker"ld"
If you want a one-step command, you can create a script such as asld
:
as -arch i386 -o $1.o $1.s
ld -o $1 $1.o
and then just use asld file
.
Or, you could set up makefiles to do all the heavy lifting for you.