X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/7b00c0c43f52e9d27168e67a26aac19065cdb40c..a28bf75d63c6a64e4c3b417c6052e45f42c6cedd:/arm/string/strchr.s diff --git a/arm/string/strchr.s b/arm/string/strchr.s index 58981cb..229db7b 100644 --- a/arm/string/strchr.s +++ b/arm/string/strchr.s @@ -20,4 +20,42 @@ * * @APPLE_LICENSE_HEADER_END@ */ + +// char * strchr(const char *s, int c); +// +// The strchr( ) function locates the first occurence of c (converted to a +// char) in the string s. If c is '\0', the function locates the terminating +// '\0' byte. +.text +.syntax unified +.code 32 +.globl _strchr + +.align 3 +_strchr: +// Setup a frame and convert c to a char. + push {r7,lr} + mov r7, sp + and r1, r1, #0xff + +// Basic character-by-character search for c; on each pass through the loop +// we load a byte from s and increment s. We then check if this byte is equal +// to either c or to '\0'; if not, we continue the loop. +0: ldrb r2, [r0],#1 + cmp r2, r1 + tstne r2, r2 + bne 0b + +// Either we have found a match, or we have reached the end of the string (or +// both, if c == '\0'). Because we incremented s after loading the last +// character, we need to decrement the pointer by one to get a pointer to +// either the matching or terminating character. + sub r0, r0, #1 + +// Now compare the last character we loaded to the character that we're +// searching for. If they don't match, the character was not found in the +// string, so return NULL. + cmp r2, r1 + movne r0, #0 + pop {r7,pc}