Skip to content

Instantly share code, notes, and snippets.

@dixyes
Created August 13, 2025 15:32
Show Gist options
  • Select an option

  • Save dixyes/15dda3a0198e8232eab305558176cfda to your computer and use it in GitHub Desktop.

Select an option

Save dixyes/15dda3a0198e8232eab305558176cfda to your computer and use it in GitHub Desktop.
hello from many unix-like OSs
.text
// for OpenBSD compatible: https://nullprogram.com/blog/2025/03/06/
.pushsection .note.openbsd.ident, "a"
.long 8, 4, 1, 0x6e65704f, 0x00445342, 0
.popsection
.global _entry
_entry:
// for FreeBSD like
lea kern_ostype(%rip), %rdi // mib "kern.ostype"
mov $2, %rsi // sizeof(mib) 2
lea -64(%rsp), %rdx // oldp, on stack
movq $32, -72(%rsp) // save sizeof(oldp) = 32 on stack
lea -72(%rsp), %r10 // sizeof(oldp) 32
xor %r8, %r8 // new = NULL
xor %r9, %r9 // newlen = 0
mov $202, %rax // 202 for SYS_sysctl on bsds, NR_futex on Linux
syscall
// assert syscall success
test %rax, %rax
jnz .Lchecklinux
// 0x65657246 is "Free"
cmpl $0x65657246, -64(%rsp)
jne .Lchecknetbsd
// 0x00445342 is "BSD\0"
cmpl $0x00445342, -60(%rsp)
jne .Lchecknetbsd
jmp .Lfreebsd
.Lchecknetbsd:
// 0x4274654e is "NetB"
cmpl $0x4274654e, -64(%rsp)
jne .Lcheckdragonfly
// 0x00004453 is "SD\0\0"
cmpl $0x00004453, -60(%rsp)
jne .Lcheckdragonfly
jmp .Lnetbsd
.Lcheckdragonfly:
// 0x67617244 is "Drag"
cmpl $0x67617244, -64(%rsp)
jne .Lcheckopenbsd
// 0x6c466e6f is "onFl"
cmpl $0x6c466e6f, -60(%rsp)
jne .Lcheckopenbsd
jmp .Ldragonfly
.Lcheckopenbsd:
// 0x6e65704f is "Open"
cmpl $0x6e65704f, -64(%rsp)
jne .Lcheckfail
// 0x00445342 is "BSD\0"
cmpl $0x00445342, -60(%rsp)
jne .Lcheckfail
jmp .Lopenbsd
.Lchecklinux:
lea -390(%rsp), %rdi // size 390 for SYS_newuname utsname struct
mov $63, %rax // 63 for NR_uname on linux
syscall
// assert syscall success
test %rax, %rax
jnz .Lcheckfail
// 0x756e694c is "Linu"
cmpl $0x756e694c, -390(%rsp)
jne .Lcheckfail
// 0x00000078 is "x\0\0\0"
cmpl $0x00000078, -386(%rsp)
jne .Lcheckfail
jmp .Llinux
.Lcheckfail:
int $0x3
.Llinux:
mov $1, %rdi // stdout
lea hello_linux(%rip), %rsi // buffer
mov $13, %rdx // length
mov $1, %rax // 1 for NR_write on Linux
syscall
mov $42, %rdi // ret code
mov $60, %rax // 60 for NR_exit on Linux
syscall
int $0x3
.Lfreebsd:
mov $1, %rdi // stdout
lea hello_freebsd(%rip), %rsi // buffer
mov $15, %rdx // length
mov $4, %rax // 4 for SYS_write on FreeBSD, NetBSD and DragonFly
syscall
mov $42, %rdi // ret code
mov $1, %rax // 1 for SYS_exit on FreeBSD, NetBSD and DragonFly
syscall
int $0x3
.Lnetbsd:
mov $1, %rdi // stdout
lea hello_netbsd(%rip), %rsi // buffer
mov $15, %rdx // length
mov $4, %rax // 4 for SYS_write on FreeBSD, NetBSD, OpenBSD and DragonFly
syscall
mov $42, %rdi // ret code
mov $1, %rax // 1 for SYS_exit on FreeBSD, NetBSD, OpenBSD and DragonFly
syscall
int $0x3
.Ldragonfly:
mov $1, %rdi // stdout
lea hello_dragonfly(%rip), %rsi // buffer
mov $15, %rdx // length
mov $4, %rax // 4 for SYS_write on FreeBSD, NetBSD, OpenBSD and DragonFly
syscall
mov $42, %rdi // ret code
mov $1, %rax // 1 for SYS_exit on FreeBSD, NetBSD, OpenBSD and DragonFly
syscall
int $0x3
.Lopenbsd:
mov $1, %rdi // stdout
lea hello_openbsd(%rip), %rsi // buffer
mov $15, %rdx // length
mov $4, %rax // 4 for SYS_write on FreeBSD, NetBSD, OpenBSD and DragonFly
syscall
mov $42, %rdi // ret code
mov $1, %rax // 1 for SYS_exit on FreeBSD, NetBSD, OpenBSD and DragonFly
syscall
int $0x3
.data
kern_ostype: .long 1 /* CTL_KERN */, 1 /* KERN_OSTYPE */
hello_linux: .asciz "Hello linux!\n"
hello_freebsd: .asciz "Hello freebsd!\n"
hello_netbsd: .asciz "Hello netbsd!\n"
hello_dragonfly: .asciz "Hello dragonfly!\n"
hello_openbsd: .asciz "Hello openbsd!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment