Header image at HALCODE

Writing Programs with Echo (DOS)

May 4th, 2008  |  Published in Assembler, Debug, Languages, Programming, Retro  |  8 Comments

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 :) As suggested by a reader, 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 drop me a line. 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 :)

Responses

  1. NaruFan says:

    May 9th, 2008at 8:21 pm(#)

    wow, this is more advanced and well, WEIRD!

    lol

  2. Karibu says:

    May 9th, 2008at 11:30 pm(#)

    I did not understand… what’s this for?

  3. Carlos Vasquez says:

    May 10th, 2008at 12:32 am(#)

    Crazy ;-)

  4. Daniel Freites says:

    May 10th, 2008at 9:53 am(#)

    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…

  5. hello world, C and GNU as | HALCODE says:

    May 11th, 2008at 11:46 pm(#)

    [...] Later, we encoded such program directly in hexadecimal (no need for DEBUG). And finally, we abused the MS-DOS ECHO command to create a binary, executable hello, world! program directly from the DOS command line (again no [...]

  6. Matt says:

    September 13th, 2008at 12:30 am(#)

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

    I’m curious about your definition of “healthy” :)

    Very cool, I’m impressed.

  7. * 雪 白い says:

    May 3rd, 2009at 2:18 pm(#)

    用Echo命令编写DOS程序…

    (以后会不定期地发点有意思的技术文章上来。
    文章均是由我亲自翻译的,所以某些地方并不是非常忠实于原文,但是可以保…

  8. Alex says:

    November 15th, 2009at 8:28 pm(#)

    Very good !! Здорово!
    Thanks a lot!

Leave a Response