Machine language Hello-World in 120 bytes

Nikolaus Gradwohl2009-02-25T05:37:00+00:00

a while ago i read the really cool tutorial on programming a pdp11. after typing the "hello world" example into the simulator, i startet to search for instructions how to write the machine code for the pdp11 myself.

after a while coding in raw octal numbers, i wanted to take it to the next level and searched for a way to write machine code by hand for my linux box

So i downloaded Intel Architecture Software Developer's Manual, Volume 2: Instruction Set Reference Manual and wrote a shell script that can be used to dump the raw machine code into a file. of course my dualcore-pentium-mega-bla processor in far more sophisticated than the pdp11 cpu was. so it can't be programmed in octal numbers - i used hexadezimal numbers :-)

execute the shellscript and pipe it into a file

sh dump.sh > hello

make it executable and - TATAAAA - a hello world programm in 120 bytes (including the elf header - which adds 80 bytes all by himself)

# write a elf header in the file
echo -ne "\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"
echo -ne "\x02\x00\x03\x00\x01\x00\x00\x00\x54\x80\x04\x08\x34\x00\x00\x00"
echo -ne "\x80\x00\x00\x00\x00\x00\x00\x00\x34\x00\x20\x00\x01\x00\x28\x00"
echo -ne "\x03\x00\x02\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x80\x04\x08"
echo -ne "\x00\x80\x04\x08\x6f\x00\x00\x00\x6f\x00\x00\x00\x05\x00\x00\x00"
echo -ne "\x00\x10\x00\x00"


echo -ne "\x31\xc0"  # clear eax
echo -ne "\x50"  # push 0 on the stack
echo -ne "\x68rld\x0a"  # push the string onto the stack in reverse order, 4 bytes a time
echo -ne "\x68o Wo"
echo -ne "\x68Hell"

echo -ne "\x89\xe1"  # move stackpointer to ecx ( the start of our string )
echo -ne "\xb0\x04"  # move 4 to al ( eax is already 0 because auf line one )
echo -ne "\x33\xc3\xb3\x01"  # xor ebx and move 1 to bl
echo -ne "\x32\xc2\xb2\x0c"  # xor edx and move 12 onto dl (length of the string )
echo -ne "\xcd\x80"  # int 0x80
echo -ne "\xb0\x01"  # move 1 into al
echo -ne "\xcd\x80"  # int 0x80
read more ...