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.JdbcOdbc...