首先,我读取了整个文件并将其保存到名为buffer的数组中。接下来,我从缓冲区逐行获取字符串,然后保存到名为temp的数组中。打印临时数组后,然后我返回以将元素从缓冲区保存到临时。我在第35行收到一个错误,所以我认为出了点问题
#My D:\\source.txt file is:
#1+2+3
#4+3+4+5+6
#5+4
.data
fin: .asciiz "D:\\source.txt" # filename for input
buffer: .space 1024
temp: .space 100
newLine: .asciiz "\n"
.text
.globl main
main:
#open file
li $v0,13
la $a0,fin
li $a1,0
syscall
move $s0,$v0
#read file
li $v0,14
move $a0,$s0
la $a1,buffer
la $a2,1024
syscall
#handle
addi $t2,$0,0 #counter
addi $t3,$0,0
loop:
lb $t1,buffer($t2) #load an element in buffer to $t1
sb $t1,temp($t3) #save an element from $t1 to temp
add $t2,$t2,1 #counter++
add $t3,$t3,1 #counter++
addi $ra,$zero,35 #save command at line 35 to $ra
beq $t1,'\n',print #if $t2='\n', print temp
bne $t1,'\0',loop #if $t1 != '\0', loop
#print temp
print:
li $t3,0 #set $t3 as 0
li $v0, 4 #print temp
la $a0, temp
syscall
li $v0, 4 #print newLine
la $a0, newLine
syscall
jr $ra #run command at $ra
#close file
li $v0,16
move $a0,$s0
syscall
exit:
li $v0,10
syscall