Self-contained hello-world program

3

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.

Thorsten Staerk

Posted 2014-01-02T06:23:27.620

Reputation: 261

10

Similar: http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html

– Peter Taylor – 2014-01-02T07:00:41.663

1@PeterTaylor The page I immediately thought of when reading this. Thanks for digging it up before me. – J B – 2014-01-02T12:27:19.423

1@breadbox is a high-rep user on our site. I'm sure they'll nail this one. ;-) – Chris Jester-Young – 2014-01-02T12:27:55.773

1Too bad you specified the platform :) – marinus – 2014-01-02T15:05:28.720

1@marinus why is this a bad thing? – John Dvorak – 2014-01-02T16:13:12.963

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

– Thorsten Staerk – 2014-01-04T18:01:09.047

why not write your own platform? Thingy should fit into a bootsector :) 0x001d bytes: http://www.staerk.de/thorsten/How_to_program_your_own_OS plus two bytes boot sector signature

– Thorsten Staerk – 2014-01-04T22:00:54.370

and sorry for calling this an OS, back in 2008 I was a bit ignorant – Thorsten Staerk – 2014-01-04T22:02:01.203

Answers

1

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.

marinus

Posted 2014-01-02T06:23:27.620

Reputation: 30 224

no mistake... I had it in my mind somewhere... I used to write programs with debug.exe... nopret.com, 2 bytes – Thorsten Staerk – 2014-01-05T15:40:39.847

that was why 70,000 bytes are not possible as .com program... and .com is not part of any URL ;) – Thorsten Staerk – 2014-01-05T15:41:52.587

the rules don't say that it has to terminate. You can remove the int 0x20. – peter ferrie – 2017-11-27T20:44:35.537

at the very least, the int 0x20 can be a ret. – peter ferrie – 2017-11-27T21:35:07.677

1

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

ggrandes

Posted 2014-01-02T06:23:27.620

Reputation: 111

use nasm as assembler and get it much smaller – Thorsten Staerk – 2014-01-04T17:59:25.057

ok, edited as EDITED(2) – ggrandes – 2014-01-05T10:56:19.070

1

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.

marinus

Posted 2014-01-02T06:23:27.620

Reputation: 30 224

Just tried it, works for me on an i7 running 64-bit Ubuntu 14.04 LTS. – LegionMammal978 – 2016-05-14T11:23:47.350