Reputation: 101
I've got a file named trace.dat
that contains 4 byte integers. Can anybody tell me how to open and read the file, and store each integer in an array called arr
(in MIPS)? Thanks!
So far I have this:
li $v0, 13
la $a0, file #file contains the file name
li $a1, 0
li $a2, 0
syscall
add $s0, $v0, $0
Upvotes: 1
Views: 2462
Reputation: 19286
The code you have merely opens the file, and does not read it. In order to actually read the contents of a file you've opened into a buffer, you need to use syscall number 14, like this :
li $v0, 14
move $a0, $s0
la $a1, arr
li $a2, 32
syscall
bltz $v0, error
This code assumes that $s0
contains the file descriptor of the opened file, which you already have in there due to add $s0, $v0, $0
. It also assumes that the size of arr
is 32 bytes.
If your file is larger than 32 bytes, you can write a loop that runs until syscall 14 returns 0 or a value smaller than the size of the buffer. Then, you can process the data read from the file inside the loop.
Upvotes: 3