Sunteți pe pagina 1din 12

Application level of input and output: file services of DOS

Pattern for file handling: create a new file/open an existing file read from/write to the file close the file Using the DOS functions to create or open a file, the name of the file must be specified. The file name is so called ASCIIZ string, i.e. string terminated by zero, e.g. File1 DB MyFile.asm,0 File2 DB C:\Student\Test.txt,0 When the file is created or opened, a file handle is assigned to the file. A file handle is a 16-bit numerical code identifying the file. In the further operations (reading, writing, closing), the file is identified using its file handle instead of its name. When a file is created, its attributes are set. File attributes are passed to the function in register CX: 15 5 4 3 2 1 0

For a normal file: CX = 0

archive directory volume label

1 = read-only system hidden

Assembly Language 10/ 1

If a file operation has been successful, then CF is set to 0. If an error occurs, then CF is set to 1 and AX contains a code indicating the cause of the error. Function number 3Ch Description Create a new file. If the file with a given name already exists, then that file will be destroyed. If the existing file is read-only, then an error occurs. Create a new file. If the file with a given name already exists, then an error will be reported but the file will not overwrite. Open a file. The file pointer is set to the beginning of the file (0). The file must exist. Input parameters DS:DX address of the file name CX file attributes Output parameters AX file handle

5Bh

DS:DX address of the file name CX file attributes DS:DX address of the file name AL access mode: 0 read 1 write 2 read/write BX file handle

AX file handle

3Dh

AX file handle

3Eh

Close a file.

Assembly Language 10/ 2

Function number 3Fh

Description

Input parameters

Output parameters

Read from a file. BX file handle AX CX number of bytes number of Data is read bytes read to be read byte-by-byte DS:DX address of starting at the position indicated a buffer in which to store the bytes read by the current value of the file pointer. Write to a file. BX file handle Data is written to CX number of bytes the file starting at to be written DS:DX address of the current position of the file a buffer containing data to be written pointer. BX file handle CX:DX number of bytes by which to move the file pointer AL move is relative to: 0 the beginning of the file 1 the current location of the file pointer 2 the end of the file
Assembly Language 10/ 3

40h

AX number of bytes writtten

42h

Set the file pointer to the position specified in CX:DX relative to the position given by AL.

Services of Windows 95 and the later Windows versions


Windows 95 and the later Windows versions implement functions of the application programming interface (API) Win32 and makes them available to application programs. Description of functions: win32.hlp Functions are all in files known as dynamic link libraries (.dll) and are loaded at run-time. In the assembly language program, the name of a function is declared as an external symbol of type proc. The library file import32.lib must be linked to the program (libraries provide information to the linker about functions imported from dynamic-link libraries).

File services of Windows


Services have more parameters than in DOS because: Windows 95 is multitasking operating system: more tasks may access to a file at the same time. Windows 95 enables asynchronous access to a file: a program starts reading/writing and then continues its work while the input/output operation runs in parallel. The name of a file is a null-terminated string. It may contain a disk specification and a path. If the file name is coded in ANSI => the name of a function is terminated by letter A in UNICODE => the name of a function is terminated by letter W
Assembly Language 10/ 4

