<--- Turn the page     (contents page)     Turn the page --->


More Files

How to open more than 20 files in your app.



Not very often do we need to open more than 15 to 20 files at a time. However, if we do a lot of programming requiring a lot of file accessing, we might need to. DOS stores a handle number for each file opened in the current PSP. This location allows 20 entry's. However, DOS uses 5 of these entry's as stdin, stdout, stderr, stdaux, and stdprn. In DOS version before 3.3x, that leaves the programmer with 15 open entry's. This months column, I will discuss where this file handle array is and how to make it larger.

The programs PSP is at the first of the program. This PSP contains the file handle array, the size, and the address of this array. The size is at offset 32h and is 2 bytes in length. It will contain 20 as its value. The address is located in the PSP so that DOS can find the handle array. For now, it points to the 20 bytes at offset 18h. This address is a 4 byte segmented address at offset 34h.

You have probably thought of it by now, but all we have to do is allocate another buffer, change the size at offset 32h, and the address pointer at offset 34h.
However, there is one more thing. Each one of the byte entry's contains a value. If the value is FFh, it is an available handle. If it is other than FFh, it is a used handle and we must copy it to our new buffer. Rather than trying to pick out which are used and which aren't, let us just copy all 20 bytes to our buffer, and then set the remaining bytes in our buffer to FFh.

See Listing 1 below for an assembler routine to do this. This routine works in all DOS versions. However, DOS 3.3x included a new service numbered 67h that would do this for you. You put the amount of handles in BX and call this service allowing DOS to do the rest.

I didn't create this months column only to show you how to have a lot of files opened at once, but to show you more about the PSP. To see more on the PSP, see issue 1 of this e-mag. ¥



Listing 1
; Before DOS 3.3, there was no way to open more than 20 handles
;  at a time.  Also, DOS already occupied 5 of these handles.
; To open more than 15 handles in DOS before version 3.3x, try
;  the following code.
; To open more than 15 handles in DOS 3.3x and higher, use
;  service 67h of Interrupt 21h.
;
.model tiny
.code

           .start                       ; resize mem block and adjust stack
           mov  ah,48h                  ; allocate a new file array
           mov  bx,0016                 ; 16 para's (256 bytes)
           int  21h
           jnc  short MemOk
           mov  dx,offset MemErrS
           mov  ah,09h
           int  21h
           .exit

MemOk:     mov  word [0032h],256        ; new array size
           mov  word [0034h],00h        ; address of new array
           mov  [0036h],ax              ;

           push es                      ; copy old array to new array
           mov  es,ax                   ;
           xor  di,di                   ;
           mov  si,0018h                ;
           mov  cx,20                   ;
           rep                          ;
           movsb                        ;

           mov  al,0FFh                 ; now fill the remaining entry's
           mov  cx,236                  ;  with 0FFh to denote a free
           rep                          ;  handle.
           stosb
           pop  es                      ;

           ; from here on out, you have room for
           ; 256 handle including the five (5)
           ; DOS STDxx handles
           ; (stdin, stdout, stderr, stdaux, stdprn)

           ; DOS will still limit the amount of files
           ;  opened by the FILES=x in config.sys
           ; So to open all 256-5 files, set FILES=255
           ; in your config.sys file.

           .exit

MemErrS    db  13,10,'Error allocating memory.$'

.end


<--- Turn the page     (contents page)     Turn the page --->

Page 3