Live data from Hacker News

The smallest Hello World program

blog.lohr.dev

11–20 of 53 posts

Re: The smallest Hello World program

#11
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

People in the embedded space care. Symbian OS was compiled for small size, only certain parts were allowed use O3, such as the jpeg decoder.

Re: The smallest Hello World program

#13
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…

Linux initializes all general purpose registers to zero. It's not documented AFAIK, but should be reliable - it has to init them to some value anyway to avoid leaking kernel state. So you can get away with:

    mov     al,1       ;write
    mov     edi,eax    ;handle=stdout
    mov     esi,msg    ;assumes load address below 4G
    mov     dl,msg.len
    syscall
    mov     al,60      ;assuming syscall succeeded, EAX was bytes written
    xor     edi,edi
    syscall
The load address stays constant unless there's some magic GNU extension header to enable ASLR. If we could get the code loaded below 64K, we could save another byte by using SI instead of ESI; however this doesn't work by default, you'd have to run 'echo 0 > /proc/sys/vm/mmap_min_addr' as root first.

Re: The smallest Hello World program

#16
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

I learned to write COM programs at some point but quickly unlearned it. There were some spots where you can use them and not .bat files, but outside of that it’s a lot.

Re: The smallest Hello World program

#17

Could a script be a program? Because it would be much smaller in a bat file than contains : echo Hello World!

For the purposes of this challenge, no.

> Let’s first establish some rules for our ‘Hello World’ program:

> It should be able to execute directly without passing to any other programs first (so no decompression)

> It should be a ‘proper‘ executable binary according to the spec

Re: The smallest Hello World program

#18
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

Some people care about executable size, (mostly) everyone else ships Electron apps.

Re: The smallest Hello World program

#20
post #5
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

JMP FFFF:0000

INT 13h... uff chills
Post reply on HN