x86 NASM Assembly - Hello World Explained
In this post, we’ll take you on a step-by-step journey through a classic “Hello, World!” assembly program. We’ll break down each line of code.
;
, allowing us to add explanations to our code without affecting its functionality. To display text on the screen, we’ll utilize the system call sys_write
.
sys_write
mov edx,len ; message length
mov ecx,msg ; message to write
mov ebx,1 ; file descriptor 1 (stdout)
mov eax,4 ; system call number 4 (sys_write)
int 0x80 ; call kernel
for more info: assembly system calls
section .data
msg db "Hello, world!", 0xa ;string to be printed
len equ $ - msg ;length of the string
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker entry point
mov edx,len ;message length
mov ecx,msg ;message to write
mov ebx,1 ;file descriptor 1 (stdout)
mov eax,4 ;system call number (sys_write)
int 0x80 ;call kernel
mov eax,1 ;system call number 1 (sys_exit)
int 0x80 ;call kernel
section .data
− this section is where static variables are defined
msg db "Hello World!", 0xA
− declaring a variable of bytes containing “hello world!” and the line feed character ‘0xA’ or 10 in decimal and storing its address in msg
len equ $ - msg
− getting the length of msg
by subtracting its address from the value of the current addresssection .text
− this section is where the program instructions are located
global _start
− to declare the entry point of our program_start
− tell the linker this is our entry point
mov edx, len
− store msg
length in the register edx
mov ecx, msg
− storing the hello world string in the register ecx
mov ebx, 1
− setting file descriptor to 1 (stdout)mov eax, 4
− setting system call 4 (sys_write)int 0x80
− calling the kernel to take actionmov eax,1
− setting system call 1 (sys_exit)int 0x80
− calling the kernel againAnd that’s what a hello world program looks like in assembly.
to compile this program, two steps are needed assembling and linking:
First, make sure you have the assembler
nasm
and the linkerld
installed in your system.
- to install nasm on Ubuntu type
sudo apt install nasm
, on other platforms check this link assembly environment setup- ld comes preinstalled on most operating systems
nasm -f elf hello.asm
hello.o
will be created in the same directoryld -m elf_i368 -o hello hello.o
Alternatively, you can run it online Here.
Thanks for reading!