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
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
- 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.
- Press Alt-1
ASCII character 1. This makes the first line look
like "mov ah,1".
- Press Alt-205
Code CD, "int ??". 'int' simply calls an interrupt.
An interrupt is an instruction built in to the CPU.
- 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.
- Press Alt-195
Code C3, "ret". 'ret' basically tells the computer to
return to the previous environment.
- 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
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.
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).
|Alt-180, Alt-8 (can't be
done at DOS prompt)
(this is the 88-C2 command)
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!