article

The 5 byte EXE file

Email
Submitted on: 1/3/2015 9:30:00 AM
By: Jonathan Smith (from psc cd)  
Level: Advanced
User Rating: By 13 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 1120
 
     This article is based on Vbmew's "make 7 byte .exes" (http://www.1cplusplusstreet.com/vb/scripts/ShowCode.asp?txtCodeId=2221&lngWId=3) His article perked my interest in the Assembly language, so I went out and did some research. This article is a very brief primer on assembler and machine code.

 
				

Note: Before I begin, I realize this program isn't really C or C++. If it's anything, it's assembler. I posted it to the C++ section because it's closer to what I want to accomplish. Personally, I think an ASM section on PSC is long, long overdue. So please, no complaining about this not being C or C++.

With having said that, let us begin.

THE 5-BYTE EXECUTABLE - A PRIMER ON ASSEMBLY LANGUAGE AND MACHINE CODE

For starters, I'd like to create a program which is only 5 bytes in size and does more than the 7 byte EXE that Vbmew shows how to create.

At a DOS prompt, type "copy con echochar.com"

The listing of the program is as follows (with the assembler code explaination)

  1. Press Alt-180
    Code B4, "mov ah, ??". 'mov' is a symbol used to tell the processor to copy a value from somewhere into somwhere else. 'ah' is a CPU register. ah is commonly used with input and output routines. '??' is the value we want to put in to the ah register. We fill in the value of '??' in the next line.
     
  2. Press Alt-1
    ASCII character 1. This makes the first line look like "mov ah,1".
     
  3. Press Alt-205
    Code CD, "int ??". 'int' simply calls an interrupt. An interrupt is an instruction built in to the CPU.
     
  4. Press Alt-33
    ASCII character 33. In hex, 33 is 21. This makes the previous line look like "int 21h". Interrupt 21h is a commonly used IO interrupt. By setting the ah register to 1 and calling interrupt 33 (or 21h), we're telling the computer to stop and wait for input from the keyboard. Since ah is set to 1, once a key is pressed, it is echoed to the screen. If, however, ah was set to 8, the character pressed would *not* be echoed.
     
  5. Press Alt-195
    Code C3, "ret". 'ret' basically tells the computer to return to the previous environment.
     
  6. Press Ctrl-Z to mark the end of the file and the press Enter to write the file.

Like Vbmew's program, this one displays a character to the screen.  Unlike his program, however, this one lets you choose which character is displayed. =)

While this program basically does nothing, it's a great primer (for me, at least).  It gives an introduction as to what basic assembler commands do what, and what their machine code representation is.

Here's the program again, this time, in all assembly.

mov ah,1
int 21h
ret

To make this even more low-level, we could eliminate the automatic display of the character to the screen and display it with code instead. As stated earlier, to eliminate the character echo, the ah register needs to be set to 8 instead of 1.  After a key is pressed, is put into the 'al' register, which is simply another register in the CPU which you need not concern yourself with at the moment. Just know that it holds the ASCII value of the key that was pressed and we need to put that value into the 'dl' register, which is commonly used for output. To do this, we need to use totally different symbols specific for moving registers to registers. One of these new symbols are 88 and C2 combined. In fact, the 88-C2 command specifically copies the value in the al register to the dl register. After performing this operation, we need to tell the computer that we want to display the output. This is done by setting the ah register to 2 and once again calling interrupt 21h.

I also recommend a unicode hex editor for this as the DOS prompt will not suffice (because Alt-8 translates into backspace).

Keyboard command

ASM Code

Machine Code

Alt-180, Alt-8 (can't be done at DOS prompt) mov ah,8 ┤◘
Alt-205, Alt-33 int 21h ═!
Alt-136, Alt-194 mov dl,al    (this is the 88-C2 command)
Alt-180, Alt-2 mov ah,2 ┤☻
Alt-205, Alt-33 int 21h ═!
Alt-195 ret

There you have it. An even more low-level program that does practically the same thing as the first. And it's only 11 bytes in size, still. =)

I know this article might not be the hardcore "we-don't-need-no-stinkin'-programming-language-we've-got-pure-machine-code" tutorial, but at least it's a start. I would recommend taking a look at http://www.theteacher.freeserve.co.uk/alevel/assem/assemix.htm for more information on assembly language and CPU architecture, and I'd also recommend W32Dasm and NASM for writing programs in assembly and finding out what the symbol codes are.

Also, don't forget to vote for an ASM section for PSC!


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.