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
Tweet This! submit to reddit Digg! Tags: | 4 comments | no trackbacks

See also:

Handassembled java bytecode

Trackbacks

Comments

Leave a response

  1. Kayos 2009-07-20T12:40:31+00:00

    Can I get the source for your dump.sh?

    My email is Kayos.alt@gmail.com

  2. free electricity 2011-10-08T19:36:43+00:00

    nice and thanks.

  3. Steuarty Crutsingero 2012-07-18T17:30:50+00:00

    I needed to compose you a very little observation just to give thanks again just for the awesome ideas you have provided here. It was simply tremendously generous of you to deliver publicly all a number of people could have made available as an e-book to earn some cash for themselves, notably given that you could possibly have done it in case you wanted. Those creative ideas as well worked as the fantastic way to be sure that someone else have the identical zeal the same as my very own to find out a good deal more in regard to this condition. I'm sure there are lots of more pleasant opportunities up front for folks who see your blog post.

  4. Stay Alive 2014-05-23T11:32:09+00:00

    I always dreamed of seeing it through..but never did. This is just the information I need to be able to do a lot...thanks so so much...God bless you

Leave a comment