Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing and Reading Files Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames.

Similar presentations


Presentation on theme: "Writing and Reading Files Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames."— Presentation transcript:

1 Writing and Reading Files std@kmitnb.ac.th

2 Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames –Cannot address directories File Handles –Accessing of the file –Return codes to identity errors

3 Operations using File Handles Use INT 21H services –3CH : Create file –3DH : Open file –3EH : Close file –3FH : Read record –40H : Write record –42H : Move file pointer

4 Operations using File Handles Use other services –INT 25H : Absolute read –INT 26H : Absolute write

5 Operations using FCBs Use INT 21H services –0FH : Open file –10H : Close file –14H : Read record –15H : Write record –16H : Create file –21H : Read record randomly –22H : Write record randomly –27H : Read block randomly –28H : Write block randomly

6 ASCIIZ strings Containing the filespec : –the location of the disk drive –directory path –filename –‘All optional within apostrophes’ –a bytes of hex zeros –maximum length string is 128 bytes

7 ASCIIZ strings Example1 : defines a drive and filename –PATHNAM1 DB ‘D:\Filetest.asm’, 00H Example2 : defines a drive, subdirectory and filename –PATHNAM2 DB ‘E:\Utility\Filetest.exe’, 00H

8 File Handles Standard devices –00 = input –01 = output –02 = error output –03 = auxiliary device –04 = printer

9 Error Return Codes File handle operations for disk deliver a “completion status” via –Carry flag –Register AX Successful operation –clear carry flag & perform other functions Unsuccessful operation –set carry flag & return an error code in AX

10 Lists of error codes : 01-36 01 : Invalid function number 02 : File not found 03 : Path not found 04 : Too many file open 05 : access denied 06 : Invalid handle 07 : Memory control block destroyed 08 : Insufficient memory 09 : Invalid memory block address 10 : Invalid environment

11 Lists of error codes : 01-36 11 : Invalid format 12 : Invalid access code 13 : Invalid data 15 : Invalid drive specified 16 : Attempt to remove directory 17 : Not same device 18 : No more files 19 : Write-protected disk 20 : Unknown unit

12 Lists of error codes : 01-36 21 : Invalid function number 22 : Unknown command 23 : CRC data error 24 : Bad request structure length 25 : Seek error 26 : Unknown media type 27 : Sector not found 28 : Printer out of paper 29 : Write fault 30 : Read fault

13 Lists of error codes : 01-36 31 : General failure 32 : Sharing violation 33 : Lock violation 34 : Invalid disk change 35 : FCB unavailable 36 : Sharing buffer overflow

14 File Pointers ตัวชี้ที่ทำหน้าที่ชี้ตำแหน่งข้อมูลของไฟล์ เมื่อเปิดไฟล์ Pointer จะชี้ที่ไบท์แรกเสมอ การเคลื่อนย้ายข้อมูลในไฟล์ต้องย้าย file pointer ไปยังตำแหน่งที่ต้องการ โดยใช้ INT 21H function 42H

15 Using File handles to create disk files Procedure for writing a disk –Use an ASCIIZ string to get a file handle from the system –Use INT 21H function 3CH to create the file –Use INT 21H function 40H to write records in the file –Use INT 21H function 3EH to close the file

16 INT 21H Function 3CH : Create File AH = 3CH CX = File attribute DX = Address of the ASCII string

17 INT 21H Function 3CH : Create File File attribute –00H : Normal file –01H : Read only –02H : Hidden file –04H : System file –08H : Volume label –10H : Subdirectory –20H : Archive file

18 INT 21H Function 3CH : Create File Example –PATHNAM1DB‘E:\ACCOUNTS.FIL’, 00H –FILHAND1DW? –... – MOVAH,3CH – MOVCX,00 – LEADX,PATHNAM1 – INT21H – JCerror – MOVFILHAND1,AX

19 INT 21H Function 40H : Write Record AH = 40H BX = Stored file handle CX = Number of bytes to write DS:DX = Address of the output area

20 Example –FILHAND1DW? –OUTAREADB256 DUP(‘ ‘) –... – MOVAH,40H – MOVBX,FILHAND1 – MOVCX,256 – LEADX,OUTAREA – INT21H – JCerror2 – CMPAX,256 – JNEerror3 INT 21H Function 40H : Write Record

21 INT 21H Function 3EH : Close File AH = 3EH BX = File handle

22 Example –MOVAH,3EH – MOVBX,FILHAND1 – INT21H INT 21H Function 3EH : Close File

23 Using File handles to read disk files Procedure for reading a disk file –Use an ASCIIZ string to get a file handle from the system –Use INT 21H function 3DH to open the file –Use INT 21H function 3FH to read records from the file –Use INT 21H function 3EH to close the file

24 INT 21H Function 3DH : Open File AH = 3DH AL = Access code –00 : Read only –01 : Write only –02 : Read/Write DX = Address of the ASCII string

25 Example –FILHAND2DW? –... – MOVAH,3DH – MOVAL,00 – LEADX,PATHNAM1 – INT21H – JCerror4 – MOVFILHAND2,AX INT 21H Function 3DH : Open File

26 INT 21H Function 3FH : Read Record AH = 3FH BX = File handle CX = Number of bytes to read DS:DX = Address of the input area

27 Example –FILHAND2DW? –INAREADB512 DUP(‘ ‘) –... – MOVAH,3FH – MOVBX,FILHAND2 – MOVCX,512 – LEADX,INAREA – INT21H – JCerror5 – CMPAX,00 – JEendfile INT 21H Function 3FH : Read Record

28 INT 21H Function 42H : Move File Pointer AH = 42H BX = File handle CX : DX = Offset address required AL = Method code –00 : Take the offset from the start of the file –01 : Take the offset from the current location of the file pointer –02 : Take the offset from the end-of-file

29 Example –MOVAH,42H – MOVAL,00 – MOVBX,HANDLE1 – MOVCX,00 – MOVDX,1024 – INT21H – JCerror INT 21H Function 42H : Move File Pointer


Download ppt "Writing and Reading Files Methods for processing disk files File Control Blocks (FCBs) –Supported by DOS –Can address drives and filenames."

Similar presentations


Ads by Google