Mips,将输入的字符串与存储的字符串进行比较

Mips , comparing a String taked in input with a stored String

我正在编写一个函数,它接受一个输入字符串并将其与存储的字符串进行比较,在这种情况下,我创建了一个 .asciiz 字符串,其中包含值 "false",如果输入的字符串等于 "false" 它将输出消息 "they match" 否则 "they dont match"。

但是我的代码运行不正常,我不知道为什么。

.data


savedString:  .asciiz "false"

write: .asciiz "write the string to compare:"

match: .asciiz "they match"

nonmatch: .asciiz "they dont match"

string: .space 256



.text

main:

writeSTring:

li $v0,4
la $a0,write
syscall

la $a0, string
li $a1, 256
li $v0, 8
syscall


checkVisibility:

li $t3,0

la $a0,savedString       # adress stringsaved and writed string            
la $a1,string         


jal match

## if they match

beq $v0,$zero,match_msg

## else 

j noMatch_msg


match:

add $t0,$zero,$zero  
add $t1,$zero,$a0  
add $t2,$zero,$a1  


loop:  


lb $t3($t1)         #load a byte from each string  
lb $t4($t2)  
beqz $t3,checkt    #str1 end  
beqz $t4,nonEqual  
slt $t5,$t3,$t4     #compare two bytes  
bnez $t5,nonEqual 
addi $t1,$t1,1      #t1 points to the next byte of str1  
addi $t2,$t2,1  
j loop 

nonEqual:   

addi $v0,$zero,1  
j end

checkt:  

bnez $t4,missmatch  
add $v0,$zero,$zero  

end:  

jr $ra



match_msg:

la $a0,match_msg     
li $v0,4
syscall

j exit

noMatch_msg:

la $a0,nonmatch     
li $v0,4
syscall

j exit


exit:

li $v0,10 # exit
syscall

您的程序无法编译。我做了一些更改并添加了一些代码,现在我认为该程序可以满足您的要求。

.data  
string: .space 20  

write:.asciiz "write the string to compare:"  
match:.asciiz "\nthey match"
nonmatch:.asciiz "\nthey dont match"  
strToCompare: .asciiz "false\n"

.text
.globl main

main:  
    li $v0,4        #loads msg1  
    la $a0,write  
    syscall

    li $v0,8
    la $a0,string
    addi $a1,$zero,20
    syscall          #got string to manipulate

    li $v0,8
    la $a0,match
    addi $a1,$zero,20

    la $a0,string             #pass address of str1  
    la $a1,strToCompare         #pass address of str2  
    jal methodComp      #call methodComp  

    beq $v0,$zero,ok    #check result  
    li $v0,4
    la $a0,nonmatch
    syscall
    j exit
ok:  
    li $v0,4  
    la $a0,match  
    syscall  
exit:  
    li $v0,10  
    syscall  

methodComp:  
    add $t0,$zero,$zero  
    add $t1,$zero,$a0  
    add $t2,$zero,$a1  

loop:  
    lb $t3($t1)         #load a byte from each string  
    lb $t4($t2)  
    beqz $t3,checkt2    #str1 end  
    beqz $t4,missmatch  
    slt $t5,$t3,$t4     #compare two bytes  
    bnez $t5,missmatch  
    addi $t1,$t1,1      #t1 points to the next byte of str1  
    addi $t2,$t2,1  
    j loop  

missmatch:   
    addi $v0,$zero,1  
    j endfunction  
checkt2:  
    bnez $t4,missmatch  
    add $v0,$zero,$zero  

endfunction:  
    jr $ra