Assembly 如何正确写入文件读取字符串?
我正在寻找一个程序,该程序读取一个文件,然后在存储的缓冲区中更改另一个文件的所有外观的特定字符,然后将修改后的缓冲区的副本写入另一个文件,该程序似乎运行良好,但它没有写入所需的内容,这就是我所做的:Assembly 如何正确写入文件读取字符串?,assembly,mips32,mars-simulator,Assembly,Mips32,Mars Simulator,我正在寻找一个程序,该程序读取一个文件,然后在存储的缓冲区中更改另一个文件的所有外观的特定字符,然后将修改后的缓冲区的副本写入另一个文件,该程序似乎运行良好,但它没有写入所需的内容,这就是我所做的: .data file: .asciiz "test.txt" fout: .asciiz "result.txt" buffer: .space 128 bsize: .word 127 msg: .asciiz "The program has been
.data
file: .asciiz "test.txt"
fout: .asciiz "result.txt"
buffer: .space 128
bsize: .word 127
msg: .asciiz "The program has been written properly"
char_to: .asciiz "n"
char_re: .asciiz "p"
#Macro for open a file for reading
.macro open_file_r()
li $v0, 13
la $a0 file
li $a1 0
li $a2 0
syscall
move $s0, $v0
.end_macro
.macro read_file()
li $v0, 14 #Llamada al sistema para leer archivo
move $a0, $s0
la $a1, buffer
lw $a2, bsize #Longitud del buffer
syscall
.end_macro
#Open a file for writing
.macro open_file_w()
li $v0, 13
la $a0 fout
li $a1 1
li $a2 0
syscall
move $s6, $v0
.end_macro
.macro write_file()
li $v0, 15 # system call for write to file
move $a0, $s6 # file descriptor
la $a1, ($t0) # address of buffer from which to write
li $a2, 127 # hardcoded buffer length
syscall # write to file
.end_macro
.macro close_file()
li $v0, 16
move $a0, $s0
syscall
.end_macro
.macro close_file_W()
li $v0, 16
move $a0, $s6
syscall
.end_macro
.macro exit()
li $v0 10
syscall
.end_macro
.text
main:
open_file_r()
read_file()
la $t0 buffer
la $t1, char_to
la $t2, char_re
addi $s1, $t1, 0
addi $s2, $t2, 0
loop:
lbu $t3, 0($t0)
addi $t0, $t0, 1
beq $t3 $s1 replace
beq $t3 $zero end
bne $t3 $s1 loop
replace:
sb $s2 -1($t0)
b loop
end:
close_file()
open_file_w()
write_file()
close_file_W()
exit()
文件中给出的输出(警告为已损坏)是:
\00\00\00程序已正确编写。\00n\00p\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00
那么,有谁能帮我看看代码中有什么错误吗?最明显的问题是,您没有保存
read\u文件的返回值
syscall,它应该是长度。(除非火星很奇怪?)你会想让你的写作知道要传递的长度。这可能比在数据中查找零要好。此外,在一些指令中,如beq$t3$s1 replace
;我没想到MARS会把它组合起来。@Peter Cordes MARS实际上是把这种表达式组合起来的,可以肯定的是,我在每个人身上都加了逗号,结果还是一样的,所以建议先确定长度,然后用字符串的长度作为写的输入,而不是写入整个缓冲区,包括它开始时的零。