It comprises two steps.
1. Compiling with assembler
The assembly language compiler in linux is as. The code to compile a program say helloWorld.s is the following$ as helloWorld.s -o helloWorld.o
2. Linking and Executing the code
The output of assembling is object code which needs linking. The code for linking the object file "helloWorld.o".$ ld -helloWorld.o -o helloWorld.outTo execute the code we need to do the following.
$ ./helloWorld.out
The code for helloWorld.s is attached here and thanks to reference 1.
.text
.global _start
_start:
movl $len,%edx # third argument: message length
movl $msg,%ecx # second argument: pointer to message to write
movl $1,%ebx # first argument: file handle (stdout)
movl $4,%eax # system call number (sys_write)
int $0x80 # call kernel
# and exit
movl $0,%ebx # first argument: exit code
movl $1,%eax # system call number (sys_exit)
int $0x80 # call kernel
.data # section declaration
msg:
.ascii "Hello, world!\n" # our dear string
len = . - msg # length of our dear string
References :
1. http://asm.sourceforge.net/howto/hello.html
3 comments:
Great post, I took your example and made a simple program to read a sudo language and compile it to an executable... Check it out http://spiceycurry.blogspot.com/2010/05/simple-compilable-programming-language.html
also, there is a typo in your post, there is a '-' on the ld line tht should not be there.
Mine (ubuntu 10.04) has to link like this to work.
ld -o helloWorld.out helloWorld.o
--Thanks
lies beyond lies omni get a job
Post a Comment