Skip to content

Instantly share code, notes, and snippets.

@AnatolyShirykalov
Forked from armicron/hello_world_into_file.asm
Last active January 19, 2018 11:24
Show Gist options
  • Save AnatolyShirykalov/cf948a9548ee59d5d28e8016c58c5877 to your computer and use it in GitHub Desktop.
Save AnatolyShirykalov/cf948a9548ee59d5d28e8016c58c5877 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)
mov rdi, 0 ;do not return error code
syscall ;call kernel
section .data
msg db 'Hello, world', 0xa
len equ $ - msg
filename db 'test.txt', 0
lenfilename equ $ - filename
fd dq 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment