使用 MIPS 程序集读取文件

CJ *_*ter 6 io file mips mars-simulator

我正在尝试编写一个程序,该程序从 .dat 文件中读取字符,这些字符对应于要在 LED 模拟器中显示的不同颜色;x = 关闭,R = 红色,等等。我的问题是,我无法弄清楚打开 .dat 文件时我做错了什么。我环顾四周并尝试了所有我能想到的,但是每次我组装和运行时,我都会在 $v0 中得到一个 -1 表示错误。这是我打开/读取/关闭文件的代码:

.data  
fin: .asciiz "maze1.dat"      # filename for input
buffer: .asciiz ""

.text
#open a file for writing
li   $v0, 13       # system call for open file
la   $a0, fin      # board file name
li   $a1, 0        # Open for reading
li   $a2, 0
syscall            # open a file (file descriptor returned in $v0)
move $s6, $v0      # save the file descriptor 

#read from file
li   $v0, 14       # system call for read from file
move $a0, $s6      # file descriptor 
la   $a1, buffer   # address of buffer to which to read
li   $a2, 1024     # hardcoded buffer length
syscall            # read from file

# Close the file 
li   $v0, 16       # system call for close file
move $a0, $s6      # file descriptor to close
syscall            # close file
Run Code Online (Sandbox Code Playgroud)

文件 maze1.dat 与 MIPS 程序位于同一目录中。非常感谢任何帮助或建议。

Kiz*_*aru 5

唯一的问题是您的缓冲区只是一个空字符串,它只保留一个字节(空字节)。您应该改为使用buffer: .space 1024或使用您需要的字节数。其他一切似乎都很好。

如果您在打开文件时遇到问题,请确保扩展名完全正确。但是我的测试只处理了一个 .dat 文件和一些随机文本文件。