121430Sdist/* 2*61222Sbostic * Copyright (c) 1983, 1993 3*61222Sbostic * The Regents of the University of California. All rights reserved. 434480Sbostic * 542639Sbostic * %sccs.include.redist.c% 621430Sdist */ 717327Ssam 834819Sbostic#if defined(LIBC_SCCS) && !defined(lint) 9*61222Sbostic .asciz "@(#)rindex.s 8.1 (Berkeley) 06/04/93" 1034819Sbostic#endif /* LIBC_SCCS and not lint */ 1121430Sdist 1217327Ssam/* 1317327Ssam * Find the last occurence of c in the string cp. 1417327Ssam * Return pointer to match or null pointer. 1517327Ssam * 1617327Ssam * char * 1717327Ssam * rindex(cp, c) 1817327Ssam * char *cp, c; 1917327Ssam */ 2017329Ssam#include "DEFS.h" 2117327Ssam 2217329SsamENTRY(rindex, 0) 2317327Ssam movq 4(ap),r1 # r1 = cp; r2 = c 2417327Ssam tstl r2 # check for special case c == '\0' 2517327Ssam bneq 2f 2617327Ssam1: 2717327Ssam locc $0,$65535,(r1) # just find end of string 2817327Ssam beql 1b # still looking 2917327Ssam movl r1,r0 # found it 3017327Ssam ret 3117327Ssam2: 3217334Smckusick moval tbl,r3 # r3 = address of table 3317334Smckusick bbss $0,(r3),5f # insure not reentering 3417334Smckusick movab (r3)[r2],r5 # table entry for c 3517327Ssam incb (r5) 3617327Ssam clrl r4 # last found 3717327Ssam3: 3817334Smckusick scanc $65535,(r1),(r3),$1 # look for c or '\0' 3917327Ssam beql 3b # keep looking 4017327Ssam tstb (r1) # if have found '\0' 4117327Ssam beql 4f # we are done 4217327Ssam movl r1,r4 # save most recently found 4317327Ssam incl r1 # skip over character 4417327Ssam jbr 3b # keep looking 4517327Ssam4: 4617327Ssam movl r4,r0 # return last found (if any) 4717327Ssam clrb (r5) # clean up table 4817334Smckusick clrb (r3) 4917327Ssam ret 5017327Ssam 5117327Ssam .data 5217334Smckusicktbl: .space 256 5317327Ssam .text 5417334Smckusick 5517334Smckusick/* 5617334Smckusick * Reentrant, but slower version of rindex 5717334Smckusick */ 5817334Smckusick5: 5917334Smckusick movl r1,r3 6017334Smckusick clrl r4 # r4 = pointer to last match 6117334Smckusick6: 6217334Smckusick locc $0,$65535,(r3) # look for '\0' 6317334Smckusick bneq 8f 6417334Smckusick decw r0 # r0 = 65535 6517334Smckusick1: 6617334Smckusick locc r2,r0,(r3) # look for c 6717334Smckusick bneq 7f 6817334Smckusick movl r1,r3 # reset pointer and ... 6917334Smckusick jbr 6b # ... try again 7017334Smckusick7: 7117334Smckusick movl r1,r4 # stash pointer ... 7217334Smckusick addl3 $1,r1,r3 # ... skip over match and ... 7317334Smckusick decl r0 # ... decrement count 7417334Smckusick jbr 6b # ... try again 7517334Smckusick8: 7617334Smckusick subl3 r3,r1,r0 # length of short block 7717334Smckusick incl r0 # +1 for '\0' 7817334Smckusick9: 7917334Smckusick locc r2,r0,(r3) # look for c 8017334Smckusick beql 0f 8117334Smckusick movl r1,r4 # stash pointer ... 8217334Smckusick addl3 $1,r1,r3 # ... skip over match ... 8317334Smckusick decl r0 # ... adjust count and ... 8417334Smckusick jbr 9b # ... try again 8517334Smckusick0: 8617334Smckusick movl r4,r0 # return stashed pointer 8717334Smckusick ret 88