Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
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
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

This comment has been minimized.

Copy link
Owner Author

commented Oct 10, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.