I was trying to compare two strings because of my adding integers code which i posted here before. I could take string from user, also convert that to integer, but i have faced a problem during comparison of string (max size is 20 and if string is less than 20, there will be spaces) read from user and my character " * ". I thought that if i can compare them and they are not equal, i convert to integer and continue to adding, if they are equal it will exit from loop.
I wrote a simple code for comparing two strings, however that didn't give the result. Here is my code;
.data
str1: .asciiz "Comp"
str2: .asciiz "Comp"
.text
main:
la $s2, str1
la $s3, str2
move $s6, $s2
move $s7, $s3
li $s1, 5
beq $s6, $s7, exit
move $a0, $s1
li $v0, 1
syscall
exit:
li $v0, 10
syscall
After i have checked QtSpim's registers $s6 and $s7, i observed that there are different values. How can i compare two strings? Thank you.
I've adapted your program to prompt the user for strings, so you can try many values quickly. The
cmploop
is the "meat" of the string compare, so you can just use that if you wish.Here's it is [please pardon the gratuitous style cleanup]:
The comparison relates the pointers. There needs to be a dereferencing in there.
Also, there needs to be checks for end of string.