Writing Programs with Echo (DOS)

How do you input those characters as parameters for the echo command? I found no way of doing that. If you know a way, please drop me a line.

Is that possible? Yes, it is. It’s just a matter of redirecting echo output to a file. Writing the program with echo should be a straightforward task if we are able to produce the sequence of characters corresponding to the intended binary, executable file. Is that useful? Surely not. But it’s a healthy way to waste your time 🙂 This can be achieved by writing the characters of the executable file, using a simple text editor like notepad or even the old MS-DOS Editor. Of course, the program should be relatively small or we would adventure into the dangerous lands of masochism. By using the echo command of DOS we will be following the conceited style of doing things 🙂 But we’ll restrict this post to the simple hello, world! program we have been reviewing in previous entries.

The hexadecimal code of our program is:

EB 12 0D 0A 68 65 6C 6C 6F 2C 20 77 6F 72 6C 64
21 0D 0A 24 B4 09 BA 02 01 CD 21 B4 00 CD 21 0D

Now, we only have to input and redirect these hexadecimal values to a file, that we’ll name hello.com.

That would be fairly easy except for some values such as 00 and 09, which represent the NULL and TAB characters, respectively. How do you input those characters as parameters for the echo command? I found no way of doing that. If you know a way, please explain in the comments. Therefore, I changed the code of the program in two ways:

  • The 09 character comes from the instruction mov ah,9. I replaced that by two instructions: mov ah,7 and add ah,2. The semantic stays intact, but the contrived approach allows us to discard the 09 character.
  • Regarding the NULL character (00), it’s a consequence of the line mov ah,00. But we can accomplish the effect of clearing ah by executing xor ax,ax instead. And that’s it.

Take a look at the complete command I used:

hello, world!

Nice 🙂

10 thoughts on “Writing Programs with Echo (DOS)”

  1. mmmm… are you sure it’s not possible to input a 00 character in a file? There has to be way… without editing the file directly, of course

    we can simply open the file in a hexadecimal editor and input the zero, but that’s not what you are trying to do…

  2. > But it’s a healthy way to waste your time

    I’m curious about your definition of “healthy” 🙂

    Very cool, I’m impressed.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.