Mips,将输入中的String与存储的String进行比较

时间:2016-06-05 04:15:55

标签: string assembly mips

我正在编写一个带有输入字符串并将其与存储字符串进行比较的函数,在这种情况下,我创建了一个.asciiz字符串,其值为" false"在其中,如果输入中的字符串等于" false"它会输出一条消息"它们匹配"和"他们不匹配"否则。

但我的代码不能很好地运作,我无法弄清楚原因。

.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

1 个答案:

答案 0 :(得分:1)

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

.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