ALT by Bilal Ahmed Shaik - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

img195.png

 

 

  Assembly File Management

 

  The system considers any input or output data as stream of bytes. There are three standard file streams:

 

   Standard input (stdin)

 

   Standard output (stdout)

 

   Standard error (stderr)

 

  File Descriptor

 

  A file descriptor is a 16-bit integer assigned to a file as a file id. When a new file is created, or an existing file is opened, the file descriptor is used for accessing the file.

 

  File descriptor of the standard file streams - stdin, stdout and stderr are 0, 1 and 2 respectively.

 

  File Pointer

 

  A file pointer specifies the location for a subsequent read/write operation in the file in terms of bytes. Each file is considered as a sequence of bytes. Each open file is associated with a file pointer that specifies an offset in bytes, relative to the beginning of the file. When a file is opened, the file pointer is set to zero.

 

  File Handling System Calls

 

  The following table briefly describes the system cal s related to file handling:

 

img196.png

img197.png

  The steps required for using the system cal s are same, as we discussed earlier:

 

   Put the system call number in the EAX register.

   Store the arguments to the system call in the registers EBX, ECX, etc.

   Call the relevant interrupt (80h)

   The result is usualy returned in the EAX register

 

  Creating and Opening a File

 

  For creating and opening a file, perform the following tasks:

   Put the system call sys_creat() number 8, in the EAX register

   Put the filename in the EBX register

   Put the file permissions in the ECX register

 

  The system call returns the file descriptor of the created file in the EAX register, in case of error, the error code is in the EAX register.

 

  Opening an Existing File

 

  For opening an existing file, perform the following tasks:

   Put the system call sys_open() number 5, in the EAX register

   Put the filename in the EBX register

   Put the file access mode in the ECX register

   Put the file permissions in the EDX register

 

  The system call returns the file descriptor of the created file in the EAX register, in case of error, the error code is in the EAX register.

 

  Among the file access modes, most commonly used are: read-only (0), write-only (1), and read-write (2).

 

  Reading from a File

 

  For reading from a file, perform the following tasks:

   Put the system call sys_read() number 3, in the EAX register

   Put the file descriptor in the EBX register

   Put the pointer to the input buffer in the ECX register

   Put the buffer size, i.e., the number of bytes to read, in the EDX register

 

  The system cal returns the number of bytes read in the EAX register, in case of error, the error code is in the EAX

  register.

 

  Writing to a File

 

  For writing to a file, perform the following tasks:

 

   Put the system call sys_write() number 4, in the EAX register

 Put the file descriptor in the EBX register

   Put the pointer to the output buffer in the ECX register

   Put the buffer size, i.e., the number of bytes to write, in the EDX register

 

  The system cal returns the actual number of bytes written in the EAX register, in case of error, the error code is in the EAX register.

 

  Closing a File

 

  For closing a file, perform the following tasks:

   Put the system call sys_close() number 6, in the EAX register

   Put the file descriptor in the EBX register

 

  The system cal returns, in case of error, the error code in the EAX register.

 

  Updating a File

 

  For updating a file, perform the following tasks:

 

   Put the system call sys_lseek () number 19, in the EAX register

   Put the file descriptor in the EBX register

   Put the offset value in the ECX register

   Put the reference position for the offset in the EDX register

 

  The reference position could be:

 

   Beginning of file - value 0

   Current position - value 1

   End of file - value 2

 

  The system calL returns, in case of error, the error code in the EAX register.

 

  Example:

 

  The following program creates and open a file named myfile.txt, and writes a text 'Welcome to Tutorials Point' in this file. Next the program reads from the file and stores the data into a buffer named info. Lastly it displays the text as stored in info.

 

img198.png

img199.png

img200.png