Microsoft KB Archive/62888

COBOL Program to Find All Available Drives in MS-DOS

PSS ID Number: Q62888 Article last modified on 06-13-1990

3.00 3.00a MS-DOS

Summary: A COBOL program can call interrupts (via the special subprogram X“84”) to find out which logical drives are available. The interrupts used are 21 Hex, with function 0E Hex (Select disk) and 21 Hex, with function 19 Hex (Get current disk). The method involves circulating through each possible logical drive (A through Z) and calling the interrupt to select that drive. If this is successful, subsequently calling the interrupt to get the current drive should return that same drive. If it isn’t the same, the drive wasn’t successfully selected and therefore isn’t available. This information applies to Microsoft COBOL Compiler versions 3.00 and 3.00a for MS-DOS.

More Information: Function 0E Hex of interrupt 21 Hex (Select disk) requires input from the following registers: AH (upper byte of AX) = 0E Hex DL (lower byte of DX) = drive code (0=A, 1=B, etc.) When called, it makes the drive passed in DL current, if possible. If that drive is not available, the interrupt does not return an error. This is why function 19 Hex of interrupt 21 Hex (Get current disk) must be called to see if function 0E Hex was successful. Function 19 Hex requires the following registers for input: AH (upper byte of AX) = 19 Hex It returns the following: AL (lower byte of AX) = current drive code (0=A, 1=B, etc.) For more information on calling interrupts from COBOL 3.00 and 3.00a, see Pages 8-4 and 8-5 of the “Microsoft COBOL Compiler 3.0: Operating Guide.” For more information on interrupt 21 Hex, functions 0E Hex and 19 Hex, see Pages 355 and 367 (respectively) of “Advanced MS-DOS Programming, Second Edition,” by Ray Duncan (Microsoft Press, 1988). The program below (LOGICAL.CBL) displays all the available logical drives. To compile and link the program, type the following at the DOS command line: cobol logical; link logical;

Code Example
$SET ANS85 DATA DIVISION. WORKING-STORAGE SECTION. * AX, BX, CX and DX will hold the values of the registers. 01 AX. 05 AH        PIC 99   COMP-X. 05 AL        PIC 99   COMP-X. 01 BX           PIC 9(4) COMP-X. 01 CX           PIC 9(4) COMP-X. 01 DX           PIC 9(4) COMP-X. * Initialize to interrupt 21 Hex. 01 Interrupt    PIC X           VALUE X&quot;21&quot;. * Load actual registers with passed parameters. 01 Flag         PIC X           VALUE X&quot;FF&quot;. * Drive numbers 0-25 and the corresponding letters (A-Z). 01 DriveNumber  PIC 99   COMP-X. 01 DriveLetter  REDEFINES DriveNumber PIC X.   PROCEDURE DIVISION. DISPLAY &quot;The following logical drives are available:&quot; *  Circulate through drives A-Z. PERFORM VARYING DX FROM 0 BY 1 UNTIL DX > 25 *     Function 0E Hex (Select Disk). MOVE 14         TO AH         CALL X&quot;84&quot; USING Interrupt, Flag, AX, BX, CX, DX  *      Function 19 Hex (Get current disk) MOVE 25         TO AH         CALL X&quot;84&quot; USING Interrupt, Flag, AX, BX, CX, DX  *      If current drive = selected drive, drive is available. IF AL = DX THEN *        Set drive number to ASCII code for drive letter. COMPUTE DriveNumber = DX + 65 DISPLAY DriveLetter END-IF END-PERFORM. STOP RUN. Copyright Microsoft Corporation 1990.