December 30th, 2024

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 articleLink Icon
CuriositySkepticismNostalgia
The smallest Hello World program

Michael 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.

AI: What people are saying
The discussion around Michael Lohr's "Hello World" executable in Rust reveals various perspectives on minimalism in programming.
  • 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.
Link Icon 13 comments
By @bd01 - 15 days
This is pretty bad. Let's start with the very first instruction:

  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.
By @Tepix - 15 days
Here's a tiny DOS COM file that does it in 18 bytes:

    ;; 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-...)
By @smokel - 15 days
My favorite language for implementing short Hello World programs in is HQ9+ [1].

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...

By @mrfinn - 15 days
These challenges are funny - they remind me of the old days. Back in the DOS/Windows days, we used to have the .com format, which was perfect for tiny programs. One could even write a program of less than 10 bytes that could actually do something!

We've come a long way since then, and is like, at some point, nobody cared about optimizing executable size anymore

By @5- - 15 days
here's an 80 byte x86_64 linux 'hello world' (okay, not 'Hello world!'). convert to binary with xxd -r -p:

  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.
By @whynotmaybe - 15 days
Could a script be a program?

Because it would be much smaller in a bat file than contains :

echo Hello World!

By @gr33kdude - 15 days
Linking a similar, very popular past example of this: Teensy: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...
By @musicale - 15 days
I realize TFA is trying for object code, but for source code, QuickBASIC (and its successors) isn't bad:

    ? "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.

By @fjfaase - 15 days
Would it be fair to name the program 'Hello World!' and than use argv[0], which is on the stack, to print out 'Hello World!'?
By @xpasky - 15 days
Now, can we make it even smaller applying https://nathanotterness.com/2021/10/tiny_elf_modernized.html ? We shouldn't need the full ELF header...
By @oneshtein - 15 days
The smallest "hello, world" programs in Rust I did for Arduino are 294 bytes for blink and 388 bytes for hw.
By @Multicomp - 15 days
129 byes...supposedly that's 2 punch cards according to Dr gpt. Small program!