找到你要的答案

Q:Read binary file into array in MIPS

Q:读二进制文件到阵列的MIPS

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

我有一个名叫trace.dat包含4字节的整数的文件。谁能告诉我如何打开和读取文件,并存储每个整数数组称为ARR(MIPS)?谢谢!

到目前为止我有这个:

li $v0, 13
la $a0, file    #file contains the file name
li $a1, 0
li $a2, 0
syscall
add $s0, $v0, $0
answer1: 回答1:

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.

您所打开的代码只打开该文件,而不读取该文件。为了读出一个文件你打开到一个缓冲区的内容,你需要使用系统调用号14,像这样:

li $v0, 14
move $a0, $s0
la $a1, arr
li $a2, 32
syscall
bltz $v0, error

这个代码假定$ S0包含打开的文件的文件描述符,这你已经在那里由于加$ S0,$ v0,0美元。它还假定ARR的大小是32字节。

如果你的文件大于32字节,你可以写一个循环直到系统调用14返回0或小于缓冲区的大小值。然后,可以处理从文件内读取的数据循环。

assembly  mips  file-read