Int 21H [1.0] Function 22H (34) Random write

Writes data from memory into a selected record in a file.

Call with:

AH = 22H

DS:DX = segment:offset of previously opened file control block

Returns:

AL = 00H if write successful

01H if disk full

02H if segment wrap, write canceled

Notes:

The record is written (logically, not necessarily physically) to the file from memory at the current disk transfer address, specified by the most recent call to Int 21H Function 1AH. If the size and location of the buffer are such that a segment overflow or wraparound would occur, the function fails with a return code of 02H.

The file location of the data to be written is determined by the combination of the relative-record field (offset 21H) and the record-size field (offset 0EH) of the FCB. The default record size is 128 bytes.

The current block field (offset 0CH) and current record field (offset 20H) are updated to agree with the relative-record field as a side effect of the function.

The relative-record field of the FCB is not incremented by this function; it is the responsibility of the application to update the FCB appropriately if it wishes to write successive records. Compare with Int 21H Function 28H, which can write multiple records with one function call and automatically increments the relative-record field.

If a record is written beyond the current end of file, the space between the old end of file and the new record is allocated but not initialized.

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

Example:

Open the file MYFILE.DAT, set the record length to 1024 bytes, write record number 4 into the file from the buffer named buff, then close the file.

myfcb db 0 ; drive = default

db 'MYFILE ' ; filename, 8 chars

db 'DAT' ; extension, 3 chars

db 25 dup (0) ; remainder of FCB

buff db 1024 dup (?) ; buffer for write

.

.

.

; open the file

mov ah,0fh ; function number

mov dx,seg myfcb ; address of FCB

mov ds,dx

mov dx,offset myfcb

int 21h ; transfer to MS-DOS

or al,al ; check status

jnz error ; jump if no file

; set DTA address

mov dx,offset buff ; buffer address

mov ah,1ah ; function number

int 21h ; transfer to MS-DOS

; set record size

mov word ptr myfcb+0eh,1024

; set record number

mov word ptr myfcb+21h,4

mov word ptr myfcb+23h,0

; write the record

mov ah,22h ; function number

mov dx,offset myfcb ; address of FCB

int 21h ; transfer to MS-DOS

or al,al ; check status

jnz error ; jump if write failed

; close the file

mov ah,10h ; function number

mov dx,offset myfcb ; address of FCB

int 21h ; transfer to MS-DOS

or al,al ; check status

jnz error ; jump if close failed

.

.

.