Assembly - GNU/Linux - Hello World!

In this tutorial, we are going to see how to create an executable from an assembly file.

The following example uses a 32-bit version of Ubuntu.
Notice that the version is important when you compiling your assembly file.
It is the same for the linkage operation.

Let's see it in detail.

1. The code

This file is named hello.asm.

section .text
    global _start   ;must be declared for linker (ld)

_syscall:   ;system call
int 0x25

_start:    ;tell linker entry point

 push dword len ;message length
push dword msg ;message to write
push dword 1  ;file descriptor (stdout)
mov eax,0x3  ;system call number (sys_write)
call _syscall ;call kernel
add esp,12  ;clean stack (3 * 4)

 push dword 0  ;exit code
mov eax,0x3f ;system call number (sys_exit)
call _syscall ;call kernel
    ;no need to clean stack
section .data

msg db "Hello, world!",0xa ;our dear string
len equ $ - msg   ;length of our dear string

2. Compilation

The following command will create a new file: hello.o.
We tell to the compiler that we want an elf file, standing for Executable and Linkage Format.
We use nasm tool to compile it.

$ nasm -f elf hello.asm

It will be necessary for the linkage.

3. Linkage

$ ld -s hello.o -o hello

This time we use the ld tool to link our object (hello.o) into an executable.
A new file is then created, named hello.

4. Running

Let's run it!

$ ./hello


$ Hello, world!
Well done, you just made your first Assembly program! cool


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.