strncmp issue
Mark Tinguely
tinguely at casselton.net
Thu Apr 30 17:06:02 UTC 2009
> Yes. We should get the following results:
>
> strncmp("a", "b", 0); 0
> strncmp("a", "b", *); < 0
>
> where * is any other number :)
>
> Warner
ENTRY(strncmp)
/* if (len == 0) return 0 */
cmp r2, #0
moveq r0, #0
RETeq
/* ip == last src address to compare */
add ip, r0, r2
1:
ldrb r2, [r0], #1
ldrb r3, [r1], #1
cmp ip, r0
- cmpcs r2, #1
+ beq 2f
+ cmp r2, #1
cmpcs r2, r3
beq 1b
+2:
sub r0, r2, r3
RET
also ip < r0 if r2 = (unsigned int) -1 using 32 bit math. so original
loop will terminate and compare the first characters. For example
strncmp("ab", "aa", -1) will result is 0.
I sent Olivier a couple patches, both wrong. Again, sorry for all the noise.
The above code, though, should work in all cases.
strncmp("b", "a", 0) result is 0
strncmp("abcdef", "abcdef", n) result is 0
strncmp("abcde", "abcdef", n) 0 if 0 <= n < 6 neg if n < 0 or n > 5
strncmp("abcdef", "abcde", n) 0 if 0 <= n < 6 pos if n < 0 or n > 5
The "beq" will break out of the loop if we give a count <= the string
length the first arguement.
The next cmp looks for the NULL byte, and the last cmp checks the characters
in the strings.
--Mark.
More information about the freebsd-arm
mailing list