DEBUG Scripts

Keyboard Access

First, some easy DEBUG scripts: a pair of utilities that a batch program can create and delete on the fly that provide single key keyboard input that allows trapping most keys, including the Space bar and Enter key. The examples are pretty trivial - one just shows what keys are pressed,and the other shows how a single key press can be used to do multiple things, even pass the key on to the next program after analysing it. The utilities created are tiny (and somewhat limited) implementations of the kbhit() and getch() functions from C - they just access a couple of the BIOS keyboard interrupt functions and return the key code as ERRORLEVEL. You might want to run the scripts alone and keep the utilities in your \bin directory for general use - go right ahead, my versions are in the public domain: the two scripts themselves are explicitly excluded from my copyright claim.

KBHIT.COM

a
MOV     AH,01
INT     16
JNZ     0108
XOR     AL,AL
MOV     AH,4C
INT     21

rcx
0c
nkbhit.com
w
q


GETCH.COM
a
XOR     AX,AX
INT     16
MOV     AH,4C
INT     21

rcx
8
ngetch.com
w
q
You can create the .COM files by putting the above scripts into KBHIT.SRC and GETCH.SRC, then issuing these commands:

debug kbhit.com < kbhit.src
and

debug getch.com < getch.src
which will invoke DEBUG, create the files, run the script, and put the resulting binary code into the files before terminating. The blank line above "rcx" is an essential part of the script - do not omit it.

Now, lets look at how a batch file can create such a binary program. The idea is simply to echo the script lines to the script file, then invoke DEBUG with the above commands. We then run the programs and look at the returned ERRORLEVELs before erasing the no longer needed .COM and .SRC files.

MLTL0010.BAT (MKGETCH.BAT)
 @echo off
 echo a > getch.src
 echo XOR     AX,AX >> getch.src
 echo INT     16 >> getch.src
 echo MOV     AH,4C >> getch.src
 echo INT     21 >> getch.src
 echo. >> getch.src
 echo rcx >> getch.src
 echo 8 >> getch.src
 echo ngetch.com >> getch.src
 echo w >> getch.src
 echo q >> getch.src
 debug < getch.src
 echo Press keys to see what happens: the program recognizes 'A, 'a', 'g', 'Space, and Enter - ESC  to terminate.
 echo Note that the program is case sensitive and does not recognize certain keys on the enhanced keyboard.
 :loop
 getch
 if errorlevel 65 if not errorlevel 66 echo A
 if errorlevel 97 if not errorlevel 98 echo a
 if errorlevel 103 if not errorlevel 104 echo g
 if errorlevel 13 if not errorlevel 14 echo Enter
 if errorlevel 32 if not errorlevel 33 echo Space bar
 if errorlevel 27 if not errorlevel 28 goto done
 goto loop
 :done
 del getch.*
For one-shot use, it is not necessary to create the .COM file unless there is an exit code (ERRORLEVEL) to be tested - these examples all require the exit code and so all require the .COM file.

The second example of a DEBUG script determines when a key has been pressed and prints a message, then it terminates and leaves the key for DOS to do something with - the character will appear at the prompt after the batch program terminates.

MLTL0020.BAT (MKKBHIT.BAT)
 @echo off
 echo a  > kbhit.src
 echo MOV     AH,01 >> kbhit.src
 echo INT     16 >> kbhit.src
 echo JNZ     0108 >> kbhit.src
 echo XOR     AL,AL >> kbhit.src
 echo MOV     AH,4C >> kbhit.src
 echo INT     21 >> kbhit.src
 echo. >> kbhit.src
 echo rcx >> kbhit.src
 echo 0c >> kbhit.src
 echo nkbhit.com >> kbhit.src
 echo w >> kbhit.src
 echo q >> kbhit.src
 debug < kbhit.src > nul
 :loop
 kbhit
 if not errorlevel 1 goto loop
 if errorlevel 65 if not errorlevel 91 echo Upper case letter
 if errorlevel 97 if not errorlevel 123 echo Lower case letter
 if errorlevel 1 if not errorlevel 65 echo Something other than a letter
 if errorlevel 91 if not errorlevel 97 echo Something other than a letter
 if errorlevel 123 echo Something other than a letter
 del kbhit.*
To adapt those programs to your own needs, you will need a list of keys and their codes. Unfortunately this is more of a task than I want to undertake at this time. I have made a list of ASCII characters and their codes available, but that doesn't address such things as the arrow keys and function keys. For these, you will need a list of scan codes (as distinct from character codes) and examples of how to deal with scan codes. Unfortunately, that will have to wait for another rainy weekend.




Disk Utilities

The first one is one I wrote in early 1999 to put an end to a long discussion in alt.msdos.batch about how to tell if a drive has removable media or not. Floppies, CDs, and Zip drives have removable media; hard disks and network drives do not.
 @echo off
 :: Author=TDavis, Status=Public Domain
 set drive=Invalid
 echo a > testdrv.src
 echo mov ax,4408 >> testdrv.src
 echo mov bl,%1 >> testdrv.src
 echo int 21 >> testdrv.src
 echo mov ah,4c >> testdrv.src
 echo int 21 >> testdrv.src
 echo. >> testdrv.src
 echo rcx >> testdrv.src
 echo 0b >> testdrv.src
 echo w >> testdrv.src
 echo q >> testdrv.src
 debug testdrv.com < testdrv.src > nul
 testdrv
 if errorlevel 15 goto cont0
 set drive=removable
 if errorlevel 1 set drive=non%drive%
 :cont0
 echo Drive %1 is %drive%
 :end
The program is invoked with the drive number (0 = default, A = 1, B = 2, etc.) and reports whether the drive is invalid, removable media, or non-removable media. It does not hang if the removable drive is empty, does not issue error messages, and works with network and CD drives, and with real DOS, Win9x, and NT. Be aware that in the form presented, it writes to the default drive:directory and does not clean up after itself (I left the drive variable and the script and com files so that users can examine them. A working version should clean up after itself.

BTW, the .com is 11 bytes.

Explanation:
DOS interrupt 21, function 44h, subfunction 08 returns information about the removability of the media in the given drive: 0=removable, 1=nonremovable. If the drive doesn't exist, it returns a status of 0f. The status code is returned as ERRORLEVEL through the DOS Terminate with Exit Code function. If ERRORLEVEl is 15 (0f), the drive doesn't exist and is therefore invalid. If it is not invalid, then ERRORLEVEL is examined to see if the media are nonremovable. The status is then echoed to the console.


Readers are encouraged to study a bit of assembly language and to familiarize themselves with the DOS and BIOS interrupts - Ralf Brown's Interrupt List is the standard work on the subject. A simple change to the above scripts makes them recognize the entire enhanced keyboard, but makes them incompatible with older machines and keyboards - see the information on INT 16, functions 0x10 and 0x11 - and there are numerous other easily adapted interrupt functions (I've published one elsewhere that determines what kind of drive one is working with - I would include it here but I can't find the script (too many places to lose too many files)).




  ** Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001 Ted Davis - see License, included by reference. ** 

Input and feedback from readers are welcome. NOTE: the subject of the message must contain the word "batch" for the message to get past the spam filter.

Back to the Table of Contents page

Back to my personal links page - back to my home page