100 likes | 253 Views
File I/O. I/O Flags. Flags are passed to give some information about how the file is to be used. Read only file – flag=0x0 Write only file – flag=0x1 Read and write – flag=0x2 Also tell other info by ORing above with Create – flag=0x100 Truncate – flag=0x200 Append – flag=0x8
E N D
I/O Flags • Flags are passed to give some information about how the file is to be used. • Read only file – flag=0x0 • Write only file – flag=0x1 • Read and write – flag=0x2 • Also tell other info by ORing above with • Create – flag=0x100 • Truncate – flag=0x200 • Append – flag=0x8 • And tell the type by ORing above with • Text – 0x4000 • Binary – 0x8000
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 $v0 code 13 • $a0 has address of string containing the filename (zero/null terminated with no LF). • $a1 flags • $a2 permissions • $v0 file descriptor for future use (-1 for error)
File Read • Use syscall code 14 • $a0 has the file descriptor (from the open) • $a1 has the address of the buffer of where to put the stuff read in (character string) • $a2 has the size of the buffer. • $v0 returns the count of how much data was read in (-1 for error, 0 for EOF)
File Write • Uses syscall code 15 • $a0 has the file descriptor • $a1 has the address of the buffer to write out (character string) • $a2 has the size of the buffer to write out • $v0 returns the amount actually written (should be the same as $a2 unless there was a problem)
File Close • Uses syscall code 16 • $a0 has the file descriptor • THAT’S IT
Review of Read String • Uses syscall code 8 • $a0 has the address of where to put the string (buffer) • $a1 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. • 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 in the lower byte of $a0 • Syscall 12 is read a character • $v0 contains the character in the lower byte • Read a character does not wait for the return