Wednesday, January 27, 2010

Two steps to using Assembly in Linux (ubuntu 9.10)

Most programmers shy away from assembly language. The reason is not that they don't have a solid background of assembly but that they don't know how to write code outside the simulators etc that they are familiar. Especially when it comes using assembly under linux.

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.out
To execute the code we need to do the following.
$ ./helloWorld.out

The code for helloWorld.s is attached here and thanks to reference 1.

                           .global _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

        .ascii  "Hello, world!\n"       # our dear string
        len = . - msg                   # length of our dear string

References :


SpiceyCurry said...

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

also, there is a typo in your post, there is a '-' on the ld line tht should not be there.

Prachya Yavinci Chalermwat said...

Mine (ubuntu 10.04) has to link like this to work.

ld -o helloWorld.out helloWorld.o