Skip to content

Instantly share code, notes, and snippets.

@armicron
Created October 10, 2016 12:35
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save armicron/e891709ce8893df2fd5fc74c846dcf20 to your computer and use it in GitHub Desktop.
Save armicron/e891709ce8893df2fd5fc74c846dcf20 to your computer and use it in GitHub Desktop.
NASM x86_64 open file and write 'Hello world'
section .text
global _start ;must be declared for linker (ld)
_start: ;tell linker entry point
mov rdi, filename
mov rsi, 0102o ;O_CREAT, man open
mov rdx, 0666o ;umode_t
mov rax, 2
syscall
mov [fd], rax
mov rdx, len ;message length
mov rsi, msg ;message to write
mov rdi, [fd] ;file descriptor
mov rax, 1 ;system call number (sys_write)
syscall ;call kernel
mov rdi, [fd]
mov rax, 3 ;sys_close
syscall
mov rax, 60 ;system call number (sys_exit)
syscall ;call kernel
section .data
msg db 'Hello, world', 0xa
len equ $ - msg
filename db 'test.txt', 0
lenfilename equ $ - filename
fd dq 0
@armicron
Copy link
Author

armicron commented Oct 10, 2016

Compile and run:
nasm -f elf64 code.asm && ld -s -o code code.o
./code

@leandrozitroc
Copy link

good ...

@alimsk
Copy link

alimsk commented Nov 24, 2021

cool

@DucLUT
Copy link

DucLUT commented Mar 13, 2023

help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment