当MIPS读取文件时,它读取最后一行两次 [英] When reading file in MIPS, it reads last line twice

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

问题描述

我可以(部分)成功地在MIP的文件读取。下面是我目前的code。在QtSpim,当我运行它,我得到一个指向$ A1文件,但该文件的最后几个字符重复两次。被重复根据文件改变的字符数。从我所看到的东西,它似乎被链接到新的行字符数的文件中,除非换行字符是在文件(意义的尽头,如果有5个新行字,文件的最后5个字符将在读取该文件)的结尾出现重复,但我看不出有任何理由为什么这应该是真实的。 (仅供参考,本code从这里除了复制几乎是逐字它读取,而不是写)

 。数据
鱼翅:.asciizC:/input.txt
fBuffer:1024。空间
。文本
主要:
    JAL中openFile
    JR $ RA#returns:指向文件的文本$ A1
打开文件:
    李$ V0,13#系统调用打开文件
    LA $ A0,鳍#fin是文件名
    李$ A1,0#0表示读
    李$ A2,0
    系统调用#打开文件
    此举$ S6,$#V0保存文件描述符    从文件#阅读
    李$ V0,14#系统调用从文件读取
    此举$ A0,$#S6文件描述符
    LA $ A1,fBuffer
    李$ A2,1024#硬codeD缓冲区长度
    系统调用#从文件中读取    #关闭文件
    李$ V0,16#系统调用关闭文件
    此举$ A0,$#S6文件描述符关闭
    系统调用#关闭文件
    JR $ RA


解决方案

您可以不知道重复这个code中的最后一行。你清楚了该链接的结果一栏写着文件读取 $ V0 包含读取的字节数。但是你的code则会覆盖立即 $ V0 来关闭文件。

如果你改变code仅打印实际读取的字符,重复信息的外观应该走开。

如果您使用的是打印字符串系统调用,那么只是一个字节添加到缓冲区(以prevent超限),然后写入字符后空终止读。是这样的:

从文件中读取

 系统调用#(你code)
LA $ A0,fBuffer#加载32位缓冲区地址
加$ A0,$ A0,$#V0文件数据后计算一个字节的地址
SB $零,0($ A0)#设置字节为零

I was able to (partly) successfully read in a file in MIPs. Below is my current code. In QtSpim, when I run it, I get a pointer to the file in $a1, but the last few characters of the file are repeated twice. The number of characters that is repeated changes depending on the file. From what I've seen, it seems to be linked to the number of new-line characters in the file unless the new-line chars are at the very end of the file (meaning, if there were 5 new-line characters, the last 5 characters of the file would appear duplicated at the end of the file read in), although I don't see any reason why this should be true. (FYI, this code is copied almost verbatim from here except it reads instead of writes)

.data
fin: .asciiz "c:/input.txt"
fBuffer: .space 1024
.text
main:
    jal  openFile
    jr   $ra

#returns: pointer to file's text in $a1
openFile:
    li   $v0, 13       # system call for open file 
    la   $a0, fin  #fin is the file name
    li   $a1, 0    # 0 means 'read'
    li   $a2, 0
    syscall            # open file
    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, fBuffer   
    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
    jr $ra

解决方案

You can't know the last line was repeated with this code. The link you gave clearly says in the Result column for file read that $v0 contains the number of bytes read. But your code immediately clobbers $v0 to close the file.

If you change your code to print only the characters actually read, the appearance of repeated information ought to go away.

If you are using the print string syscall, then just add one byte to the buffer (to prevent overrun) and then write a null terminator after the characters read. Something like:

syscall            # (your code) read from file 
la $a0, fBuffer    # load 32-bit buffer address
add $a0, $a0, $v0  # calculate address of byte after file data 
sb $zero, 0($a0)   # set that byte to zero

这篇关于当MIPS读取文件时,它读取最后一行两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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