Mips,尝试将txt文件读入数组但始终获取最后一个元素

如何解决Mips,尝试将txt文件读入数组但始终获取最后一个元素

在我的项目中,我需要读取最多 10 个字符的字符串并将它们放入一个数组中。为了实现这一点,我逐字节读取每一行,如果得到“/n”行,则将整行添加到数组中并移至下一行。我可以将每个元素打印到控制台从数组中读取,然后将它们放入“add_to_array:”中的数组中,但问题是当我尝试在“read_done:”中访问数组的不同元素时,它总是给我数组的最后一个元素。

Ex input:
mert
file
edit
20637
format
view
help    
Ex output:
0mert
1file
2edit
320637
4format
5view
6help
help
Reading file is complete.
Size of Array: 7
-- program is finished running --

注意:我在“add_to_array:”中使用相应的索引打印它们。但是当我尝试打印“read_done:”中的任何元素时,它总是会打印“help”。

.data

buffer: .space 1    # 1 byte buffer in order to read the line byte by byte
line: .space 11     # Hard coded line size with max 11 byte long

array: .word 0:15   # An empty array of words with size of 1000
arraySize: .word 0

finName: .asciiz "D:/input.txt"


endl: .asciiz "\n"

soa : .asciiz "\nReading file is complete.\nSize of Array: "

foutInsertion:  .asciiz "insertion_sort_out.txt"      # filename for insertion sort output
foutSelection:  .asciiz "selection_sort_out.txt"      # filename for selection sort output
    
    
    .text
main:   # First we need to open txt file in order to read it
    la $s1 buffer
    la $s2 line
    li $s3 0            # current line length
    
      
    # Open file
    li $v0,13      # System call code = 13 to open file
    la $a0,finName     # Input file name
    li $a1,0       # Open for reading (flags are 0: read,1: write)
    syscall         # Open the file (file descriptor returned in $v0)
    move $s0,$v0        # Save the file descriptor to read 

read_line:      
    
    #sw $zero,buffer       
    # Read file byte by byte
    li $v0,14      # System call code = 14 to read file
    move $a0,$s0       # File descriptor
    la $a1,buffer      # The buffer that holds the byte
    la $a2,1       # Hardcoded buffer lenght
    syscall         # Read the byte and write into the buffer


    blez $v0 read_done  # keep reading until bytes read <= 0
        
                        
        # naively handle exceeding line size by exiting
    slti $t0,$s3,11
    beqz $t0,read_done

    lb $s4,($s1)
    li $t0 10           # If current byte is a EOL (end of line)
    beq $s4 $t0 add_to_array    # Then add line to array

    # otherwise,append byte to line
    add $s5 $s3 $s2
    sb $s4 ($s5)
    
    # increment line length
    addi $s3 $s3 1

     b read_line

add_to_array:
    lw  $t0,arraySize  # Load current array size from memory
    addi $t0,$t0,1    # Increment size of array by one
    sw $t0,arraySize   # Save uptaded array size to memory
    

    add $s5 $s3 $s2     # null terminate line
    sb $zero ($s5)

    li $s3 0        # reset bytes read
    

    lw $t0,arraySize   # Add line to array
    addi $t0,-1   # Get corresponsing index 
    
    li $v0,1
    move $a0,$t0
    syscall
    
    la $t1,array       # Load base address of array into $t1
    mul $t0,4     # Mul
    add $t1,$t1,$t0 
    sw $s2,0($t1)
    
    # print line (or consume it some other way)
    li $v0 4
    lw $a0 0($t1)
    syscall

    # print newline
    li $a0 10
    li $v0 11
    syscall

    b read_line
    
read_done:  
    
    li $v0 4
    la $t2,array
    #mul $t2,$t2,4    
    lw $a0 20($t2)
    syscall
   

    li $v0,4
    la $a0,soa
    syscall
    
    li $v0,1
    lw $a0,arraySize
    syscall

    
    li $v0,16
    move $a0,$s0
    syscall         # Close the file
    
    
        li $v0 10
        syscall         # Exit the program


    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?