CRT的 源码分为3部分:
-
c C语言
-
cpp c++
-
asm 性能优化汇编版本
strcat
1char * __cdecl strcat ( 2 char * dst, 3 const char * src 4 ) 5{ 6 char * cp = dst; 7 8 while( *cp ) 9 cp++; /* find end of dst */ 10 11 while( *cp++ = *src++ ) ; /* Copy src to end of dst */ 12 13 return( dst ); /* return dst */ 14 15}
strcmp
1int __cdecl strcmp ( 2 const char * src, 3 const char * dst 4 ) 5{ 6 int ret = 0 ; 7 8 while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst) 9 ++src, ++dst; 10 11 if ( ret < 0 ) 12 ret = -1 ; 13 else if ( ret > 0 ) 14 ret = 1 ; 15 16 return( ret ); 17}
strcmp asm 版本
1 page ,132 2 title strcmp.asm - compare two strings 3;*** 4;strcmp.asm - routine to compare two strings (for equal, less, or greater) 5; 6; Copyright (c) Microsoft Corporation. All rights reserved. 7; 8;Purpose: 9; STRCMP compares two strings and returns an integer 10; to indicate whether the first is less than the second, the two are 11; equal, or whether the first is greater than the second, respectively. 12; Comparison is done byte by byte on an UNSIGNED basis, which is to 13; say that Null (0) is less than any other character (1-255). 14; 15;******************************************************************************* 16 17 .xlist 18 include cruntime.inc 19 .list 20 21page 22;*** 23;strcmp - compare two strings, returning less than, equal to, or greater than 24; 25;Purpose: 26; Compares two string, determining their lexical order. Unsigned 27; comparison is used. 28; 29; Base Algorithm: 30; int strcmp ( const char *str1, const char *str2 ) 31; { 32; const unsigned char *src1 = (const unsigned char *)str1; 33; const unsigned char *src2 = (const unsigned char *)str2; 34; int ret = 0 ; 35; 36; while( ! (ret = *src1 - *src2) && *src2) 37; ++src1, ++src2; 38; 39; if ( ret < 0 ) 40; ret = -1 ; 41; else if ( ret > 0 ) 42; ret = 1 ; 43; 44; return( ret ); 45; } 46; 47;Entry: 48; const char * src1 - string for left-hand side of comparison 49; const char * src2 - string for right-hand side of comparison 50; 51;Exit: 52; EAX < 0, 0, or >0, indicating whether the first string is 53; Less than, Equal to, or Greater than the second string. 54; 55;Uses: 56; ECX, EDX 57; 58;Exceptions: 59; 60;******************************************************************************* 61 62 CODESEG 63 64 public strcmp 65strcmp proc \ 66 str1:ptr byte, \ 67 str2:ptr byte 68 69 OPTION PROLOGUE:NONE, EPILOGUE:NONE 70 71; .FPO (cdwLocals, cdwParams, cbProlog, cbRegs, fUseBP, cbFrame) 72 .FPO ( 0, 2, 0, 0, 0, 0 ) 73 74 mov edx,[esp + 4] ; edx = src 75 mov ecx,[esp + 8] ; ecx = dst 76 77 test edx,3 78 jnz short dopartial 79 80 align 4 81dodwords: 82 mov eax,[edx] 83 84 cmp al,[ecx] 85 jne short donene 86 test al,al 87 jz short doneeq 88 cmp ah,[ecx + 1] 89 jne short donene 90 test ah,ah 91 jz short doneeq 92 93 shr eax,16 94 95 cmp al,[ecx + 2] 96 jne short donene 97 test al,al 98 jz short doneeq 99 cmp ah,[ecx + 3] 100 jne short donene 101 add ecx,4 102 add edx,4 103 test ah,ah 104 jnz short dodwords 105 106 align 4 107doneeq: 108 xor eax,eax 109 ret 110 111 align 8 112donene: 113 ; The instructions below should place -1 in eax if src < dst, 114 ; and 1 in eax if src > dst. 115 116 sbb eax,eax 117 or eax,1 118 ret 119 120 align 16 121dopartial: 122 test edx,1 123 jz short doword 124 125 mov al,[edx] 126 add edx,1 127 cmp al,[ecx] 128 jne short donene 129 add ecx,1 130 test al,al 131 jz short doneeq 132 133 test edx,2 134 jz short dodwords 135 136 137 align 4 138doword: 139 mov ax,[edx] 140 add edx,2 141 cmp al,[ecx] 142 jne short donene 143 test al,al 144 jz short doneeq 145 cmp ah,[ecx + 1] 146 jne short donene 147 test ah,ah 148 jz short doneeq 149 add ecx,2 150 jmp short dodwords 151 152strcmp endp 153 154 end
strlen
1size_t __cdecl strlen ( 2 const char * str 3 ) 4{ 5 const char *eos = str; 6 7 while( *eos++ ) ; 8 9 return( eos - str - 1 ); 10}