1/******************************************************************************* 2 * 3 * Copyright (c) 1993 Intel Corporation 4 * 5 * Intel hereby grants you permission to copy, modify, and distribute this 6 * software and its documentation. Intel grants this permission provided 7 * that the above copyright notice appears in all copies and that both the 8 * copyright notice and this permission notice appear in supporting 9 * documentation. In addition, Intel grants this permission provided that 10 * you prominently mark as "not part of the original" any modifications 11 * made to this software or documentation, and that the name of Intel 12 * Corporation not be used in advertising or publicity pertaining to 13 * distribution of the software or the documentation without specific, 14 * written prior permission. 15 * 16 * Intel Corporation provides this AS IS, WITHOUT ANY WARRANTY, EXPRESS OR 17 * IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY 18 * OR FITNESS FOR A PARTICULAR PURPOSE. Intel makes no guarantee or 19 * representations regarding the use of, or the results of the use of, 20 * the software and documentation in terms of correctness, accuracy, 21 * reliability, currentness, or otherwise; and you rely on the software, 22 * documentation and results solely at your own risk. 23 * 24 * IN NO EVENT SHALL INTEL BE LIABLE FOR ANY LOSS OF USE, LOSS OF BUSINESS, 25 * LOSS OF PROFITS, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES 26 * OF ANY KIND. IN NO EVENT SHALL INTEL'S TOTAL LIABILITY EXCEED THE SUM 27 * PAID TO INTEL FOR THE PRODUCT LICENSED HEREUNDER. 28 * 29 ******************************************************************************/ 30 31#include <picolibc.h> 32 33 .file "strlen.s" 34#ifdef __PIC 35 .pic 36#endif 37#ifdef __PID 38 .pid 39#endif 40 41/* 42 * (c) copyright 1988,1993 Intel Corp., all rights reserved 43 */ 44 45/* 46 procedure strlen (optimized assembler version for the 80960K series) 47 48 src_addr = strlen (src_addr) 49 50 return the number of bytes that precede the null byte in the 51 string pointed to by src_addr. 52 53 Undefined behavior will occur if the end of the source string (i.e. 54 the terminating null byte) is in the last four words of the program's 55 allocated memory space. This is so because strlen fetches ahead 56 several words. Disallowing the fetch ahead would impose a severe 57 performance penalty. 58 59 Strategy: 60 61 Fetch the source array by long-words and scanbyte the words for the 62 null byte until found. Examine the word in which the null byte is 63 found, to determine its actual position, and return the length. 64 65 Tactics: 66 67 1) Do NOT try to fetch the words in a word aligned manner because, 68 in my judgement, the performance degradation experienced due to 69 non-aligned accesses does NOT outweigh the time and complexity added 70 by the preamble that would be necessary to assure alignment. This 71 is supported by the intuition that many source strings will be word 72 aligned to begin with. 73*/ 74 75 .globl _strlen 76 .globl __strlen 77 .leafproc _strlen, __strlen 78 .align 2 79_strlen: 80#ifndef __PIC 81 lda Lrett,g14 82#else 83 lda Lrett-(.+8)(ip),g14 84#endif 85__strlen: 86 87 mov g14,g13 # preserve return address 88 ldl (g0),g4 # fetch first two words 89 addo 8,g0,g2 # post-increment src word pointer 90 lda 0xff,g3 # byte extraction mask 91 92 93Lsearch_for_word_with_null_byte: 94 scanbyte 0,g4 # check for null byte 95 mov g5,g7 # copy second word 96 bo.f Lsearch_for_null # branch if null found 97 scanbyte 0,g7 # check for null byte 98 ldl (g2),g4 # fetch next pair of word of src 99 addo 8,g2,g2 # post-increment src word pointer 100 bno Lsearch_for_word_with_null_byte # branch if null not found yet 101 102 subo 4,g2,g2 # back up the byte pointer 103 mov g7,g4 # move word with null to search word 104Lsearch_for_null: 105 subo 9,g2,g2 # back up the byte pointer 106Lsearch_for_null.a: 107 and g4,g3,g14 # extract byte 108 cmpo 0,g14 # is it null? 109 addo 1,g2,g2 # bump src byte ptr 110 shro 8,g4,g4 # shift word to position next byte 111 bne Lsearch_for_null.a 112 113Lexit_code: 114 subo g0,g2,g0 # calculate string length 115 bx (g13) # g0 = addr of src; g14 = 0 116Lrett: 117 ret 118 119/* end of strlen */ 120