turbo-pascal-assembly

0499 Open Function

This is the Open(File) function. It assumes that File is already be bound to a filename using the Assign(File, Filename) function.

SYS0499: MOV BX,SP

Use BX to address items on the stack. Upon entry into this subroutine, the stack looks as follows:

Index Contents
BX Return Address (OFFSET)
BX+02 Return Address (SEGMENT)
BX+04 Pointer to File/Text Record Data (OFFSET)
BX+06 Pointer to File/Text Record Data (SEGMENT)
SYS049B: PUSH DS

Save DS as it will be modified later.

SYS049C: SS:
SYS049D: LDS DI,[BX+04]

Load pointer to File into DS:DI.

SYS04A0: XOR CX,CX
SYS04A2: MOV [DI:Handle],CX

Set File’s Handle to 0 (STDIN).

SYS04A4: MOV AX,3D00

Prepare to open File using DOS INT 21h AH = 3Dh service with parameters:

SYS04A7: CMP WORD PTR [DI:Mode],fmInput
SYS04AC: JZ 04BB

Check if File (Mode is to be opened using Reset().

SYS04AE: MOV AL,02
SYS04B0: INC WORD PTR [DI:Handle]

Otherwise, set access mode to read/write and set File’s Handle to 1 (STDOUT) by adding 1 [DI].

SYS04B2: CMP WORD PTR [DI:Mode],fmInOut
SYS04B7: JZ 04BB

Check if File (Mode) is to be opened using Reset() and Rewrite().

SYS04B9: MOV AH,3C

Prepare to create the file using DOS INT 21h AH = 3Ch service with parameters:

SYS04BB: CMP BYTE PTR [DI:Name],00
SYS04BF: JZ 04CA

If the filename is empty, there is no need to call the DOS service indicated in AH. The empty string indicates that this is either Input or Output.

SYS04C1: LEA DX,[DI:Name]
SYS04C4: INT 21

Load the pointer to the File’s Name into DS:DX. DS was already set in SYS:04A2 above. Call on the DOS service indicated in AH.

SYS04C6: JB 0522

On error, return immediately with a non-zero error code in AX (to be stored later in InOutRes).

SYS04C8: MOV [DI:Handle],AX

Upon success of the call to the DOS service, AX contains the handle of the openend / created file. Copy AX in File’s Handle.

SYS04CA: MOV AX,040B
SYS04CD: MOV DX,SYS
SYS04D0: XOR CX,CX
SYS04D2: XOR BX,BX
SYS04D4: CMP WORD PTR [DI:Mode],fmInput
SYS04D9: JZ 050A

Set File’s InOutFunc to SYS:040B Read Function then check if File was opened with Reset().

SYS04DB: MOV BX,[DI:Handle]
SYS04DD: MOV AX,4400
SYS04E0: INT 21

Get device information using DOS INT 21h AX = 4400h service with parameter BX = File handle. It returns device information in DX.

SYS04E2: TEST DL,80
SYS04E5: MOV AX,0460
SYS04E8: MOV DX,SYS
SYS04EB: MOV CX,AX
SYS04ED: MOV BX,DX
SYS04EF: JNZ 0505

If File is a character device (80h), set its InOutFunc and FlushFunc to SYS:0460 Write Function.

SYS04F1: CMP WORD PTR [DI:Mode],fmInOut
SYS04F6: JNZ 04FB
SYS04F8: CALL 0526

Check if File (Mode) was opened using Reset() and Rewrite() move file pointer to end of the file.

SYS04FB: MOV AX,043B
SYS04FE: MOV DX,SYS
SYS0501: XOR CX,CX
SYS0503: XOR BX,BX

Set File’s InOutFunc to SYS:43B Write to File Function.

SYS0505: MOV WORD PTR [DI:Mode],fmOutput

Mark the File as opened using Rewrite().

SYS050A: MOV [DI:InOutFunc.Offset],AX
SYS050D: MOV [DI:InOutFunc.Segment],DX
SYS0510: MOV [DI:FlushFunc.Offset],CX
SYS0513: MOV [DI:FlushFunc.Segment],BX

Set up the handlers for File’s InOutFunc and FlushFunc. DX:AX, BX:CX, at this point contains the correct pointers to the subroutines.

SYS0516: MOV WORD PTR [DI:CloseFunc.Offset],0480
SYS051B: MOV WORD PTR [DI:CloseFunc.Segment],SYS

Set up the handler for File’s CloseFunc to SYS:0480 Close Function.

SYS0520: XOR AX,AX

Set I/O result in InOutRes to 0 on success.

SYS0522: POP DS

Restores DS (Saved in SYS:049B).

SYS0523: RETF 0004

Return and pop-off parameter from the stack. The error code in AX, later stored in InOutRes.

See also: Text File Type, File Modes, SYS:040B Read Function, SYS:043B Write to File Function, SYS:0460 Write Function, SYS:0480 Close Function, SYS:0526 Open Function (II) or go back