Assembly Language Getting started with Assembly Language Hello world for Linux x86_64 (Intel 64 bit)


Example

section .data
    msg db "Hello world!",10      ; 10 is the ASCII code for a new line (LF)

section .text
    global _start

_start:
    mov rax, 1
    mov rdi, 1
    mov rsi, msg
    mov rdx, 13
    syscall
    
    mov rax, 60
    mov rdi, 0
    syscall

If you want to execute this program, you first need the Netwide Assembler, nasm, because this code uses its syntax. Then use the following commands (assuming the code is in the file helloworld.asm). They are needed for assembling, linking and executing, respectively.

  • nasm -felf64 helloworld.asm
  • ld helloworld.o -o helloworld
  • ./helloworld

The code makes use of Linux's sys_write syscall. Here you can see a list of all syscalls for the x86_64 architecture. When you also take the man pages of write and exit into account, you can translate the above program into a C one which does the same and is much more readable:

#include <unistd.h>

#define STDOUT 1

int main()
{
    write(STDOUT, "Hello world!\n", 13);
    _exit(0);
}

Just two commands are needed here for compilation and linking (first one) and executing:

  • gcc helloworld_c.c -o helloworld_c.
  • ./helloworld_c