2010-11-10 86 views
3

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

.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计划。任何帮助或建议非常感谢。

回答

3

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

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

0

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

0

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