MIPS:解析和修改字符串

问题描述 投票:0回答:1

我必须在MIPS中编写一个程序,我必须打开一个文件,其名称由用户输入并使用MARS模拟器进行解析。但是,由于syscall 8(从用户输入读取字符串)遵循C函数fgets的语义,我必须从字符串中删除换行符\ n字符才能尝试打开文件。

我能够解析字符串(这里我打印每个字符的ASCII码用于调试目的)。但是当我尝试使用该行将换行符更改为NULL字符时

sb $zero 0($t1)

火星在执行期间遇到异常:

“0x00400050处的运行时异常:地址超出范围0x00000000”

如果我注释掉这一行,程序运行正常并打印字符串的每个ascii代码。

.data

prompt : .asciiz "enter filename:\n"
lf : .asciiz "\n"
space : .asciiz " "

.text 

la $a0 prompt      # asking user for filename
jal print_string
jal read_string

la $t0 ($a0)   #copying the string address 
lb $t1 0($t0)  #loading the first  byte

parsing_string : #loop
beq $t1 0 remove_line_feed  # reaching the NULL character '\0'

move $a0  $t1   #printing the ascii code
li $v0 1 
syscall

la $a0 space  #printing a space
li $v0 4
syscall  

add $t0 $t0 1   # loading  
lbu $t1 ($t0)   # the next byte

j parsing_string

remove_line_feed :
sb $zero 0($t1)   # runtime exception if i uncomment  this line 
j end

end : 


li $v0 10     # syscall 10 : end program 
syscall



read_string :   #read a string in $a0

li $a1 100  # $a1 = maximum number of characters to read        
li $v0 8    # syscall 8  : fgets()
syscall 
jr $ra


print_string  :  #print string in $a0
li $v0 4      # syscall 4 : print string
syscall
jr $ra

我不明白我在这里做错了什么,在程序的这一点上,寄存器$ t1应该包含写入换行字符的地址。但是我可能误解了指令如何保存字节和加载字节实际工作。

非常感谢任何帮助或建议:)

assembly mips mars-simulator
1个回答
0
投票

在asm代码中

sb $zero 0($t1)   # runtime exception if i uncomment  this line 

您使用$t1作为要清除的字节的地址。

但事实并非如此。你用过

la $t0 ($a0)   #copying the string address 

你所有的访问都使用$t0

在你的parsing_string函数结束时,你读取地址$t0的字符并将其放入$t1

lbu $t1 ($t0)   # the next byte

然后你回到parsing_string,测试t1是否为0

beq $t1 0 remove_line_feed  # reaching the NULL character '\0'

remove_line_feed

remove_line_feed :
sb $zero 0($t1)   # runtime exception if i uncomment  this line 
j end

你试图在地址0+$t1写入零(你刚刚测试的等于零)。

并且模拟器正确地告诉您尝试在地址0处写入并生成错误。

如果您将代码更改为

remove_line_feed :
sb $zero 0($t0)   # runtime exception if i uncomment  this line 
j end

这将删除运行时错误。

但是,它不会使您的代码正确。你想要做的是清除\0之前的'\ n',你的代码应该是

remove_line_feed :
sb $zero -1($t0)    
j end

或者,您可以更改比较,而不是搜索字符串的最终\0,搜索10(ascii换行符)并将相同的char替换为0。

© www.soinside.com 2019 - 2024. All rights reserved.