from Hacker News

The smallest Hello World program

by michidk on 12/30/24, 4:23 PM with 49 comments

  • by bd01 on 1/2/25, 8:32 PM

    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 on 1/2/25, 9:19 PM

    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 on 1/2/25, 8:35 PM

    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 on 1/2/25, 8:22 PM

    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- on 1/2/25, 10:08 PM

    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 on 1/2/25, 8:40 PM

    Could a script be a program?

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

    echo Hello World!

  • by gr33kdude on 1/2/25, 8:27 PM

    Linking a similar, very popular past example of this: Teensy: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...
  • by musicale on 1/3/25, 3:15 AM

    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 on 1/3/25, 10:07 AM

    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 on 1/2/25, 8:29 PM

    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 on 1/2/25, 9:00 PM

    The smallest "hello, world" programs in Rust I did for Arduino are 294 bytes for blink and 388 bytes for hw.
  • by Multicomp on 1/3/25, 12:33 AM

    129 byes...supposedly that's 2 punch cards according to Dr gpt. Small program!