Check Disk
Checks a disk drive for ready returns error in DOS' ERRORLEVEL for use in batch files.
This file is distributed to show how to check for disk ready using a verify-sector routine. This technique is undocumented in most DOS programming books or manuals.
*Programming description
Out of most all DOS interrupts, only READ, WRITE, and VERIFY DISK SECTORS or the only interrupt services that won't call DOS' Critical Error Handler.
So we will take advantage of the VERIFY service. All this service does is verifies the contents of one or more disk
sectors. This service simply checks the sectors can be found and read and that the CRC is correct. If a disk is not found in the drive, then this service returns an error in AH, while not letting DOS know about the error.
*Registers used: (F)loppy (H)ard drive
AL - number of sectors to verify
AH - (04h) service number for interrupt 13h
CH - cylinder number (F)
low-order 8 bits of cylinder number (H)
CL - sector number (F)
high-order 2 bits of cylinder number,
plus 6 bit sector number (H)
DL - drive number
0 - A:, 1 - B:, 80h - first hard drive (C:)
DH - head number
BX - not used for verifying sectors
On return, if carry flag then error.
*Using in a .bat file
You can use this in a batch file.
Chkdsk returns a 0 in DOS' ERRORLEVEL if disk ready
Chkdsk returns a 1 in DOS' ERRORLEVEL if disk NOT ready
Example:
Chkdsk
@if ErrorLevel == 1 goto error
.
.do normal disk operation
.
goto end
:error
.
.do what ever error handling wanted
.
:end
; Assemble with NBASM
.MODEL tiny
.CODE
org 100h
mov ah,04h ; Verify Disk Sectors
mov al,01h ; Amount to verify
mov ch,01h ; High order cylinder number (8 bits)
mov cl,01h ; Sector Number in bits 0-5
xor dl,dl ; use drive A:
xor dh,dh ; remaining 6-7 bits are low order for CH
int 13h ; DH is the head number
xor al,al ; DOS' ERRORLEVEL
jnc Done ;
mov dl,07h ; simple beep
mov ah,02h ;
int 21h ;
mov al,01h ; DOS' ERRORLEVEL
Done: mov ah,4Ch ; exit to DOS
int 21h ;
.end ; End of assembly code