The smallest Hello World program
Michael Lohr demonstrates creating the smallest "Hello World" executable in Rust, achieving a final size of 167 bytes by optimizing assembly code and manually crafting the ELF header.
Read original articleMichael Lohr explores the creation of the smallest possible "Hello World" executable in Rust, focusing on minimizing binary size. He begins by developing the program in assembly to establish a baseline for comparison with other languages. The program must run on modern 64-bit x86 Linux machines, print "Hello World" to standard output, and exit successfully. Lohr details the assembly code required to achieve this, emphasizing the need to bypass the overhead of using the standard C library's printf function. After assembling and linking the code, he finds the binary size to be 4728 bytes, which he reduces to 4352 bytes by stripping unnecessary symbols. He explains the ELF (Executable and Linkable Format) and its role in binary size, noting that while a flat binary can be created, it cannot be executed without the ELF header. By manually crafting the ELF header, he successfully reduces the size to 167 bytes, and further optimizations could potentially bring it below 100 bytes. Lohr concludes by recommending further reading for those interested in executable formats and optimizations.
- The smallest "Hello World" program in assembly was created to minimize binary size.
- Initial binary size was 4728 bytes, reduced to 4352 bytes by stripping symbols.
- The ELF format is crucial for executable binaries on Linux.
- A flat binary can be created but lacks the necessary ELF header for execution.
- The final executable size achieved was 167 bytes, with potential for further reduction.
Related
Lisp with GC in 436 Bytes
SectorLISP, a compact programming language with garbage collection, fits in a 436-byte boot sector, surpassing FORTH and BASIC. It runs on 1981 PC models and emphasizes file size optimization.
SectorC: A C Compiler in 512 bytes (2023)
SectorC is a compact C compiler fitting within a 512-byte boot sector, supporting a significant subset of C with advanced features, achieving a size of 303 bytes for practical applications.
Smallest Self Reproducing Program
The 1994 IOCCC entry "Worst abuse of the rules" by Szymon Rusinkiewicz claims to be the smallest self-reproducing program, prompting judges to revise contest rules to prevent similar submissions.
Fitting a Forth in 512 bytes (2021)
The article explores bootstrapping a system from a 512-byte seed using a compact Forth implementation, employing techniques like threaded code and compression to optimize functionality within strict constraints.
A Simple ELF
The article examines the complexities of creating a simple Linux program, contrasting a standard C version with one using direct system calls, emphasizing that simplicity reduces complexity, not necessarily ease.
- Several commenters share alternative methods and languages for creating smaller "Hello World" programs, with examples in Python, BASIC, and DOS assembly.
- There is a debate on the efficiency of assembly instructions, with suggestions for optimizing the Rust implementation further.
- Some participants reminisce about the past, noting the ease of creating tiny programs in older formats like DOS .com files.
- Links to resources and examples of small executables are shared, indicating a community interest in minimizing code size.
- Comments reflect a mix of humor and nostalgia regarding the challenges of writing compact code.
mov rax, 1
An actual "mov rax, 1" would assemble to 48 B8 01 00 00 00 00 00 00 00, a whopping TEN bytes.nasm will optimize this to the equivalent "mov eax, 1", that's 6 bytes, but still:
xor eax, eax ; 2 bytes
inc eax ; 2 bytes
would be much smaller. Second line: mov rdi, 1
You already have the value 1 in eax, so a "mov edi, eax" (two bytes) would suffice. Etc. etc. ;; 18 bytes
DB 'HELLO_WOIY<$' ; executes as machine code, returning SP to original position without overwriting return address
mov dx, si ; mov dx,0100h MS-DOS (all versions), FreeDOS 1.0, many other DOSes
xchg ax, bp ; mov ah,9 MS-DOS 4.0 and later, and FreeDOS 1.0
int 21h
ret
(credits: https://stackoverflow.com/questions/72635031/assembly-hello-...)Joking aside, this page [2] used to be a great tutorial on writing small ELF binaries, but I'm not sure whether it will still work in 64-bit land. It proved very helpful for writing a 4K intro back in 1999.
[1] https://esolangs.org/wiki/HQ9%2B
[2] https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...
We've come a long way since then, and is like, at some point, nobody cared about optimizing executable size anymore
7f454c46488d3537000000ffc7b20eeb03003e00
b001eb1a01000000050000001800000000000000
1800000005000000b03c0f05ebfa380001006865
6c6c0000010068656c6c00006f20776f726c640a
i'm sure this can be improved -- but i could never get any x86_64 linux elf to under 80 bytes. see if you can fit the exclamation point still.Because it would be much smaller in a bat file than contains :
echo Hello World!
? "hello, world!"
PILOT eliminates the quotes: T:hello, world!
Of course a typical REPL (Python, JavaScript, Lisp, etc.) will print out something similar (but often quoted) if you just type the quoted string.And I'm sure there is already some language (call it HELLO) which simply prints "hello, world!" for an empty program.
Related
Lisp with GC in 436 Bytes
SectorLISP, a compact programming language with garbage collection, fits in a 436-byte boot sector, surpassing FORTH and BASIC. It runs on 1981 PC models and emphasizes file size optimization.
SectorC: A C Compiler in 512 bytes (2023)
SectorC is a compact C compiler fitting within a 512-byte boot sector, supporting a significant subset of C with advanced features, achieving a size of 303 bytes for practical applications.
Smallest Self Reproducing Program
The 1994 IOCCC entry "Worst abuse of the rules" by Szymon Rusinkiewicz claims to be the smallest self-reproducing program, prompting judges to revise contest rules to prevent similar submissions.
Fitting a Forth in 512 bytes (2021)
The article explores bootstrapping a system from a 512-byte seed using a compact Forth implementation, employing techniques like threaded code and compression to optimize functionality within strict constraints.
A Simple ELF
The article examines the complexities of creating a simple Linux program, contrasting a standard C version with one using direct system calls, emphasizing that simplicity reduces complexity, not necessarily ease.