Как вставить в программу на паскале код открытия, закрытия и т.д. процедур для работы с файлами.
Иначе: мне нужно что-то вроде исходников процедур Assign, reset и т.д.
--------
Что такое дескриптор и можно-ли как то их использовать в пасе?
Function FileOpen(FileName:PChar):Word;Assembler;
{Возвращает Handle или 0 при ошибке}
Asm
PUSH DS
LDS DX,[FileName]
MOV AX,3D02h {Read+Write}
INT 21h
JNC @@1
XOR AX,AX
@@1:
POP DS
End;
Function FileCreate(FileName:PChar):Word;Assembler;
{Возвращает Handle или 0 при ошибке}
Asm
PUSH DS
LDS DX,[FileName]
MOV AH,3Ch {Read+Write}
MOV CX,0020h {Attribute = Archive}
INT 21h
JNC @@1
XOR AX,AX
@@1:
POP DS
End;
Procedure FileClose(f:Word);Assembler;
Asm
MOV BX,[f]
MOV AH,3Eh
INT 21h
Ebd;
Function FileRead(f:Word;Var Buf;Count:Word):Word;Assembler;
Asm
PUSH DS
LDS DX,[Buf]
MOV CX,[Count]
MOV BX,[f]
MOV AH,3Fh
INT 21h
End;
Function FileWrite(f:Word;Const Buf;Count:Word):Word;Assembler;
Asm
PUSH DS
LDS DX,[Buf]
MOV CX,[Count]
MOV BX,[f]
MOV AH,40h
INT 21h
End;
[...] ;header stuff
.CODE ;this stuff is used for all the examples
FileName db "TextFile.TXT",0
FileHandle dw 0
Buffer db 300 dup (0)
BytesRead dw 0
FileSize dd 0
[...] ;more stuff
push ds
mov ax,3d00h ; open file for read only
mov ax,seg FileName
mov ds,ax ;we use CS, cause it's pointing to the CODE segment
; and our file name is in the code segment
mov dx,offset FileName
int 21h
jc FileError_Open
mov [FileHandle],ax
[...] ;etc...
mov bx,[FileHandle]
mov ah,3eh
int 21h
mov bx,[FileHandle]
mov ax, seg buffer
mov ds,ax
mov dx,offset buffer
mov ah,3Fh
mov cx,300
int 21h
mov [BytesRead],ax
mov bx,[FileHandle]
mov ax,seg buffer
mov ds,ax
mov dx,offset buffer
mov ah,40h
mov cx,[BytesRead]
int 21h
cmp cx,ax
jne FileError_Write
mov ah,3ch
mov ax,seg FileName
mov ds,ax
mov dx,offset FileName
mov cx,0 ;no attributes
int 21h
jc FileError_Create
mov [FileHandle],ax
mov ah,41h ;kill the sucker
mov ax,seg FileName
mov ds,ax
mov dx,offset FileName
int 21h
jc FileError_Delete
mov ah,42h ;НАХОЖДЕНИЕ РАЗМЕРА ФАЙЛА!!!
mov bx,[FileHandle]
xor cx,cx
xor dx,dx
mov al,2
int 21h
mov [word low FileSize],ax
mov [word high FileSize],dx;load data into filesize
[...]
mov ah,43h ;change attribute to that of a normal file
mov ax,cs
mov ds,ax
mov dx,offset FileName
mov al,1 ;set to whats in CX
mov cx,0 ;attribute = 0
int 21h
mov ah,41h ;Nuke it with the delete command
int 21h
; VERY, VERY simple ANSI/text viewer
;
; Coded by Draeden [VLA]
;
DOSSEG
.MODEL SMALL
.STACK 200h
.CODE
Ideal
;===- Data -===
BufferSeg dw 0
ErrMsgOpen db "Error opening `"
FileName db "ANSI.TXT",0,8,"'$" ;8 is a delete character
;0 is required for filename
;(displays a space)
FileLength dw 0
;===- Subroutines -===
PROC DisplayFile NEAR
push ds
mov ax,cs
mov ds,ax
mov ax,3d00h ;open file (ah=3dh)
mov dx,offset FileName
int 21h
jc OpenError
mov bx,ax ;move the file handle into bx
mov ds,[BufferSeg]
mov dx,0 ;load to [BufferSeg]:0000
mov ah,3fh
mov cx,0FFFFh ;try to read an entire segments worth
int 21h
mov [cs:FileLength],ax
mov ah,3eh
int 21h ;close the file
cld
mov si,0
mov cx,[cs:FileLength]
PrintLoop:
mov ah,2
lodsb
mov dl,al
int 21h ;print a character
dec cx
jne PrintLoop
pop ds
ret
OpenError:
mov ah,9
mov dx,offset ErrMsgOpen
int 21h
pop ds
ret
ENDP DisplayFile
;===- Main Program -===
START:
mov ax,cs
mov ds,ax
mov bx,ss
add bx,200h/10h ;get past the end of the file
mov [BufferSeg],bx ;store the buffer segment
call DisplayFile
mov ax,4c00h
int 21h
END START