Int 21H [3.0] Function 5BH (91) Create new file

Given an ASCIIZ pathname, creates a file in the designated or default directory on the designated or default drive, and returns a handle that can be used by the program for subsequent access to the file. If a file with the same name already exists, the function fails.

Call with:

AH = 5BH

CX = attribute (bits may be combined)

Bit(s) Significance (if set)

0 read-only

1 hidden

2 system

3 volume label

4 reserved (0)

5 archive

6—15 reserved (0)

DS:DX = segment:offset of ASCIIZ pathname

Returns:

If function successful

Carry flag = clear

AX = handle

If function unsuccessful

Carry flag = set

AX = error code

Notes:

The function fails if:

any element of the specified path does not exist.

a file with the identical pathname (i.e., the same filename and extension in the same location in the directory structure) already exists.

the file is being created in the root directory, and the root directory is full.

[3.0+] the program is running on a network, and the user has insufficient access rights to the directory that will contain the file.

The file is usually given a normal attribute (0) when it is created, and is opened for both read and write operations. The attribute can subsequently be modified with Int 21H Function 43H.

See also Int 21H Functions 3CH, 5AH, and 6CH, which provide alternative ways of creating files.

This function may be used to implement semaphores in a network or multitasking environment. If the function succeeds, the program has acquired the semaphore. To release the semaphore, the program simply deletes the file.

Example:

Create and open a file named MYFILE.DAT in the directory \MYDIR on drive C; MS-DOS returns an error if a file with the same name already exists in that location.

fname db 'C:\MYDIR\MYFILE.DAT',0

fhandle dw ? ; file handle

.

.

.

mov ah,5bh ; function number

xor cx,cx ; normal attribute

mov dx,seg fname ; filename address

mov ds,dx

mov dx,offset fname

int 21h ; transfer to MS-DOS

jc error ; jump if create failed

mov fhandle,ax ; save file handle

.

.

.