Skip to content

Instantly share code, notes, and snippets.

@nekodjin
Last active January 3, 2022 21:43
Show Gist options
  • Save nekodjin/de295ce73182c5d7bf6291d17f671b7f to your computer and use it in GitHub Desktop.
Save nekodjin/de295ce73182c5d7bf6291d17f671b7f to your computer and use it in GitHub Desktop.
Linux x64 Hello World with NASM
%define fd_stdin 0
%define fd_stdout 1
%define fd_stderr 2
%macro sysc 1-4
%if %0 == 4
%if %4 == 0
xor rdx, rdx
%else
mov rdx, %4
%endif
%endif
%if %0 >= 3
%if %3 == 0
xor rsi, rsi
%else
mov rsi, %3
%endif
%endif
%if %0 >= 2
%if %2 == 0
xor rdi, rdi
%else
mov rdi, %2
%endif
%endif
%if %1 == 0
xor rax, rax
%else
mov rax, %1
%endif
syscall
%endmacro
global _start
%include "macros.asm"
%include "syscalls.asm"
%include "fds.asm"
segment .data
msg db "Hello, world!", 0x0a
len equ $ - msg
segment .text
_start:
sysc sys_write, fd_stdout, msg, len
sysc sys_exit, 0
%define sys_read 0
%define sys_write 1
; ...
%define sys_exit 60
; ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment