Int 21H [2.0] Function 40H (64) Write file or device

Given a valid file handle from a previous open or create operation, a buffer address, and a length in bytes, transfers data from the buffer into the file and then updates the file pointer position.

Call with:

AH = 40H

BX = handle

CX = number of bytes to write

DS:DX = segment:offset of buffer

Returns:

If function successful

Carry flag = clear

AX = bytes transferred

If function unsuccessful

Carry flag = set

AX = error code

Notes:

If the carry flag is returned clear but AX < CX, then a partial record was written or there is an error. This can be caused by a Ctrl-Z (1AH) embedded in the data if the destination is a character device in cooked mode or by a disk full condition if the destination is a file.

If the function is called with CX = 0, the file is truncated or extended to the current file pointer position.

[3.0+] If the program is running on a network, the user must have Write access rights to the directory and file.

Example:

Using the handle from a previous open or create operation, write 1024 bytes to disk at the current file pointer from the buffer named buff.

buff db 1024 dup (?) ; buffer for write

fhandle dw ? ; contains file handle

.

.

.

mov ah,40h ; function number

mov dx,seg buff ; buffer address

mov ds,dx

mov dx,offset buff

mov bx,fhandle ; file handle

mov cx,1024 ; length to write

int 21h ; transfer to MS-DOS

jc error ; jump, write failed

cmp ax,1024 ; entire record written?

jne error ; no, jump

.

.

.