7

我正在尝试编写一个程序,该程序从 .dat 文件中读取对应于要在 LED 模拟器中显示的不同颜色的字符;x = off,R = red 等。我的问题是,我无法弄清楚打开 .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

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

4

3 回答 3

5

The only issue is your buffer is simply an empty string, which is only reserving one byte (null byte). You should instead use buffer: .space 1024 or however many bytes you need. Everything else seems fine.

If you are having trouble opening the file, make sure the extension is exactly correct. But my test just worked a .dat file and a few random text files.

于 2010-11-11T00:10:53.560 回答
2

确保您从文件所在的同一目录运行 MARS。只需将 MARS .jar 移动到包含“maze1.dat”的目录并从那里运行它。

于 2015-01-29T21:14:35.097 回答
1

我有同样的问题。特别是如果您使用的是 Linux,请尝试“./maze1.dat”。

于 2016-01-29T14:19:24.073 回答