File IO IO Flags Flags are passed to

  • Slides: 10
Download presentation
File I/O

File I/O

I/O Flags • Flags are passed to give some information about how the file

I/O Flags • Flags are passed to give some information about how the file is to be used. – Read only file – flag=0 x 0 – Write only file – flag=0 x 1 – Read and write – flag=0 x 2 • Also tell other info by ORing above with – Create – flag=0 x 100 – Truncate – flag=0 x 200 – Append – flag=0 x 8 • And tell the type by ORing above with – Text – 0 x 4000 – Binary – 0 x 8000

File Descriptor • This is a value (held in 4 bytes = 1 word)

File Descriptor • This is a value (held in 4 bytes = 1 word) that uniquely identifies the file. • It is provided when the file is opened. • This is used when accessing the file to identify the file. • The open uses the file name and the system assigns a file descriptor value to be used by the other file I/O commands

File Open • Use syscall with $v 0 code 13 • $a 0 has

File Open • Use syscall with $v 0 code 13 • $a 0 has address of string containing the filename (zero/null terminated with no LF). • $a 1 flags • $a 2 permissions • $v 0 file descriptor future use (-1 for error)

File Read • Use syscall code 14 • $a 0 has the file descriptor

File Read • Use syscall code 14 • $a 0 has the file descriptor (from the open) • $a 1 has the address of the buffer of where to put the stuff read in (character string) • $a 2 has the size of the buffer. • $v 0 returns the count of how much data was read in (-1 for error, 0 for EOF)

File Write • Uses syscall code 15 • $a 0 has the file descriptor

File Write • Uses syscall code 15 • $a 0 has the file descriptor • $a 1 has the address of the buffer to write out (character string) • $a 2 has the size of the buffer to write out • $v 0 returns the amount actually written (should be the same as $a 2 unless there was a problem)

File Close • Uses syscall code 16 • $a 0 has the file descriptor

File Close • Uses syscall code 16 • $a 0 has the file descriptor • THAT’S IT

Review of Read String • Uses syscall code 8 • $a 0 has the

Review of Read String • Uses syscall code 8 • $a 0 has the address of where to put the string (buffer) • $a 1 has the size of the buffer • The input is read up the return character and a LF is put at the end of the string along with the null character (after the LF). • Some applications don’t want the LF so it needs to be removed.

ASCII • All the file I/O commands use the address of a buffer. •

ASCII • All the file I/O commands use the address of a buffer. • This I/O is all done without conversion to ASCII characters. – You can write out binary numbers by placing them in the buffer, however these values will not be displayable on the screen (much like executable files). • To write numbers to a file that you can see, you must convert to ASCII • However, you can write numbers to a file in binary and have another program read them (not a human).

Character I/O • Syscall code 11 is print a character – The character is

Character I/O • Syscall code 11 is print a character – The character is in the lower byte of $a 0 • Syscall 12 is read a character – $v 0 contains the character in the lower byte – Read a character does not wait for the return