用MIPS汇编读取文件 [英] Reading files with MIPS assembly

查看:1201
本文介绍了用MIPS汇编读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序,读取一个.dat文件中对应于LED模拟器中显示的不同颜色的字符。 x = off,R = red等。我的问题是,我不知道我在做什么错误打开.dat文件。我环顾四周,尽我所能想到,但每次组装和运行,我得到一个在$ v0 -1表示一个错误。这是我打开/阅读/关闭文件的代码:

  .data 
fin:.asciizmaze1.dat #输入文件名
缓冲区:.asciiz

.text
#打开文件
li $ v0,13#系统调用打开文件
la $ a0,fin#board文件名
li $ a1,0#打开阅读
li $ a2,0
syscall#打开一个文件(文件描述符返回$ v0)
移动$ s6,$ v0#保存文件描述符

#读文件
li $ v0,14#系统调用读文件
移动$ a0,$ s6#文件描述符
la $ a1,缓冲区#要读取的缓冲区地址
li $ a2,1024#硬编码缓冲区长度
系统调用#从文件读取

#关闭文件
li $ v0,16#系统调用关闭文件
移动$ a0,$ s6#文件描述符关闭
syscall#关闭文件

文件maze1.dat i与MIPS程序在同一个目录中。任何帮助或建议,不胜感激。
解决方案唯一的问题是你的缓冲区只是一个空字符串,它只保留一个字节(空字节)。你应该使用 buffer:.space 1024 或者你需要的很多字节。一切似乎都很好。



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


I'm trying to write a program that reads in characters from a .dat file that correspond to different colors to be displayed in the LED simulator; x = off, R = red, etc. My problem is, I cannot figure out what I'm doing wrong with opening the .dat file. I've looked around and tried all I can think of, but each time I assemble and run, I get a -1 in $v0 signifying an error. Here's my code for opening/reading/closing the file:

.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

The file maze1.dat is in the same directory as the MIPS program. Any help or suggestions are greatly appreciated.

解决方案

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.

这篇关于用MIPS汇编读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