Live data from Hacker News

The smallest Hello World program

blog.lohr.dev

31–40 of 53 posts

Re: The smallest Hello World program

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

Re: The smallest Hello World program

#32

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 emp…

There are probably some golfing languages out there where an empty program outputs Hello World.

Re: The smallest Hello World program

#33

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 emp…

There are probably some golfing languages out there where an empty program outputs Hello World.

[deleted]

Re: The smallest Hello World program

#34

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 emp…

There are probably some golfing languages out there where an empty program outputs Hello World.

I'm certain there is, but I don't have a reference for it yet other than my imaginary HELLO (Highly Efficient Limited Line Output) language.

Re: The smallest Hello World program

#35
post #10
post #7

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 suff…

push 1 pop rax is even shorter (credit: https://old.reddit.com/r/programming/comments/q6mnz1/what_is... )

I feel like I shouldn't love x86 encoding, but there is something charming about this. Probably echoing its 8-bit predecessors. It seems like it's designed for tiny memory environments (embedded, bootstrapping, etc.) where you don't mind taking a hit for memory access.

Re: The smallest Hello World program

#36
post #14

Here's a tiny DOS COM file that does it in 18 bytes: ;; 18 bytes DB 'HELLO_WOIY (credits: https://stackoverflow.com/questions/72635031/assembly-hello-... )

COM files for CP/M and DOS really are a no-nonsense executable format.

I'm a bit disappointed that Linux (or BSD, macOS, etc.) doesn't support them (or similar) out of the box, though Windows will sort of run them via ntvdm.

Re: The smallest Hello World program

#37

Linking a similar, very popular past example of this: Teensy: https://www.muppetlabs.com/~breadbox/software/tiny/teensy.ht...

Thank you, I knew I had read somewhere someone put the program in the ELF header itself and got it down to 45 bytes, this is that exact post.

Re: The smallest Hello World program

#39
post #7

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 suff…

I was able to shave off one additional byte with this:

  ...
  xor rax, rax       ; = 0
  inc rax            ; = 1 - syscall: sys_write
  mov rdi, rax       ; copy 1 - file descriptor: stdout
  lea rsi, [rel msg] ; pointer to message
  mov rdx, 14        ; message length
  syscall
  ...

  $ nasm -f bin -o elf elf.asm; wc -c elf; ./elf
  166 elf
  Hello, World!
So I guess NASM already optimizes this quite well

However, using the stack-based instructions as xpasky hinted at:

  ...
  push 1             ; syscall: sys_write
  pop rax
  pop rdi       ; copy 1 - file descriptor: stdout
  lea rsi, [rel msg] ; pointer to message
  push 14            ; message length
  pop rdx
  syscall
  ...
I get down to 159 bytes! I updated the article to reflect that

Re: The smallest Hello World program

#40
post #21
post #2

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

debug -a 100 178A:0100 int 19 178A:0102 -r cx CX 0000 :2 -n reboot.com -w Writing 00002 bytes -q

Great example, a two bytes reboot utility. From the times when we could turn off the computer with a push of a button without fearing a global catastrophe...
Post reply on HN