3
\$\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\$
  • 10
    \$\begingroup\$ Similar: muppetlabs.com/~breadbox/software/tiny/teensy.html \$\endgroup\$ – Peter Taylor Jan 2 '14 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 '14 at 12:27
  • 1
    \$\begingroup\$ @breadbox is a high-rep user on our site. I'm sure they'll nail this one. ;-) \$\endgroup\$ – Chris Jester-Young Jan 2 '14 at 12:27
  • 1
    \$\begingroup\$ Too bad you specified the platform :) \$\endgroup\$ – marinus Jan 2 '14 at 15:05
  • 1
    \$\begingroup\$ @marinus why is this a bad thing? \$\endgroup\$ – John Dvorak Jan 2 '14 at 16:13
1
\$\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\$
  • \$\begingroup\$ no mistake... I had it in my mind somewhere... I used to write programs with debug.exe... nopret.com, 2 bytes \$\endgroup\$ – Thorsten Staerk Jan 5 '14 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\$ – Thorsten Staerk Jan 5 '14 at 15:41
  • \$\begingroup\$ the rules don't say that it has to terminate. You can remove the int 0x20. \$\endgroup\$ – peter ferrie Nov 27 '17 at 20:44
  • \$\begingroup\$ at the very least, the int 0x20 can be a ret. \$\endgroup\$ – peter ferrie Nov 27 '17 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\$
  • \$\begingroup\$ use nasm as assembler and get it much smaller \$\endgroup\$ – Thorsten Staerk Jan 4 '14 at 17:59
  • \$\begingroup\$ ok, edited as EDITED(2) \$\endgroup\$ – ggrandes Jan 5 '14 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\$
  • \$\begingroup\$ Just tried it, works for me on an i7 running 64-bit Ubuntu 14.04 LTS. \$\endgroup\$ – LegionMammal978 May 14 '16 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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