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)
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 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. =)
Labels: reviews
0 Comments:
Post a Comment
<< Home