Tuesday, 21 July 2015

Dealing with Files in Assembly !

Each operating system has it’s own way of dealing with files. There are two primary ways to access files: file descriptors and file streams. File descriptors use a set of low-level I/O functions, and file streams are a higher-level form of buffered I/O that is built on the lower-level functions. Here,the focus will be on the low-level I/O functions that use file descriptors.In Unix and related computers operating systems, a file descriptor (fd, less frequently fildes) is an abstract indicator used to access a file or other input/output resource, such as a pipe or network connection.

The system considers any input or output data as stream of bytes. Linux programs usually have at least three open file descriptors when they begin.There are:
Standard input (stdin)
Standard output (stdout)
Standard error (stderr)


Standard input (stdin):This is the standard input. It is a read-only file, and usually represents your keyboard. This is always file descriptor 0.
Standard output (stdout): This is the standard output. It is a write-only file, and usually represents your screen display. This is always file descriptor 1.
Standard error (stderr): This is your standard error. It is a write-only file, and usually represents your creen display.  This is always file descriptor 1.

                                                          
                  

In our programs we will deal with files in the following ways:
1.Tell Linux the name of the file to open, and  in what mode you want it opened (read, write, both read and write, create it if it doesn’t exist, etc.). This is handled with the open system call, which takes a filename, a number representing the mode, and a  permission set as its parameters. %eax will hold the system call number, which is 5. The address of the first  character of the filename should be stored in %ebx. The read/write intentions, represented as a number, should be stored in %ecx.

2.Linux will then return to you a file descriptor in %eax. Remember, this is a number that you use to refer to this file throughout your program.

3.Next you will operate on the file doing reads and/or writes, each time giving Linux the file descriptor you want to use. read is system call 3, and to call it you need to have the file descriptor in %ebx, the address of a buffer for storing the data that is read in %ecx, and the size of the buffer in %edx.write is system call 4, and it requires the same parameters as the read system call, except that the buffer should already be filled with the data to write out. The write system call will give back the number of bytes written in %eax or an error code.

4.When you are through with your files, you can then tell Linux to close them. Afterwards, your file descriptor is no longer valid. This is done using close, system call 6. The only parameter to close is the file descriptor, which is placed in %ebx.

Creating and Opening a File:
For creating and opening a file, perform the following tasks:-
a.Put the system call sys_creat() number 8, in the EAX register.
b.Put the filename in the EBX register.
c.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.

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.
Now, Put the file descriptor in the EBX register.
Now, Put the pointer to the input buffer in the ECX register.
Now, Put the buffer size, i.e., the number of bytes to read, in the EDX 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.
Now,Put the file descriptor in the EBX register.
Now,Put the pointer to the output buffer in the ECX register.
Now, Put the buffer size, i.e., the number of bytes to write, in the EDX register.


Closing a File:
For closing a file, perform the following tasks:
Put the system call sys_close() number 6, in the EAX register.
Now, Put the file descriptor in the EBX register.
If you like this post or have any question, please feel free to comment!

No comments:

Post a Comment

Blogger Widget