Reputation: 130
I'm trying to use MIPS Syscall 13 to simply open a file so I can read in some strings and print them to the console, but the file descriptor keeps returning as -1 when I open the file. I've checked the file extensions and everything seems to be in order. There's a file of name "cards.dat" in the same directory as the source code. Here's my code. If Anyone could help, it would be appreciated.
.data
filename: .asciiz "cards.dat" #file name
textSpace: .space 1050 #space to store strings to be read
.text
main:
li $v0, 13 #open a file
li $a1, 0 # file flag (read)
la $a0, filename # load file name
add $a2, $zero, $zero # file mode (unused)
syscall
move $a0, $v0 # load file descriptor
li $v0, 14 #read from file
la $a1, textSpace # allocate space for the bytes loaded
li $a2, 1050 # number of bytes to be read
syscall
la $a0, textSpace # address of string to be printed
li $v0, 4 # print string
syscall
Upvotes: 2
Views: 18142
Reputation: 31
If you are giving MIPS a file name like you have in your code. The MIPS .jar must be in the same directory as the file; not your source code.
Upvotes: 3
Reputation: 22585
You have to ensure that the working directory is the one you are expecting, as you using relative paths. That is, cards.dat
needs to be in the working directory of your program.
The code seems just fine. Try using an absolute path in filename
if you know the exact location of the file you want to open.
E.g.:
filename: .asciiz "c:\\files\\cards.dat" #file name
Another thing you may experiment to get the working directory is to create a file within your code and then look into the filesystem where that file gets created... For that, use service 13 with $a1
set to 1 (write flag).
Also, don't forget to use service 16 to close the file handle after you have used it. It's one of those best practices you should definitely do.
Upvotes: 2