This is only a preview of the May 1994 issue of Silicon Chip. You can view 31 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Articles in this series:
Items relevant to "Fast Charger For Nicad Batteries":
Items relevant to "Two Simple Servo Driver Circuits":
Items relevant to "An Induction Balance Metal Locator":
Items relevant to "Dual Electronic Dice":
Items relevant to "Multi-Channel Infrared Remote Control":
Items relevant to "Computer Bits":
Articles in this series:
Articles in this series:
|
COMPUTER BITS
BY DARREN YATES
What’s your free disc space?
This month, we begin a series of articles looking
at BIOS & DOS interrupts. In this article, we take
a look at a simple programming technique that
enables us to find out the free space on any drive
without having to use the DOS DIR command.
If you’re writing programs that use
up lots of disc space, particularly database files or any graphics programs,
the odds are that you’ll need to keep
track of how much space you have to
work with on a disc drive, whether it
be hard or floppy.
A program that crashes when the
disc drive is full is pretty useless but
most programming languages don’t
have a simple routine which returns
the bytes free in a variable.
An example of this related to
electronics is if you’re writing code
which allows the computer to sample
incoming analog voltages via an A/D
converter. At any time, and depending
on your sampling speed, it would be
quite easy for the system to run out of
disc space.
A crash at this point would be disastrous and would almost certainly
necessitate a repeat performance of the
sample. Thankfully, we can get access
to this information in the same way
the DIR command does using a simple
QuickBASIC routine.
CALL INTERRUPT()
In the past, we’ve used the CALL
ABSOLUTE() routine built into both
QBasic and QuickBASIC. This time,
let’s try an even simpler routine which
is built into QuickBASIC called CALL
INTERRUPT().
The original method consisted of
writing assembly code which has to be
74 Silicon Chip
stored into an array and then the base
and offset address found for the first
element of that array and so on. While
it worked well, it was by no means an
easy to remember process.
When QuickBASIC was launched,
the people at Microsoft realised the
usefulness of the inbuilt BIOS and
DOS routines and incorporated a simple interface function which allowed
easy access to them.
One of these routines happens to be
“Find Disc Space”. In the DOS routines, it is designated INT 21h,3600h.
When this routine is called, it returns
various pieces of useful information
and this can be seen in Table 1.
Upon calling INT 21H, the AX register must be loaded with 3600 hex.
As we mentioned a couple of months
ago, you can think of the interrupt as
TABLE 1
Get Disk Free Space
(interrupt 21h, service 36h)
Category: Disk services
Registers on Entry:
AH: 36h
DL: Drive code
Registers on Return:
AX: Sectors per cluster
BX: Available clusters
CX Bytes per sector
DX: clusters per drive
the street name and the AX register as
the house number.
When the routine is completed, all
four general purpose 16-bit registers
AX to DX are called into play and
return the sectors per cluster, number
of free clusters, bytes per sector and
clusters per drive, respectively.
From these registers, we can gather
the following about the drive:
• bytes per sector;
• number of sectors per cluster;
• number of free clusters;
• number of clusters per drive;
• amount of drive space used;
• drive space remaining; and
• total drive size.
In most normal programming cases,
only the last three items are of any
practical use.
Sample program
Right, let’s now take a look at
the sample program in Fig.1. If you
compare this to the other programs
published so far, you’ll notice that it
requires fewer lines of code.
To start off, we first have to define
a type variable which we’ll call REGTYPE. This consists of all the required
registers for CALL INTERRUPT(). The
initial values are not important at this
stage so we don’t have to worry about
clearing them.
Next, we define two arrays – INARY
and OUTARY – as copies of the type
REGTYPE.
After that, the AX field of INARY
is set to 3600 hex. The following line
takes advantage of QuickBASIC’s
COMMAND$ string metacommand.
When typing in the command
line at the DOS prompt to start the
program, you would normally type
BYTEFREE C: or whatever drive letter
you like. COMMAND$ then contains
everything after the program name.
Basic Listing For Disk Bytes Free Utility
‘Disk Bytes Free Utility
‘Written by DARREN YATES B.Sc.
‘Copyright 1994 Silicon Chip Publications Pty Ltd
TYPE regtype
AX AS INTEGER
BX AS INTEGER
CX AS INTEGER
DX AS INTEGER
BP AS INTEGER
SI AS INTEGER
DI AS INTEGER
FLAGS AS INTEGER
DS AS INTEGER
ES AS INTEGER
END TYPE
DIM inary AS regtype
DIM outary AS regtype
inary.AX = &H3600
drive$ = UCASE$(MID$(COMMAND$, 1, 1))
inary.DX = ASC(drive$) - 64
CALL interrupt(&H21, inary, outary)
bytespersector& = outary.CX
sectorspercluster& = outary.AX
freeclusters& = outary.BX
clustersperdrive& = ABS(outary.DX)
memfree& = bytespersector& * sectorspercluster& * freeclusters&
drivesize& = bytespersector& * sectorspercluster& * clustersperdrive&
PRINT : PRINT “Analysis of drive “; drive$ + “:”
PRINT : PRINT “Bytes free:”; memfree&; “bytes”, , “Bytes per sector :”;
bytespersector&
PRINT “Drive size:”; drivesize&; “bytes”, , “Sectors per cluster:”;
sectorspercluster&
PRINT “Bytes used:”; drivesize& - memfree&; “bytes”, , “Clusters per drive :”;
clustersperdrive&
program can be converted to work in
the same manner as BUTTON.BAS
published previously by just substituting the new INT number and adding
the appropriate bytes to the arguments
being passed to the machine code
program.
For those using QuickBASIC, make
sure that you load QB with the QB.QLB
quick library. This library holds the
CALL INTERRUPT and CALL ABSOLUTE routines. You can do this by
typing in the following line at the C:\
QB45 does prompt:
C:\QB45> QB/LQB.QLB
The /L option allows you to load
in an extra Quick library and automatically invokes QB.LIB when you
compile the program. The program
will also work with drives running
Microsoft Double
Space, recognising
both the real and host drives.
To run the EXE file, simply type:
BYTEFREE <drive>, where <drive>
is the drive letter.
As usual, we are making copies of
BYTEFREE.BAS/ OBJ/ EXE available
for $7 plus $3 postage. You can either
send your cheque to SILICON CHIP
or call (02) 979 5644 and quote your
SC
credit card details.
K
ALEX
The UV People
ETCH TANKS
● Bubble Etch ● Circulating
So in this case, it would contain “C:”.
Looking back at the program, this
line takes the first character in the
COMMAND$ string, converts it to
upper case and then stores it in the
DRIVE$ string.
The interrupt we’re going to use also
accepts a variable to allow us to select
the drive we wish to analyse. This
is stored in DX. However, the way it
recognises the drives is 0 for A:, 1 for
B:, 2 for C: and so on.
Using the ASCII code we can simply take the drive letter, convert it to
ASCII and then subtract 64 from it.
This gives us the correct number for
each drive.
The following line then makes the
call to the interrupt.
Upon return, all four registers AX
to DX contain our wanted informa-
tion. These are then stored in long
integers (four bytes wide), as denoted
by the “&” symbol. The reason for
using long integers is that it makes
the following multiplication much
easier to handle.
Now all we have to do is to multiply the bytes per sector by the sectors
per cluster and the number of free
clusters to get our resulting free disc
space.
The drive size is found by multiplying the bytes per sector by the sectors
per cluster and the number of clusters
per drive. The number of bytes used
is simply just the drive size minus the
free disc space.
The final four lines of code print this
information on the screen.
Now there are bound to be cries from
people who only have QBasic. This
LIGHT BOXES
● Portuvee 4 ● Portuvee 6
● Dual Level
TRIMMER
● Ideal
PCB DRILL
● Toyo HiSpeed
MATERIALS
● PC Board: Riston, Dynachem
● 3M Label/Panel Stock
● Dynamark: Metal, Plastic
✸ AUSTRALIA’S NO.1 STOCKIST ✸
K
ALEX
40 Wallis Ave, East Ivanhoe 3079.
Phone (03) 9497 3422, Fax (03) 9499 2381
May 1994 75
|