File handle is a 32-bit number. Value NULL, which some parameters of the functions can take, corresponds to a null pointer and is implemented as a zero 32-bit offset. Value TRUE is implemented as 1, FALSE as 0. The return value is in register EAX. Function CreateFile creates a file and opens it for the desired type of access (reading and/or writing) or opens an existing file. returns file handle in register EAX. If the function fails, the return value is INVALID_HANDLE_VALUE (-1). HANDLE CreateFile( LPCTSTR lpFileName, // address (32-bit offset) of the name of the file DWORD dwDesiredAccess, // desired access (read/write) DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // address of security descriptor DWORD dwCreationDistribution, // how to create DWORD dwFlagsAndAttributes, // file attributes and flags HANDLE hTemplateFile // handle of file with attributes to copy );

Assembly Language 10/ 5

Parameter desired access (read/write) Value 0 Meaning Application wants to query file attributes (e.g. the time of the last change) without actually accessing the file. Specifies read access to the file. Specifies write access to the file. Read and write.

GENERIC_READ (80000000h) GENERIC_WRITE (40000000h) GENERIC_READ or GENERIC_WRITE Parameter share mode Value 0

Meaning Prevents the file from being shared.

FILE_SHARE_READ (1) Other open operations can be performed on the file for read access. FILE_SHARE_WRITE (2) FILE_SHARE_READ or FILE_SHARE_WRITE Other open operations can be performed on the file for write access. The file can be shared in the read and write mode.
Assembly Language 10/ 6

Parameter address of security descriptor points to a SECURITY_ATTRIBUTES structure that specifies access authority for the file and whether the returned handle is inherited when a new process is created (if yes, child processes can access to the file). If the file created has implicit security attributes (i.e. only the owner and the administrator can access to the file and the file handle is not inheritable), then this parameter should be set to NULL. Parameter how to create Value CREATE_NEW (1) Meaning Creates a new file. The function fails if the specified file already exists.

CREATE_ALWAYS (2) Creates a new file. The function overwrites the file if it exists. OPEN_EXISTING (3) OPEN_ALWAYS (4) Opens the file. The function fails if the file does not exist. Opens the file, if it exists. If the file does not exist, the function creates it.

TRUNCATE_EXISTING Opens the file. Once opened, the file (5) is truncated so that its size is zero bytes. The calling process must open the file with at least GENERIC_WRITE access. The function fails if the file does not exist.
Assembly Language 10/ 7

Parameter file attributes and flags File attributes are in the lower word of this parameter. Bit Meaning 0 1 2 4 5 7 FILE_ATTRIBUTE_READONLY (1) FILE_ATTRIBUTE_HIDDEN (2) FILE_ATTRIBUTE_SYSTEM (4) FILE_ATTRIBUTE_DIRECTORY (10h) FILE_ATTRIBUTE_ARCHIVE (20h) FILE_ATTRIBUTE_NORMAL (80h) FILE_ATTRIBUTE_TEMPORARY (100h) a temporary file. The operating system tries to store file data in memory and not to save them to a disk (due to a faster access). The application should delete the file when it does not need the file anymore.

11 FILE_ATTRIBUTE_COMPRESSED (800h) Any combination of the attributes is acceptable. Attribute FILE_ATTRIBUTE_NORMAL is valid only if used alone. Flags are in the upper word of this parameter. They define the way in which cache memory is used when accessing the file. Cache memory with write back policy is used by default.

Assembly Language 10/ 8

FILE_FLAG_DELETE_ON_CLOSE (04000000h) indicates that the operating system deletes the file immediately after it has been closed. Parameter handle of file with attributes to copy may be either file handle of an open file or value NULL. If a new file is created and the parameter is a file handle, then the function ignores the attributes passed in the previous parameter and attributes of this specified file are used instead. If the function opens an existing file, then this parameter is ignored.

Assembly Language 10/ 9

Function ReadFile reads data from a file, starting at the position indicated by the file pointer. After the read operation has been completed, the file pointer is adjusted by the number of bytes actually read. If the function succeeds, the return value is TRUE. If the function fails, the return value is FALSE. BOOL ReadFile( HANDLE hFile, // file handle LPVOID lpBuffer, // address of buffer that receives data DWORD nNumberOfBytesToRead, // number of bytes to read LPDWORD lpNumberOfBytesRead, // address of variable, which contains the number of bytes read LPOVERLAPPED lpOverlapped // address of structure used in an asynchronous access (in synchronous access, the value of this parameter is NULL) ); Function WriteFile writes data to a file, starting at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written. The return value and the parameters are similar as for ReadFile.

Assembly Language 10/ 10

Function CloseHandle closes a file. BOOL CloseHandle( HANDLE hObject // file handle ); Example Write a string stored in variable Text to a file. .386 .MODEL flat,Stdcall include win32.inc; constants definition .DATA FileName Text TextLength FileHandle BytesWritten .CODE EXTRN EXTRN EXTRN EXTRN DB 'WINFILE.TXT',0 DB 'Ahoj!' EQU $-Text DD ? DD ?

CreateFileA:PROC WriteFile:PROC CloseHandle:PROC ExitProcess:PROC create file FileName NULL FILE_ATTRIBUTE_NORMAL CREATE_ALWAYS NULL
Assembly Language 10/ 11

Start:; push push push push

push 0 push GENERIC_WRITE push offset FileName call CreateFileA mov FileHandle,eax; save file handle ; write push push push push push call Text to the file NULL offset BytesWritten TextLength offset Text FileHandle WriteFile

; close the file push FileHandle call CloseHandle ; terminate the application push NULL call ExitProcess END Start winfile.asm \tasm\bin\tasm32 /ml winfile \tasm\bin\tlink32 /c /Tpe /aa winfile,,,\tasm\lib\import32 /ml, /c ... case sensitive /Tpe ... .exe for Windows /aa ... Win32 is used
Assembly Language 10/ 12

S-ar putea să vă placă și