1*5d9d9091SRichard Lowe/* 2*5d9d9091SRichard Lowe * CDDL HEADER START 3*5d9d9091SRichard Lowe * 4*5d9d9091SRichard Lowe * The contents of this file are subject to the terms of the 5*5d9d9091SRichard Lowe * Common Development and Distribution License (the "License"). 6*5d9d9091SRichard Lowe * You may not use this file except in compliance with the License. 7*5d9d9091SRichard Lowe * 8*5d9d9091SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*5d9d9091SRichard Lowe * or http://www.opensolaris.org/os/licensing. 10*5d9d9091SRichard Lowe * See the License for the specific language governing permissions 11*5d9d9091SRichard Lowe * and limitations under the License. 12*5d9d9091SRichard Lowe * 13*5d9d9091SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each 14*5d9d9091SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*5d9d9091SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the 16*5d9d9091SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying 17*5d9d9091SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner] 18*5d9d9091SRichard Lowe * 19*5d9d9091SRichard Lowe * CDDL HEADER END 20*5d9d9091SRichard Lowe */ 21*5d9d9091SRichard Lowe/* 22*5d9d9091SRichard Lowe * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 23*5d9d9091SRichard Lowe * Use is subject to license terms. 24*5d9d9091SRichard Lowe */ 25*5d9d9091SRichard Lowe 26*5d9d9091SRichard Lowe .file "strrchr.s" 27*5d9d9091SRichard Lowe 28*5d9d9091SRichard Lowe/ 29*5d9d9091SRichard Lowe/ strrchr(sp, c) 30*5d9d9091SRichard Lowe/ 31*5d9d9091SRichard Lowe/ Returns the pointer in sp at which the character c last 32*5d9d9091SRichard Lowe/ appears; NULL if no found 33*5d9d9091SRichard Lowe/ 34*5d9d9091SRichard Lowe/ Fast assembly language version of the following C-program strrchr 35*5d9d9091SRichard Lowe/ which represents the `standard' for the C-library. 36*5d9d9091SRichard Lowe/ 37*5d9d9091SRichard Lowe/ char * 38*5d9d9091SRichard Lowe/ strrchr(const char *sp, int c) 39*5d9d9091SRichard Lowe/ { 40*5d9d9091SRichard Lowe/ char *r = NULL; 41*5d9d9091SRichard Lowe/ 42*5d9d9091SRichard Lowe/ do { 43*5d9d9091SRichard Lowe/ if (*sp == (char)c) 44*5d9d9091SRichard Lowe/ r = (char *)sp; 45*5d9d9091SRichard Lowe/ } while (*sp++); 46*5d9d9091SRichard Lowe/ 47*5d9d9091SRichard Lowe/ return (r); 48*5d9d9091SRichard Lowe/ } 49*5d9d9091SRichard Lowe/ 50*5d9d9091SRichard Lowe 51*5d9d9091SRichard Lowe#include "SYS.h" 52*5d9d9091SRichard Lowe 53*5d9d9091SRichard Lowe ENTRY(strrchr) 54*5d9d9091SRichard Lowe pushl %edi / save register variable 55*5d9d9091SRichard Lowe movl 8(%esp), %eax / %eax = string address 56*5d9d9091SRichard Lowe movb 12(%esp), %cl / %cl = char sought 57*5d9d9091SRichard Lowe movl $0, %edi / %edi = NULL (current occurrence) 58*5d9d9091SRichard Lowe 59*5d9d9091SRichard Lowe testl $3, %eax / if %eax not word aligned 60*5d9d9091SRichard Lowe jnz .L1 / goto .L1 61*5d9d9091SRichard Lowe .align 4 62*5d9d9091SRichard Lowe.L3: 63*5d9d9091SRichard Lowe movl (%eax), %edx / move 1 word from (%eax) to %edx 64*5d9d9091SRichard Lowe cmpb %cl, %dl / if the fist byte is not %cl 65*5d9d9091SRichard Lowe jne .L4 / goto .L4 66*5d9d9091SRichard Lowe movl %eax, %edi / save this address to %edi 67*5d9d9091SRichard Lowe.L4: 68*5d9d9091SRichard Lowe cmpb $0, %dl / if a null termination 69*5d9d9091SRichard Lowe je .L8 / goto .L8 70*5d9d9091SRichard Lowe 71*5d9d9091SRichard Lowe cmpb %cl, %dh / if the second byte is not %cl 72*5d9d9091SRichard Lowe jne .L5 / goto .L5 73*5d9d9091SRichard Lowe leal 1(%eax), %edi / save this address to %edi 74*5d9d9091SRichard Lowe.L5: 75*5d9d9091SRichard Lowe cmpb $0, %dh / if a null termination 76*5d9d9091SRichard Lowe je .L8 / goto .L8 77*5d9d9091SRichard Lowe 78*5d9d9091SRichard Lowe shrl $16, %edx / right shift 16-bit 79*5d9d9091SRichard Lowe cmpb %cl, %dl / if the third byte is not %cl 80*5d9d9091SRichard Lowe jne .L6 / goto .L6 81*5d9d9091SRichard Lowe leal 2(%eax), %edi / save this address to %edi 82*5d9d9091SRichard Lowe.L6: 83*5d9d9091SRichard Lowe cmpb $0, %dl / if a null termination 84*5d9d9091SRichard Lowe je .L8 / goto .L8 85*5d9d9091SRichard Lowe 86*5d9d9091SRichard Lowe cmpb %cl, %dh / if the fourth byte is not %cl 87*5d9d9091SRichard Lowe jne .L7 / goto .L7 88*5d9d9091SRichard Lowe leal 3(%eax), %edi / save this address to %edi 89*5d9d9091SRichard Lowe.L7: 90*5d9d9091SRichard Lowe cmpb $0, %dh / if a null termination 91*5d9d9091SRichard Lowe je .L8 / goto .L8 92*5d9d9091SRichard Lowe 93*5d9d9091SRichard Lowe addl $4, %eax / next word 94*5d9d9091SRichard Lowe jmp .L3 / goto .L3 95*5d9d9091SRichard Lowe .align 4 96*5d9d9091SRichard Lowe.L1: 97*5d9d9091SRichard Lowe movb (%eax), %dl / move 1 byte from (%eax) to %dl 98*5d9d9091SRichard Lowe cmpb %cl, %dl / if %dl is not %cl 99*5d9d9091SRichard Lowe jne .L2 / goto .L2 100*5d9d9091SRichard Lowe movl %eax, %edi / save this address to %edi 101*5d9d9091SRichard Lowe.L2: 102*5d9d9091SRichard Lowe cmpb $0, %dl / if a null termination 103*5d9d9091SRichard Lowe je .L8 / goto .L8 104*5d9d9091SRichard Lowe 105*5d9d9091SRichard Lowe incl %eax / next byte 106*5d9d9091SRichard Lowe testl $3, %eax / if %eax not word aligned 107*5d9d9091SRichard Lowe jnz .L1 / goto .L1 108*5d9d9091SRichard Lowe jmp .L3 / goto .L3 (word aligned) 109*5d9d9091SRichard Lowe .align 4 110*5d9d9091SRichard Lowe.L8: 111*5d9d9091SRichard Lowe movl %edi, %eax / %edi points to the last occurrence or NULL 112*5d9d9091SRichard Lowe popl %edi / restore register variable 113*5d9d9091SRichard Lowe ret 114*5d9d9091SRichard Lowe SET_SIZE(strrchr) 115