Hello World Program in Assembly Language

bruno coleman
2 min readNov 17, 2020

Hello everybody!

Here is a simple assembly program that will do the following by using system calls.

  1. Open a file (creating it in the process)
  2. Write the string “Hello World” to the file
  3. Close the file

Tools required:

  1. Linux 64 bit —any 64 bit Linux Operating system such as Ubuntu 64 bit
  2. Text Editor program — such as nano, vim, SublimeText, Gedit, Notepad
  3. Nasm— the assembler that will take our code and create an executable program

If you are running 64 bit Linux then all of these tools will be included and you are ready to go!

section .data
filename db "hello_world.txt",0 ;Name of our file
text db "Hello World!" ;The text we will write

section .text
global _start
_start:
mov rax, SYSTEM_OPEN ;Move 2 into rax register
mov rdi, filename ;Move filename into rdi
mov rsi, CREATE+WR ;Move 65 into rsi
mov rdx, 0777o ;Move octal 0777 into rdx
syscall ;Perform the system call

mov rdi, rax ;Move rax into rdi
mov rax, SYSTEM_WRITE ;Move 1 into rax
mov rsi, text ;Move our String into rdx
mov rdx, 12 ;Move 12 into
syscall ;Perform the system call

mov rax, SYSTEM_CLOSE ;Move 3 into rax
pop rdi ;Pop rdi
syscall ;Perform the system call

exit ;Terminate the program

I have defined the following constants to make the asm code more readable, so replace the values in the assembly code with the values in the following table:

SYSTEM_WRITE  = 1  // 1 is the System Call to Write
SYSTEM_OPEN = 2 // 2 is the System call to open file
SYSTEM_CLOSE = 3 // 3 is the System Call to close a file
WR = 1 // Parameter that opens file for Read and Write
CREATE = 64 // Parameter that creates the file

If you are going to copy and paste the code above straight into notepad then make sure you substitute the constant values (SYSTEM_WRITE, SYSTEM_OPEN,SYSTEM_CLOSE,WR,CREATE) with the integer values above.

To assemble the program using nasm, run the following command:

nasm -f elf64 -o hello_world.o hello_world.asm

To link the object file you just created, use the ld command like this:

ld hello_world.o -o hello_world

Now you can execute your new binary like this:

./hello_world

--

--