⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fdos.pas

📁 C,C++ To Delphi转换器 C,C++ To Delphi转换器
💻 PAS
字号:
Unit FDOS;

Interface

Const
 fmRead=0;
 fmWrite=1;
 fmReadWrite=2;

Function CreateFile(FileName:string):word;
Function OpenFile(FileName:string;Mode:byte):word;
Procedure CloseFile(Handle:word);
Function FileSize(Handle:word):longint;
function ReadFile(Handle:word;var data; size:word):word;
Function SeekFile(Handle:word; Pos:longint):boolean;
function WriteFile(Handle:word;var data; size:word):word;

Implementation


Function CreateFile(FileName:string):word; assembler;
 asm
{
    CREAT 哪 Create a file using handle (Func 3C)

 INT 21 - DOS 2+
     AH = 3Ch
     CX = attributes for file
         bit 0: read-only
         1: hidden
         2: system
         3: volume label (ignored)
         4: reserved, must be zero (directory)
         5: archive bit
         7: if set, file is shareable under Novell NetWare
     DS:DX -> ASCIZ filename (may include drive and path)

 Return: CF set on error
         AX = error code (03h,04h,05h) (see AH=59h)
     CF clear if successful
         AX = file handle

 Note:   if a file with the given name exists, it is truncated to zero
         length

 SeeAlso: AH=3Dh

 See relationship to bug in Int 21/AH=4E
}
  mov ah,3Ch  { create file}
  xor cx,cx
  push ds
   lds dx,filename

   mov bx,dx
   add bl,[bx]
   adc bh,0
   mov byte ptr [bx+1],0

   inc dx
   int 21h
  pop  ds
  jnc @open
   mov ax,0
  @open:
 end;

Function OpenFile(FileName:string;Mode:byte):word; assembler;
 asm
{   Open disk file using handle (Func 3D)

 INT 21 - DOS 2+
     AH = 3Dh
     AL = access mode
         00h read only
         01h write only
         02h read/write
     AL bits 7-3 = file-sharing modes (DOS 3+)
         bit 7    = inheritance flag, set for no inheritance
         bits 4-6 = sharing mode
               000 compatibility mode
               001 exclusive (deny all)
               010 write access denied (deny write)
               011 read access denied (deny read)
               100 full access permitted (deny none)
               111 used internally by SHARE
         bit 3    = reserved, should be zero
     DS:DX -> ASCIZ filename

 Return: CF set on error
         AX = error code (01h,02h,03h,04h,05h,0Ch) (see AH=59h)
     CF clear if successful
         AX = file handle

 Notes:  file pointer is set to start of file
     file handles which are inherited from a parent also inherit sharing
       and access restrictions
}
  mov ah,3Dh  { open file}
  mov al,Mode
  push ds
   lds dx,filename

   mov bx,dx
   add bl,[bx]
   adc bh,0
   mov byte ptr [bx+1],0

   inc dx
   int 21h
  pop  ds
  jnc @open
   mov ax,0
  @open:
 end;

Procedure CloseFile(Handle:word); assembler;
 asm
{
    Close a file using handle (Func 3E)

 INT 21 - DOS 2+
     AH = 3Eh
     BX = file handle

 Return: CF set on error
         AX = error code (06h) (see AH=59h)
     CF clear if successful

 Note:   if the file was written to, the time and date stamps are set to
       the current time
}
  mov bx,Handle
  or  bx,bx
  jz @done
   mov ah,3Eh
   int 21h
  @done:
 end;

Function SeekFile(Handle:word; Pos:longint):boolean; assembler;
 asm
{  LSEEK 哪 Move file read/write pointer (Func 42)
 INT 21 - DOS 2+
     AH = 42h
     AL = method
         00h offset from beginning of file
         01h offset from present location
         02h offset from end of file
     BX = file handle
     CX:DX = offset in bytes

 Return: CF set on error
         AX = error code (01h,06h) (see AH=59h)
     CF clear if successful
         DX:AX = new absolute offset from beginning of file
}
  mov ax,4200h
  mov bx,Handle
  mov dx,word(pos+0)
  mov cx,word(pos+2);
  int 21h
  mov ax,False
  jc @error
   mov ax,True
  @error:
 end;

Function FileSize(Handle:word):longint; assembler;
 asm
{ get current pos }
  mov ax,4201h
  mov bx,Handle
  xor cx,cx
  xor dx,dx
  int 21h

  push dx
  push ax

{ goto end of file = get file size }
  mov ax,4202h
  mov bx,Handle
  xor cx,cx
  xor dx,dx
  int 21h

{ return to last pos }
  mov bx,dx

  pop dx
  pop cx

  push bx
  push ax

  mov ax,4200h
  mov bx,Handle
  int 21h

{ get result }
  pop ax
  pop dx
 end;


function ReadFile(Handle:word;var data; size:word):word; assembler;
 asm
{   Read from file using handle (Func 3F)

 INT 21 - DOS 2+
     AH = 3Fh
     BX = file handle
     CX = number of bytes to read
     DS:DX -> buffer

 Return: CF set on error
         AX = error code (05h,06h) (see AH=59h)
     CF clear if successful
         AX = number of bytes read (0 if at EOF before call)

 Notes:  the returned AX may be smaller than the request in CX if a partial
       read occurred
     if reading from CON, read stops at first CR
}
  mov ah,3Fh
  mov bx,Handle
  mov cx,size
  push ds
   lds dx,data
   int 21h
  pop  ds
  jnc @done
   xor ax,ax
  @done:
 end;

function WriteFile(Handle:word;var data; size:word):word; assembler;
 asm
{    Write to file using handle (Func 40)

 INT 21 - DOS 2+
     AH = 40h
     BX = file handle
     CX = number of bytes to write
     DS:DX -> buffer

 Return: CF set on error
         AX = error code (05h,06h) (see AH=59h)
     CF clear if successful
         AX = number of bytes actually written

 Notes:  if CX is zero, no data is written, and the file is truncated or
       extended to the current position
     the usual cause for AX < CX on return is a full disk
}
  mov ah,40h
  mov bx,Handle
  mov cx,size
  push ds
   lds dx,data
   int 21h
  pop  ds
  jnc @done
   xor ax,ax
  @done:
 end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -