.file "strrchr.s"
/
/ strrchr(sp, c)
/
/ Returns the pointer in sp at which the character c last
/ appears; NULL if no found
/
/ Fast assembly language version of the following C-program strrchr
/ which represents the `standard' for the C-library.
/
/ char *
/ strrchr(const char *sp, int c)
/ {
/ char *r = NULL;
/
/ do {
/ if (*sp == (char)c)
/ r = (char *)sp;
/ } while (*sp++);
/
/ return (r);
/ }
/
#include "SYS.h"
ENTRY(strrchr)
pushl %edi / save register variable
movl 8(%esp), %eax / %eax = string address
movb 12(%esp), %cl / %cl = char sought
movl $0, %edi / %edi = NULL (current occurrence)
testl $3, %eax / if %eax not word aligned
jnz .L1 / goto .L1
.align 4
.L3:
movl (%eax), %edx / move 1 word from (%eax) to %edx
cmpb %cl, %dl / if the fist byte is not %cl
jne .L4 / goto .L4
movl %eax, %edi / save this address to %edi
.L4:
cmpb $0, %dl / if a null termination
je .L8 / goto .L8
cmpb %cl, %dh / if the second byte is not %cl
jne .L5 / goto .L5
leal 1(%eax), %edi / save this address to %edi
.L5:
cmpb $0, %dh / if a null termination
je .L8 / goto .L8
shrl $16, %edx / right shift 16-bit
cmpb %cl, %dl / if the third byte is not %cl
jne .L6 / goto .L6
leal 2(%eax), %edi / save this address to %edi
.L6:
cmpb $0, %dl / if a null termination
je .L8 / goto .L8
cmpb %cl, %dh / if the fourth byte is not %cl
jne .L7 / goto .L7
leal 3(%eax), %edi / save this address to %edi
.L7:
cmpb $0, %dh / if a null termination
je .L8 / goto .L8
addl $4, %eax / next word
jmp .L3 / goto .L3
.align 4
.L1:
movb (%eax), %dl / move 1 byte from (%eax) to %dl
cmpb %cl, %dl / if %dl is not %cl
jne .L2 / goto .L2
movl %eax, %edi / save this address to %edi
.L2:
cmpb $0, %dl / if a null termination
je .L8 / goto .L8
incl %eax / next byte
testl $3, %eax / if %eax not word aligned
jnz .L1 / goto .L1
jmp .L3 / goto .L3 (word aligned)
.align 4
.L8:
movl %edi, %eax / %edi points to the last occurrence or NULL
popl %edi / restore register variable
ret
SET_SIZE(strrchr)