[英]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
.我有我的代码的问题,我开发它当一个字符串等于它返回0,当它是不同的,它返回1.我测试我的代码与此字符串首先
bruno
和bruna
这个程序返回0,并将其与发生crazy
和craza
。 And when a try to test with the first string using bruna
and second string using bruno
my program returns 1.当尝试使用
bruna
测试第一个字符串和使用bruno
第二个字符串时,我的程序返回 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. slt
是“设置小于”指令。 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.我想你想要一个
subu
指令 - 只有当两个字符相等时,减法的结果才会为零。 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.或者,用
bne $t3, $t4, missmatch
替换那里的两行 - 如果您以后不打算使用它,则无需在 $t5 中放置值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.