Created
February 4, 2015 15:24
-
-
Save menzenski/74dab45619ca69a00df3 to your computer and use it in GitHub Desktop.
Hello World in Assembly language (x86-64 Unix-like operating systems, NASM syntax)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; MacOS X: /usr/local/bin/nasm -f macho64 *.s && ld -macosx_version_min 10.7 *.o | |
; Solaris/FreeBSD/DragonFly: nasm -f elf64 -D UNIX *.s && ld *.o | |
; NetBSD: nasm -f elf64 -D UNIX -D NetBSD *.s && ld *.o | |
; OpenBSD: nasm -f elf64 -D UNIX -D OpenBSD *.s && ld -static *.o | |
; OpenIndiana: nasm -f elf64 -D UNIX *.s && ld -m elf_x86_64 *.o | |
; Linux: nasm -f elf64 *.s && ld *.o | |
%ifdef NetBSD | |
section .note.netbsd.ident | |
dd 7,4,1 | |
db "NetBSD",0,0 | |
dd 200000000 ; amd64 supported since 2.0 | |
%endif | |
%ifdef OpenBSD | |
section .note.openbsd.ident | |
align 2 | |
dd 8,4,1 | |
db "OpenBSD",0 | |
dd 0 | |
align 2 | |
%endif | |
section .text | |
%ifidn __OUTPUT_FORMAT__, macho64 ; MacOS X | |
%define SYS_exit 0x2000001 | |
%define SYS_write 0x2000004 | |
global start | |
start: | |
%elifidn __OUTPUT_FORMAT__, elf64 | |
%ifdef UNIX ; Solaris/OI/FreeBSD/NetBSD/OpenBSD/DragonFly | |
%define SYS_exit 1 | |
%define SYS_write 4 | |
%else ; Linux | |
%define SYS_exit 60 | |
%define SYS_write 1 | |
%endif | |
global _start | |
_start: | |
%else | |
%error "Unsupported platform" | |
%endif | |
mov rax,SYS_write | |
mov rdi,1 ; stdout | |
mov rsi,msg | |
mov rdx,len | |
syscall | |
mov rax,SYS_exit | |
xor rdi,rdi ; exit code 0 | |
syscall | |
section .data | |
msg db "Hello, world!",10 | |
len equ $-msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
needs moar windows