Skip to content

Instantly share code, notes, and snippets.

@michidk
Last active January 5, 2025 14:03
Show Gist options
  • Save michidk/d71f994aab9e778f5542a19cd7c1a152 to your computer and use it in GitHub Desktop.
Save michidk/d71f994aab9e778f5542a19cd7c1a152 to your computer and use it in GitHub Desktop.
Hello World Optimized
BITS 32
org 0x08048000 ; Default base address for 32-bit executables
ehdr: ; Elf32_Ehdr
db 0x7F, "ELF" ; e_ident: ELF magic
db 1, 1, 1, 0 ; e_ident: 32 bit, little endian, version 1, target System V
db 0, 0, 0, 0, 0, 0, 0, 0 ; e_ident: padding
dw 2 ; e_type
dw 3 ; e_machine
dd 1 ; e_version
dd _start ; e_entry
dd phdr - $$ ; e_phoff
dd 0 ; e_shoff
dd 0 ; e_flags
dw ehdr_size ; e_ehsize
dw phdr_size ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx
ehdr_size equ $ - ehdr
phdr: ; Elf32_Phdr
dd 1 ; p_type
dd 0 ; p_offset
dd $$ ; p_vaddr
dd $$ ; p_paddr
dd file_size ; p_filesz
dd file_size ; p_memsz
dd 5 ; p_flags (read + execute)
dd 0x1000 ; p_align
phdr_size equ $ - phdr
global _start
msg: db 'Hello, World!', 0xA ; Message with newline
_start:
; syscall: sys_write (4)
push 4 ; syscall number
pop eax
push 1 ; file descriptor: stdout
pop ebx
mov ecx, msg ; pointer to message
push 14 ; message length
pop edx
int 0x80 ; invoke syscall
; syscall: sys_exit (1)
push 1 ; syscall number
pop eax
xor ebx, ebx ; exit code 0
int 0x80 ; invoke syscall
file_size equ $ - $$
BITS 64
org 0x400000 ; Default base address for 64-bit executables
ehdr: ; Elf64_Ehdr
db 0x7F, "ELF" ; e_ident: ELF magic
db 2, 1, 1, 0 ; e_ident: 64 bit, little endian, version 1, target System V
db 0, 0, 0, 0, 0, 0, 0, 0 ; e_ident: padding
dw 2 ; e_type
dw 0x3E ; e_machine
dd 1 ; e_version
dq _start ; e_entry
dq phdr - $$ ; e_phoff
dq 0 ; e_shoff
dd 0 ; e_flags
dw ehdr_size ; e_ehsize
dw phdr_size ; e_phentsize
dw 1 ; e_phnum
dw 0 ; e_shentsize
dw 0 ; e_shnum
dw 0 ; e_shstrndx
ehdr_size equ $ - ehdr
phdr: ; Elf64_Phdr
dd 1 ; p_type
dd 5 ; p_flags
dq 0 ; p_offset
dq $$ ; p_vaddr
dq $$ ; p_paddr
dq file_size ; p_filesz
dq file_size ; p_memsz
dq 0x1000 ; p_align
phdr_size equ $ - phdr
msg: db 'Hello, World!', 0xA
_start:
; syscall: sys_write (1)
push 1 ; syscall number
pop rax
mov edi, eax ; file descriptor: stdout
lea esi, [rel msg] ; pointer to message
push 14 ; message length
pop rdx
syscall ; invoke syscall
; syscall: sys_exit (60)
mov eax, 60 ; syscall number
xor edi, edi ; exit code 0
syscall ; invoke syscall
file_size equ $ - $$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment