4
\$\begingroup\$

I want a program that outputs "hello world" on console on my x86_64 based Linux computer. Yes, a complete program, not something silly that needs an interpreter or a compiler to work.

You may:

  • use all glibc functionality
  • submit a static executable
  • submit a dynamic executable with dependencies if you outline them
  • compile an ELF file
  • dig into the ELF format and remove all unneeded stuff
  • be creative and work around the ELF format

Describe here how you create and execute the program.

The shortest program wins.

\$\endgroup\$
8
  • 10
    \$\begingroup\$ Similar: muppetlabs.com/~breadbox/software/tiny/teensy.html \$\endgroup\$ Jan 2, 2014 at 7:00
  • 1
    \$\begingroup\$ @PeterTaylor The page I immediately thought of when reading this. Thanks for digging it up before me. \$\endgroup\$
    – J B
    Jan 2, 2014 at 12:27
  • 1
    \$\begingroup\$ @breadbox is a high-rep user on our site. I'm sure they'll nail this one. ;-) \$\endgroup\$ Jan 2, 2014 at 12:27
  • 1
    \$\begingroup\$ Too bad you specified the platform :) \$\endgroup\$
    – marinus
    Jan 2, 2014 at 15:05
  • 1
    \$\begingroup\$ @marinus why is this a bad thing? \$\endgroup\$ Jan 2, 2014 at 16:13

3 Answers 3

2
\$\begingroup\$

MS-DOS .COM - 21 bytes

From the comments:

marinus may use any platform he likes to go below the 45 bytes from muppetlabs.com/~breadbox/software/tiny/teensy.html – Thorsten Staerk

So now I had to. (So don't accept this answer because it's cheating.)

He made a mistake, because that allows me to do:

BITS 16
              org     0x100
              mov     ah,9
              mov     dx,hello
              int     0x21
              int     0x20
hello:        db      "hello world$"

Compile with:

nasm -f bin -o hello.com hello.asm

And then DOSBOX or something will run the resulting .com, which is 21 bytes in size.

No hacks are even needed because .com files have no structure.

\$\endgroup\$
4
  • \$\begingroup\$ no mistake... I had it in my mind somewhere... I used to write programs with debug.exe... nopret.com, 2 bytes \$\endgroup\$ Jan 5, 2014 at 15:40
  • \$\begingroup\$ that was why 70,000 bytes are not possible as .com program... and .com is not part of any URL ;) \$\endgroup\$ Jan 5, 2014 at 15:41
  • \$\begingroup\$ the rules don't say that it has to terminate. You can remove the int 0x20. \$\endgroup\$ Nov 27, 2017 at 20:44
  • 1
    \$\begingroup\$ at the very least, the int 0x20 can be a ret. \$\endgroup\$ Nov 27, 2017 at 21:35
1
\$\begingroup\$

This is what you had in mind? Or you want any more hacky?

EDITED(2)

Assembler (NASM) { source: 155 bytes, compiled: 384 bytes }

Source code: helloworld.nasm

global _start
_start:
write:
 mov eax, 4
 mov ebx, 1
 mov ecx, h
 mov edx, 12
 int 0x80
exit:
 mov eax, 1
 mov ebx, 0
 int 0x80
h: db 'hello world', 10, 0

Compile

$ nasm helloworld.nasm -f elf64 -o helloworld.o
$ ld helloworld.o -s -o helloworld

Execution

$ ./helloworld 
hello world

Size

$ ls -al helloworld.nasm helloworld
-rw-rw-r-- 1 user user 155 Jan  5 11:08 helloworld.nasm
-rwxrwxr-x 1 user user 384 Jan  5 11:09 helloworld

Platform:

$ uname -io
x86_64 GNU/Linux
$ nasm -v
NASM version 2.09.10 compiled on Oct 17 2011
\$\endgroup\$
2
  • \$\begingroup\$ use nasm as assembler and get it much smaller \$\endgroup\$ Jan 4, 2014 at 17:59
  • \$\begingroup\$ ok, edited as EDITED(2) \$\endgroup\$
    – ggrandes
    Jan 5, 2014 at 10:56
1
\$\begingroup\$

Assembly - compiled: 76 bytes

This is a 32-bit executable, but taking advantage of backwards compatibility it will run on x86_64 systems too. At least it did on mine. It's based on breadbox's "tiny.asm" and it's hackier still so I wouldn't be that surprised if some Linuxes didn't want to touch it.

I tried it on Debian 7 in a VM, and it worked.

BITS 32
              org     0x00200000
              db      0x7F, "ELF"
hello:        db      "hello world", 10
              dw      2                               
              dw      3              
              ; nasm insisted on aligning them properly so let's do it this way                 
exit:         dd      0x80cd4066 ; inc eax - int 0x80 
              dd      start                          
              dd      phdr - $$                       
phdr:         dd      1                               
              dd      0                               
              dd      $$                              
              dw      1                               
              dw      0                               
              dd      filesize                        
              dd      filesize                        
start:        mov     ecx, hello ; B9 0400 0200 -> flag 1 set -> executable
              mov     edx, 12
              inc     eax
              shl     eax,2
              int     0x80
              xor     eax, eax
              jmp     exit
filesize      equ     $ - $$

It actually works:

~$ nasm -f bin -o hello hello.asm;chmod +x hello
~$ ./hello
hello world
~$ wc -c hello
76 hello
~$ 

It even neatly outputs a newline as I had a byte to spare in the ELF header.

\$\endgroup\$
2
  • \$\begingroup\$ Just tried it, works for me on an i7 running 64-bit Ubuntu 14.04 LTS. \$\endgroup\$ May 14, 2016 at 11:23
  • \$\begingroup\$ No output on tio \$\endgroup\$
    – l4m2
    Dec 25, 2022 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.