简体   繁体   中英

Strcmp using assembly mips

I have problems with my code, I developed it for when a string was equal it returned 0 and when it is different it returns 1. I Tested my code with this strings firstly bruno and bruna and this program returns 0, and it occurs with crazy and craza . And when a try to test with the first string using bruna and second string using bruno my program returns 1.

.data
str1:               .space 32               # para reservar 32 caracteres para as strings
str2:               .space 32
msg1:               .asciiz "Insira a primeira string\n"
msg2:               .asciiz "Insira a segunda string\n"
.text

strcmp:
        li $v0,4
        la $a0,msg1
        syscall

        li $v0,8
        la $a0,str1
        addi $a1,$zero,32

        syscall

        li $v0,4
        la $a0,msg2
        syscall

        li $v0,8
        la $a0,str2
        addi $a1,$zero,32
        syscall   #got string 2

        la $a0,str1  #pass address of str1
        la $a1,str2  #pass address of str2
        jal strAux  #call strcmp

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

loop3:
        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 loop3

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

endfunction:    
        move $a0,$v0
        li  $v0,1                       # Opção para imprimir uma string
        syscall

slt is the "Set on Less Than" instruction. You detect a mismatch only if a character in the first string is less than the corresponding character in the second string - but in both of your failing examples, the non-equal character is greater in the first string. I think you want a subu instruction there - the result of subtraction will be zero only if the two characters are equal. Or, replace the two lines there with bne $t3, $t4, missmatch - there's no need to put a value in $t5 if you aren't going to use it later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM