Lost Era, Microsoft DOS, .COM Assembly, 8 byte program, GetKey


We have previously [1][2][3] introduced the open source project DOSBox, an x86 emulator for DOS. This brings back some good memory of writing DOS assembly. The .COM binary is the simplest execution format because it does not have a header or meta data. The code loads itself at address 100h, prior to that, it is a 256 byte PSP (Program Segment Prefix) that is a data structure to hold the state of a program, e.g. command line parameters can be fetched in PSP.

The DOS interrupt 21h contains the DOS APIs. For example, we have used 9th function to print out a message to STDOUT (the message terminates at dollar sign $). We also learn to use 2nd function to print out a single character. Today, the following will show you how to get a single key from STDIN using interrupts 1, 7 and 8.

The 1st interrupt will let the user press any key and print it out (with echo). The 7 and 8 interrupt will not.  The 7th interrupt will not check the Ctrl+C/Break while the 1st and 8th will check the Ctrl+C/Break. All of them return the character ASCII code in register AL.

So we can come up with three files e.g. getkey01.com, getkey07.com and getkey08.com that check the user input (e.g. similar to getch() in C). And the code will return the ASCII code as the program exit code using DOS function 4c. The return code will be stored in register AL as well.

debug-key Lost Era, Microsoft DOS, .COM Assembly, 8 byte program, GetKey 16 bit assembly language code DOS DOSBOX I/O File implementation MSDOS 16-bit programming languages windows windows command shell

Using debug.exe, it is quicker to generate the program by directly writing the assembly language. The file size is small just 8 bytes. Using in debug.exe will start the program and ask for a single character input. After user presses the key, it will be used as the return code to the DOS. Just four lines of assembly code is more than enough to demonstrate the example of input and output. And somehow, it is useful in DOS batch programming where you can have a menu displayed and wait for the user input. By checking the errorlevel you can jump to corresponding program. For example,

:menu
echo A.  Game
echo B.  Work
echo C.  Music
getkey01.com
if errorlevel 65 goto Game
if errorlevel 66 goto Work
if errorlevel 67 goto Music
::invalid input
goto menu

:Game
echo selected Game
goto EOF

:Work
echo selected Work
goto EOF

:Music
echo selected Music
goto EOF

:EOF
::goto menu here if you wish.

The other twos are similar, you just have to change the first line of assembly code with corresponding interrupt. For example, for getkey07.com, the source code is:

mov ah, 7
int 21h
mov ah, 4ch
int 21h

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
618 words
Last Post: Emerging Era, Win32 Assembly, MASM, Hello World!
Next Post: Lost Era, Microsoft DOS, 16-bit Assembly, Echo program revisited

The Permanent URL is: Lost Era, Microsoft DOS, .COM Assembly, 8 byte program, GetKey

Leave a Reply