����14-9 linux/include/string.h
1 #ifndef _STRING_H_
2 #define _STRING_H_
3
4 #ifndef NULL
5 #define NULL ((void *) 0)
6 #endif
7
8 #ifndef _SIZE_T
9 #define _SIZE_T
10 typedef unsigned int size_t;
11 #endif
12
13 extern char * strerror(int errno);
14
15 /*
16 * This string-include defines all string functions as inline
17 * functions. Use gcc. It also assumes ds=es=data space, this should be
18 * normal. Most of the string-functions are rather heavily hand-optimized,
19 * see especially strtok,strstr,str[c]spn. They should work, but are not
20 * very easy to understand. Everything is done entirely within the register
21 * set, making the functions fast and clean. String instructions have been
22 * used through-out, making for "slightly" unclear code :-)
23 *
24 * (C) 1991 Linus Torvalds
25 */
/*
* ����ַ���ͷ�ļ�����Ƕ��������ʽ�����������ַ�������������ʹ��gccʱ��ͬʱ
* �ٶ���ds=es=���ݿռ䣬��Ӧ���dz���ġ���������ַ����������Ǿ��ֹ����д���
* �Ż��ģ������Ǻ���strtok��strstr��str[c]spn������Ӧ����������������ȴ������
* ô�������⡣���еIJ��������϶���ʹ�üĴ���������ɵģ���ʹ�ú������������ࡣ
* ���еط���ʹ�����ַ���ָ�����ʹ�ô��롰������������J
*
* (C) 1991 Linus Torvalds
*/
26
//// ��һ���ַ���(src)��������һ���ַ���(dest)��ֱ������NULL�ַ���ֹͣ��
// ������dest - Ŀ���ַ���ָ�룬src - Դ�ַ���ָ�롣
// %0 - esi(src)��%1 - edi(dest)��
27 extern inline char * strcpy(char * dest,const char *src)
28 {
29 __asm__("cld\n" // �巽��λ��
30 "1:\tlodsb\n\t" // ����DS:[esi]��1�ֽ���al��������esi��
31 "stosb\n\t" // �洢�ֽ�al��ES:[edi]��������edi��
32 "testb %%al,%%al\n\t" // �մ洢���ֽ���0��
33 "jne 1b" // �����������ת�����1�������������
34 ::"S" (src),"D" (dest):"si","di","ax");
35 return dest; // ����Ŀ���ַ���ָ�롣
36 }
37
//// ����Դ�ַ���count���ֽڵ�Ŀ���ַ�����
// ���Դ������С��count���ֽڣ����ӿ��ַ�(NULL)��Ŀ���ַ�����
// ������dest - Ŀ���ַ���ָ�룬src - Դ�ַ���ָ�룬count - �����ֽ�����
// %0 - esi(src)��%1 - edi(dest)��%2 - ecx(count)��
38 extern inline char * strncpy(char * dest,const char *src,int count)
39 {
40 __asm__("cld\n" // �巽��λ��
41 "1:\tdecl %2\n\t" // �Ĵ���ecx--��count--����
42 "js 2f\n\t" // ���count<0����ǰ��ת�����2��������
43 "lodsb\n\t" // ȡds:[esi]��1�ֽ���al������esi++��
44 "stosb\n\t" // �洢���ֽ���es:[edi]������edi++��
45 "testb %%al,%%al\n\t" // ���ֽ���0��
46 "jne 1b\n\t" // ���ǣ�����ǰ��ת�����1������������
47 "rep\n\t" // ������Ŀ�Ĵ��д��ʣ������Ŀ��ַ���
48 "stosb\n"
49 "2:"
50 ::"S" (src),"D" (dest),"c" (count):"si","di","ax","cx");
51 return dest; // ����Ŀ���ַ���ָ�롣
52 }
53
//// ��Դ�ַ���������Ŀ���ַ�����ĩβ����
// ������dest - Ŀ���ַ���ָ�룬src - Դ�ַ���ָ�롣
// %0 - esi(src)��%1 - edi(dest)��%2 - eax(0)��%3 - ecx(-1)��
54 extern inline char * strcat(char * dest,const char * src)
55 {
56 __asm__("cld\n\t" // �巽��λ��
57 "repne\n\t" // �Ƚ�al��es:[edi]�ֽڣ�������edi++��
58 "scasb\n\t" // ֱ���ҵ�Ŀ�Ĵ�����0���ֽڣ���ʱedi��ָ���1�ֽڡ�
59 "decl %1\n" // ��es:[edi]ָ��0ֵ�ֽڡ�
60 "1:\tlodsb\n\t" // ȡԴ�ַ����ֽ�ds:[esi]��al����esi++��
61 "stosb\n\t" // �����ֽڴ浽es:[edi]����edi++��
62 "testb %%al,%%al\n\t" // ���ֽ���0��
63 "jne 1b" // ���ǣ��������ת�����1���������������������
64 ::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff):"si","di","ax","cx");
65 return dest; // ����Ŀ���ַ���ָ�롣
66 }
67
//// ��Դ�ַ�����count���ֽڸ��Ƶ�Ŀ���ַ�����ĩβ���������һ���ַ���
// ������dest - Ŀ���ַ�����src - Դ�ַ�����count - �����Ƶ��ֽ�����
// %0 - esi(src)��%1 - edi(dest)��%2 - eax(0)��%3 - ecx(-1)��%4 - (count)��
68 extern inline char * strncat(char * dest,const char * src,int count)
69 {
70 __asm__("cld\n\t" // �巽��λ��
71 "repne\n\t" // �Ƚ�al��es:[edi]�ֽڣ�edi++��
72 "scasb\n\t" // ֱ���ҵ�Ŀ�Ĵ���ĩ��0ֵ�ֽڡ�
73 "decl %1\n\t" // ediָ���0ֵ�ֽڡ�
74 "movl %4,%3\n" // �������ֽ�����ecx��
75 "1:\tdecl %3\n\t" // ecx--����0��ʼ��������
76 "js 2f\n\t" // ecx <0 ?��������ǰ��ת�����2����
77 "lodsb\n\t" // ����ȡds:[esi]�����ֽ���al��esi++��
78 "stosb\n\t" // �洢��es:[edi]����edi++��
79 "testb %%al,%%al\n\t" // ���ֽ�ֵΪ0��
80 "jne 1b\n" // �����������ת�����1�����������ơ�
81 "2:\txorl %2,%2\n\t" // ��al���㡣
82 "stosb" // �浽es:[edi]����
83 ::"S" (src),"D" (dest),"a" (0),"c" (0xffffffff),"g" (count)
84 :"si","di","ax","cx");
85 return dest; // ����Ŀ���ַ���ָ�롣
86 }
87
//// ��һ���ַ�������һ���ַ������бȽϡ�
// ������cs - �ַ���1��ct - �ַ���2��
// %0 - eax(__res)����ֵ��%1 - edi(cs)�ַ���1ָ�룬%2 - esi(ct)�ַ���2ָ�롣
// ���أ������1 > ��2����1����1 = ��2����0����1 < ��2����-1��
// ��90�ж�����һ���ֲ��Ĵ����������ñ�������������eax�Ĵ����У��Ա��ڸ�Ч���ʺͲ�����
// ���ֶ�������ķ�����Ҫ������Ƕ�������С���ϸ˵���μ�gcc�ֲ���ָ���Ĵ����еı�������
88 extern inline int strcmp(const char * cs,const char * ct)
89 {
90 register int __res __asm__("ax"); // __res�ǼĴ�������(eax)��
91 __asm__("cld\n" // �巽��λ��
92 "1:\tlodsb\n\t" // ȡ�ַ���2���ֽ�ds:[esi]��al������esi++��
93 "scasb\n\t" // al���ַ���1���ֽ�es:[edi]���Ƚϣ�����edi++��
94 "jne 2f\n\t" // �������ȣ�����ǰ��ת�����2��
95 "testb %%al,%%al\n\t" // ���ֽ���0ֵ�ֽ����ַ�����β����
96 "jne 1b\n\t" // ���ǣ��������ת�����1�������Ƚϡ�
97 "xorl %%eax,%%eax\n\t" // �ǣ���ֵeax���㣬
98 "jmp 3f\n" // ��ǰ��ת�����3��������
99 "2:\tmovl $1,%%eax\n\t" // eax����1��
100 "jl 3f\n\t" // ��ǰ��Ƚ��д�2�ַ�<��1�ַ�������ֵ������
101 "negl %%eax\n" // ����eax = -eax�����ظ�ֵ��������
102 "3:"
103 :"=a" (__res):"D" (cs),"S" (ct):"si","di");
104 return __res; // ���رȽϽ����
105 }
106
//// �ַ���1���ַ���2��ǰcount���ַ����бȽϡ�
// ������cs - �ַ���1��ct - �ַ���2��count - �Ƚϵ��ַ�����
// %0 - eax(__res)����ֵ��%1 - edi(cs)��1ָ�룬%2 - esi(ct)��2ָ�룬%3 - ecx(count)��
// ���أ������1 > ��2����1����1 = ��2����0����1 < ��2����-1��
107 extern inline int strncmp(const char * cs,const char * ct,int count)
108 {
109 register int __res __asm__("ax"); // __res�ǼĴ�������(eax)��
110 __asm__("cld\n" // �巽��λ��
111 "1:\tdecl %3\n\t" // count--��
112 "js 2f\n\t" // ���count<0������ǰ��ת�����2��
113 "lodsb\n\t" // ȡ��2���ַ�ds:[esi]��al������esi++��
114 "scasb\n\t" // �Ƚ�al�봮1���ַ�es:[edi]������edi++��
115 "jne 3f\n\t" // �������ȣ�����ǰ��ת�����3��
116 "testb %%al,%%al\n\t" // ���ַ���NULL�ַ���
117 "jne 1b\n" // ���ǣ��������ת�����1�������Ƚϡ�
118 "2:\txorl %%eax,%%eax\n\t" // ��NULL�ַ�����eax���㣨����ֵ����
119 "jmp 4f\n" // ��ǰ��ת�����4��������
120 "3:\tmovl $1,%%eax\n\t" // eax����1��
121 "jl 4f\n\t" // ���ǰ��Ƚ��д�2�ַ�<��1�ַ�����1������
122 "negl %%eax\n" // ����eax = -eax�����ظ�ֵ��������
123 "4:"
124 :"=a" (__res):"D" (cs),"S" (ct),"c" (count):"si","di","cx");
125 return __res; // ���رȽϽ����
126 }
127
//// ���ַ�����Ѱ�ҵ�һ��ƥ����ַ���
// ������s - �ַ�����c - ��Ѱ�ҵ��ַ���
// %0 - eax(__res)��%1 - esi(�ַ���ָ��s)��%2 - eax(�ַ�c)��
// ���أ������ַ����е�һ�γ���ƥ���ַ���ָ�롣��û���ҵ�ƥ����ַ����ؿ�ָ�롣
128 extern inline char * strchr(const char * s,char c)
129 {
130 register char * __res __asm__("ax"); // __res�ǼĴ�������(eax)��
131 __asm__("cld\n\t" // �巽��λ��
132 "movb %%al,%%ah\n" // �����Ƚ��ַ��Ƶ�ah��
133 "1:\tlodsb\n\t" // ȡ�ַ������ַ�ds:[esi]��al������esi++��
134 "cmpb %%ah,%%al\n\t" // �ַ������ַ�al��ָ���ַ�ah��Ƚϡ�
135 "je 2f\n\t" // ����ȣ�����ǰ��ת�����2����
136 "testb %%al,%%al\n\t" // al���ַ���NULL�ַ��𣿣��ַ�����β����
137 "jne 1b\n\t" // �����ǣ��������ת�����1�������Ƚϡ�
138 "movl $1,%1\n" // �ǣ���˵��û���ҵ�ƥ���ַ���esi��1��
139 "2:\tmovl %1,%0\n\t" // ��ָ��ƥ���ַ���һ���ֽڴ���ָ��ֵ����eax
140 "decl %0" // ��ָ�����Ϊָ��ƥ����ַ���
141 :"=a" (__res):"S" (s),"0" (c):"si");
142 return __res; // ����ָ�롣
143 }
144
//// Ѱ���ַ�����ָ���ַ����һ�γ��ֵĵط��������������ַ�����
// ������s - �ַ�����c - ��Ѱ�ҵ��ַ���
// %0 - edx(__res)��%1 - edx(0)��%2 - esi(�ַ���ָ��s)��%3 - eax(�ַ�c)��
// ���أ������ַ��������һ�γ���ƥ���ַ���ָ�롣��û���ҵ�ƥ����ַ����ؿ�ָ�롣
145 extern inline char * strrchr(const char * s,char c)
146 {
147 register char * __res __asm__("dx"); // __res�ǼĴ�������(edx)��
148 __asm__("cld\n\t" // �巽��λ��
149 "movb %%al,%%ah\n" // ����Ѱ�ҵ��ַ��Ƶ�ah��
150 "1:\tlodsb\n\t" // ȡ�ַ������ַ�ds:[esi]��al������esi++��
151 "cmpb %%ah,%%al\n\t" // �ַ������ַ�al��ָ���ַ�ah���Ƚϡ�
152 "jne 2f\n\t" // ������ȣ�����ǰ��ת�����2����
153 "movl %%esi,%0\n\t" // ���ַ�ָ�뱣�浽edx�С�
154 "decl %0\n" // ָ�����һλ��ָ���ַ�����ƥ���ַ�����
155 "2:\ttestb %%al,%%al\n\t" // �Ƚϵ��ַ���0�𣨵��ַ���β����
156 "jne 1b" // �����������ת�����1���������Ƚϡ�
157 :"=d" (__res):"0" (0),"S" (s),"a" (c):"ax","si");
158 return __res; // ����ָ�롣
159 }
160
//// ���ַ���1��Ѱ�ҵ�1���ַ����У����ַ������е��κ��ַ����������ַ���2�С�
// ������cs - �ַ���1ָ�룬ct - �ַ���2ָ�롣
// %0 - esi(__res)��%1 - eax(0)��%2 - ecx(-1)��%3 - esi(��1ָ��cs)��%4 - (��2ָ��ct)��
// �����ַ���1�а����ַ���2���κ��ַ������ַ����еij���ֵ��
161 extern inline int strspn(const char * cs, const char * ct)
162 {
163 register char * __res __asm__("si"); // __res�ǼĴ�������(esi)��
164 __asm__("cld\n\t" // �巽��λ��
165 "movl %4,%%edi\n\t" // ���ȼ��㴮2�ij��ȡ���2ָ�����edi�С�
166 "repne\n\t" // �Ƚ�al(0)�봮2�е��ַ���es:[edi]������edi++��
167 "scasb\n\t" // �������Ⱦͼ����Ƚ�(ecx�ݼ�)��
168 "notl %%ecx\n\t" // ecx��ÿλȡ����
169 "decl %%ecx\n\t" // ecx--���ô�2�ij���ֵ��
170 "movl %%ecx,%%edx\n" // ����2�ij���ֵ�ݷ���edx�С�
171 "1:\tlodsb\n\t" // ȡ��1�ַ�ds:[esi]��al������esi++��
172 "testb %%al,%%al\n\t" // ���ַ�����0ֵ�𣨴�1��β����
173 "je 2f\n\t" // ����ǣ�����ǰ��ת�����2����
174 "movl %4,%%edi\n\t" // ȡ��2ͷָ�����edi�С�
175 "movl %%edx,%%ecx\n\t" // �ٽ���2�ij���ֵ����ecx�С�
176 "repne\n\t" // �Ƚ�al�봮2���ַ�es:[edi]������edi++��
177 "scasb\n\t" // �������Ⱦͼ����Ƚϡ�
178 "je 1b\n" // �����ȣ��������ת�����1����
179 "2:\tdecl %0" // esi--��ָ�����һ�������ڴ�2�е��ַ���
180 :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
181 :"ax","cx","dx","di");
182 return __res-cs; // �����ַ����еij���ֵ��
183 }
184
//// Ѱ���ַ���1�в������ַ���2���κ��ַ������ַ����С�
// ������cs - �ַ���1ָ�룬ct - �ַ���2ָ�롣
// %0 - esi(__res)��%1 - eax(0)��%2 - ecx(-1)��%3 - esi(��1ָ��cs)��%4 - (��2ָ��ct)��
// �����ַ���1�в������ַ���2���κ��ַ������ַ����еij���ֵ��
185 extern inline int strcspn(const char * cs, const char * ct)
186 {
187 register char * __res __asm__("si"); // __res�ǼĴ�������(esi)��
188 __asm__("cld\n\t" // �巽��λ��
189 "movl %4,%%edi\n\t" // ���ȼ��㴮2�ij��ȡ���2ָ�����edi�С�
190 "repne\n\t" // �Ƚ�al(0)�봮2�е��ַ���es:[edi]������edi++��
191 "scasb\n\t" // �������Ⱦͼ����Ƚ�(ecx�ݼ�)��
192 "notl %%ecx\n\t" // ecx��ÿλȡ����
193 "decl %%ecx\n\t" // ecx--���ô�2�ij���ֵ��
194 "movl %%ecx,%%edx\n" // ����2�ij���ֵ�ݷ���edx�С�
195 "1:\tlodsb\n\t" // ȡ��1�ַ�ds:[esi]��al������esi++��
196 "testb %%al,%%al\n\t" // ���ַ�����0ֵ�𣨴�1��β����
197 "je 2f\n\t" // ����ǣ�����ǰ��ת�����2����
198 "movl %4,%%edi\n\t" // ȡ��2ͷָ�����edi�С�
199 "movl %%edx,%%ecx\n\t" // �ٽ���2�ij���ֵ����ecx�С�
200 "repne\n\t" // �Ƚ�al�봮2���ַ�es:[edi]������edi++��
201 "scasb\n\t" // �������Ⱦͼ����Ƚϡ�
202 "jne 1b\n" // �������ȣ��������ת�����1����
203 "2:\tdecl %0" // esi--��ָ�����һ�������ڴ�2�е��ַ���
204 :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
205 :"ax","cx","dx","di");
206 return __res-cs; // �����ַ����еij���ֵ��
207 }
208
//// ���ַ���1��Ѱ�����������ַ���2�е��κ��ַ���
// ������cs - �ַ���1��ָ�룬ct - �ַ���2��ָ�롣
// %0 -esi(__res)��%1 -eax(0)��%2 -ecx(0xffffffff)��%3 -esi(��1ָ��cs)��%4 -(��2ָ��ct)��
// �����ַ���1���������ַ���2���ַ���ָ�롣
209 extern inline char * strpbrk(const char * cs,const char * ct)
210 {
211 register char * __res __asm__("si"); // __res�ǼĴ�������(esi)��
212 __asm__("cld\n\t" // �巽��λ��
213 "movl %4,%%edi\n\t" // ���ȼ��㴮2�ij��ȡ���2ָ�����edi�С�
214 "repne\n\t" // �Ƚ�al(0)�봮2�е��ַ���es:[edi]������edi++��
215 "scasb\n\t" // �������Ⱦͼ����Ƚ�(ecx�ݼ�)��
216 "notl %%ecx\n\t" // ecx��ÿλȡ����
217 "decl %%ecx\n\t" // ecx--���ô�2�ij���ֵ��
218 "movl %%ecx,%%edx\n" // ����2�ij���ֵ�ݷ���edx�С�
219 "1:\tlodsb\n\t" // ȡ��1�ַ�ds:[esi]��al������esi++��
220 "testb %%al,%%al\n\t" // ���ַ�����0ֵ�𣨴�1��β����
221 "je 2f\n\t" // ����ǣ�����ǰ��ת�����2����
222 "movl %4,%%edi\n\t" // ȡ��2ͷָ�����edi�С�
223 "movl %%edx,%%ecx\n\t" // �ٽ���2�ij���ֵ����ecx�С�
224 "repne\n\t" // �Ƚ�al�봮2���ַ�es:[edi]������edi++��
225 "scasb\n\t" // �������Ⱦͼ����Ƚϡ�
226 "jne 1b\n\t" // �������ȣ��������ת�����1����
227 "decl %0\n\t" // esi--��ָ��һ�������ڴ�2�е��ַ���
228 "jmp 3f\n" // ��ǰ��ת�����3����
229 "2:\txorl %0,%0\n" // û���ҵ����������ģ�������ֵΪNULL��
230 "3:"
231 :"=S" (__res):"a" (0),"c" (0xffffffff),"0" (cs),"g" (ct)
232 :"ax","cx","dx","di");
233 return __res; // ����ָ��ֵ��
234 }
235
//// ���ַ���1��Ѱ����ƥ�������ַ���2���ַ�����
// ������cs - �ַ���1��ָ�룬ct - �ַ���2��ָ�롣
// %0 -eax(__res)��%1 -eax(0)��%2 -ecx(0xffffffff)��%3 -esi(��1ָ��cs)��%4 -(��2ָ��ct)��
// ���أ������ַ���1����ƥ���ַ���2���ַ���ָ�롣
236 extern inline char * strstr(const char * cs,const char * ct)
237 {
238 register char * __res __asm__("ax"); // __res�ǼĴ�������(eax)��
239 __asm__("cld\n\t" \ // �巽��λ��
240 "movl %4,%%edi\n\t" // ���ȼ��㴮2�ij��ȡ���2ָ�����edi�С�
241 "repne\n\t" // �Ƚ�al(0)�봮2�е��ַ���es:[edi]������edi++��
242 "scasb\n\t" // �������Ⱦͼ����Ƚ�(ecx�ݼ�)��
243 "notl %%ecx\n\t" // ecx��ÿλȡ����
244 "decl %%ecx\n\t" /* NOTE! This also sets Z if searchstring='' */
/* ע�⣡���������Ϊ�գ�������Z��־ */ // �ô�2�ij���ֵ��
245 "movl %%ecx,%%edx\n" // ����2�ij���ֵ�ݷ���edx�С�
246 "1:\tmovl %4,%%edi\n\t" // ȡ��2ͷָ�����edi�С�
247 "movl %%esi,%%eax\n\t" // ����1��ָ�븴�Ƶ�eax�С�
248 "movl %%edx,%%ecx\n\t" // �ٽ���2�ij���ֵ����ecx�С�
249 "repe\n\t" // �Ƚϴ�1�ʹ�2�ַ�(ds:[esi],es:[edi])��esi++,edi++��
250 "cmpsb\n\t" // ����Ӧ�ַ���Ⱦ�һֱ�Ƚ���ȥ��
251 "je 2f\n\t" /* also works for empty string, see above */
/* �Կմ�ͬ����Ч�������� */ // ��ȫ��ȣ���ת�����2��
252 "xchgl %%eax,%%esi\n\t" // ��1ͷָ����esi���ȽϽ���Ĵ�1ָ����eax��
253 "incl %%esi\n\t" // ��1ͷָ��ָ����һ���ַ���
254 "cmpb $0,-1(%%eax)\n\t" // ��1ָ��(eax-1)��ָ�ֽ���0��
255 "jne 1b\n\t" // ������ת�����1�������Ӵ�1�ĵ�2���ַ���ʼ�Ƚϡ�
256 "xorl %%eax,%%eax\n\t" // ��eax����ʾû���ҵ�ƥ�䡣
257 "2:"
258 :"=a" (__res):"0" (0),"c" (0xffffffff),"S" (cs),"g" (ct)
259 :"cx","dx","di","si");
260 return __res; // ���رȽϽ����
261 }
262
//// �����ַ������ȡ�
// ������s - �ַ�����
// %0 - ecx(__res)��%1 - edi(�ַ���ָ��s)��%2 - eax(0)��%3 - ecx(0xffffffff)��
// ���أ������ַ����ij��ȡ�
263 extern inline int strlen(const char * s)
264 {
265 register int __res __asm__("cx"); // __res�ǼĴ�������(ecx)��
266 __asm__("cld\n\t" // �巽��λ��
267 "repne\n\t" // al(0)���ַ������ַ�es:[edi]�Ƚϣ�
268 "scasb\n\t" // ������Ⱦ�һֱ�Ƚϡ�
269 "notl %0\n\t" // ecxȡ����
270 "decl %0" // ecx--�����ַ����ó���ֵ��
271 :"=c" (__res):"D" (s),"a" (0),"0" (0xffffffff):"di");
272 return __res; // �����ַ�������ֵ��
273 }
274
275 extern char * ___strtok; // ������ʱ���ָ�����汻�����ַ���1(s)��ָ�롣
276
//// �����ַ���2�е��ַ����ַ���1�ָ�ɱ��(tokern)���С�
// ����1�����ǰ��������������(token)�����У����ɷָ���ַ���2�е�һ�������ַ�
// �ֿ�����һ�ε��� strtok()ʱ��������ָ���ַ���1�е�1��token���ַ���ָ�룬���ڷ�
// ��tokenʱ��һ null�ַ�д���ָ����������ʹ�� null ��Ϊ�ַ���1�ĵ��ã��������ַ�
// ������ɨ���ַ���1��ֱ��û��token Ϊֹ���ڲ�ͬ�ĵ��ù����У��ָ����2���Բ�ͬ��
// ������s - ���������ַ���1��ct - ���������ָ�����ַ���2��
// ��������%0 - ebx(__res)��%1 - esi(__strtok)��
// ������룺%2 - ebx(__strtok)��%3 - esi(�ַ���1ָ��s)��%4 - (�ַ���2ָ��ct)��
// ���أ������ַ���s�е�1��token�����û���ҵ�token����һ��nullָ�롣
// ����ʹ���ַ���sָ��Ϊnull�ĵ��ã�����ԭ�ַ���s��������һ��token��
277 extern inline char * strtok(char * s,const char * ct)
278 {
279 register char * __res __asm__("si");
280 __asm__("testl %1,%1\n\t" // ���Ȳ���esi(�ַ���1ָ��s)�Ƿ���NULL��
281 "jne 1f\n\t" // ������ǣ���������״ε��ñ���������ת���1��
282 "testl %0,%0\n\t" // ����NULL����ʾ�˴��Ǻ������ã���ebx(__strtok)��
283 "je 8f\n\t" // ���ebxָ����NULL�����ܴ�������ת������
284 "movl %0,%1\n" // ��ebxָ�븴�Ƶ�esi��
285 "1:\txorl %0,%0\n\t" // ��ebxָ�롣
286 "movl $-1,%%ecx\n\t" // ��ecx = 0xffffffff��
287 "xorl %%eax,%%eax\n\t" // ����eax��
288 "cld\n\t" // �巽��λ��
289 "movl %4,%%edi\n\t" // �������ַ���2�ij��ȡ�ediָ���ַ���2��
290 "repne\n\t" // ��al(0)��es:[edi]�Ƚϣ�����edi++��
291 "scasb\n\t" // ֱ���ҵ��ַ���2�Ľ���null�ַ��������ecx==0��
292 "notl %%ecx\n\t" // ��ecxȡ����
293 "decl %%ecx\n\t" // ecx--���õ��ַ���2�ij���ֵ��
294 "je 7f\n\t" /* empty delimeter-string */
/* �ָ���ַ����� */ // ����2����Ϊ0����ת���7��
295 "movl %%ecx,%%edx\n" // ����2�����ݴ���edx��
296 "2:\tlodsb\n\t" // ȡ��1���ַ�ds:[esi]��al������esi++��
297 "testb %%al,%%al\n\t" // ���ַ�Ϊ0ֵ��(��1����)��
298 "je 7f\n\t" // ����ǣ�����ת���7��
299 "movl %4,%%edi\n\t" // edi�ٴ�ָ��2�ס�
300 "movl %%edx,%%ecx\n\t" // ȡ��2�ij���ֵ���������ecx��
301 "repne\n\t" // ��al�д�1���ַ��봮2�������ַ��Ƚϣ�
302 "scasb\n\t" // �жϸ��ַ��Ƿ�Ϊ�ָ����
303 "je 2b\n\t" // �����ڴ�2���ҵ���ͬ�ַ����ָ����������ת���2��
304 "decl %1\n\t" // �����Ƿָ������1ָ��esiָ���ʱ�ĸ��ַ���
305 "cmpb $0,(%1)\n\t" // ���ַ���NULL�ַ���
306 "je 7f\n\t" // ���ǣ�����ת���7����
307 "movl %1,%0\n" // �����ַ���ָ��esi�����ebx��
308 "3:\tlodsb\n\t" // ȡ��1��һ���ַ�ds:[esi]��al������esi++��
309 "testb %%al,%%al\n\t" // ���ַ���NULL�ַ���
310 "je 5f\n\t" // ���ǣ���ʾ��1��������ת�����5��
311 "movl %4,%%edi\n\t" // edi�ٴ�ָ��2�ס�
312 "movl %%edx,%%ecx\n\t" // ��2����ֵ���������ecx��
313 "repne\n\t" // ��al�д�1���ַ��봮2��ÿ���ַ��Ƚϣ�
314 "scasb\n\t" // ����al�ַ��Ƿ��Ƿָ����
315 "jne 3b\n\t" // �����Ƿָ������ת���3����1����һ���ַ���
316 "decl %1\n\t" // ���Ƿָ������esi--��ָ��÷ָ���ַ���
317 "cmpb $0,(%1)\n\t" // �÷ָ����NULL�ַ���
318 "je 5f\n\t" // ���ǣ�����ת�����5��
319 "movb $0,(%1)\n\t" // �����ǣ��÷ָ����NULL�ַ��滻����
320 "incl %1\n\t" // esiָ��1����һ���ַ���Ҳ��ʣ�മ�ס�
321 "jmp 6f\n" // ��ת���6����
322 "5:\txorl %1,%1\n" // esi���㡣
323 "6:\tcmpb $0,(%0)\n\t" // ebxָ��ָ��NULL�ַ���
324 "jne 7f\n\t" // �����ǣ�����ת���7��
325 "xorl %0,%0\n" // ���ǣ�����ebx=NULL��
326 "7:\ttestl %0,%0\n\t" // ebxָ��ΪNULL��
327 "jne 8f\n\t" // ����������ת8�����������롣
328 "movl %0,%1\n" // ��esi��ΪNULL��
329 "8:"
330 :"=b" (__res),"=S" (___strtok)
331 :"0" (___strtok),"1" (s),"g" (ct)
332 :"ax","cx","dx","di");
333 return __res; // ����ָ����token��ָ�롣
334 }
335
//// �ڴ�鸴�ơ���Դ��ַsrc����ʼ����n���ֽڵ�Ŀ�ĵ�ַdest����
// ������dest - ���Ƶ�Ŀ�ĵ�ַ��src - ���Ƶ�Դ��ַ��n - �����ֽ�����
// %0 - ecx(n)��%1 - esi(src)��%2 - edi(dest)��
336 extern inline void * memcpy(void * dest,const void * src, int n)
337 {
338 __asm__("cld\n\t" // �巽��λ��
339 "rep\n\t" // �ظ�ִ�и���ecx���ֽڣ�
340 "movsb" // ��ds:[esi]��es:[edi]��esi++��edi++��
341 ::"c" (n),"S" (src),"D" (dest)
342 :"cx","si","di");
343 return dest; // ����Ŀ�ĵ�ַ��
344 }
345
//// �ڴ���ƶ���ͬ�ڴ�鸴�ƣ��������ƶ��ķ���
// ������dest - ���Ƶ�Ŀ�ĵ�ַ��src - ���Ƶ�Դ��ַ��n - �����ֽ�����
// ��dest<src��%0 - ecx(n)��%1 - esi(src)��%2 - edi(dest)��
// ����%0 - ecx(n)��%1 - esi(src+n-1)��%2 - edi(dest+n-1)��
// ����������Ϊ�˷�ֹ�ڸ���ʱ������ص����ǡ�
346 extern inline void * memmove(void * dest,const void * src, int n)
347 {
348 if (dest<src)
349 __asm__("cld\n\t" // �巽��λ��
350 "rep\n\t" // ��ds:[esi]��es:[edi]������esi++��edi++��
351 "movsb" // �ظ�ִ�и���ecx�ֽڡ�
352 ::"c" (n),"S" (src),"D" (dest)
353 :"cx","si","di");
354 else
355 __asm__("std\n\t" // �÷���λ����ĩ�˿�ʼ���ơ�
356 "rep\n\t" // ��ds:[esi]��es:[edi]������esi--��edi--��
357 "movsb" // ����ecx���ֽڡ�
358 ::"c" (n),"S" (src+n-1),"D" (dest+n-1)
359 :"cx","si","di");
360 return dest;
361 }
362
//// �Ƚ�n���ֽڵ������ڴ棨�����ַ���������ʹ����NULL�ֽ�Ҳ��ֹͣ�Ƚϡ�
// ������cs - �ڴ��1��ַ��ct - �ڴ��2��ַ��count - �Ƚϵ��ֽ�����
// %0 - eax(__res)��%1 - eax(0)��%2 - edi(�ڴ��1)��%3 - esi(�ڴ��2)��%4 - ecx(count)��
// ���أ�����1>��2 ����1����1<��2������-1����1==��2����0��
363 extern inline int memcmp(const void * cs,const void * ct,int count)
364 {
365 register int __res __asm__("ax"); // __res�ǼĴ���������
366 __asm__("cld\n\t" // �巽��λ��
367 "repe\n\t" // ���������ظ���
368 "cmpsb\n\t" // �Ƚ�ds:[esi]��es:[edi]�����ݣ�����esi++��edi++��
369 "je 1f\n\t" // �������ͬ������ת�����1������0(eax)ֵ
370 "movl $1,%%eax\n\t" // ����eax��1��
371 "jl 1f\n\t" // ���ڴ��2���ݵ�ֵ<�ڴ��1������ת���1��
372 "negl %%eax\n" // ����eax = -eax��
373 "1:"
374 :"=a" (__res):"0" (0),"D" (cs),"S" (ct),"c" (count)
375 :"si","di","cx");
376 return __res; // ���رȽϽ����
377 }
378
//// ��n�ֽڴ�С���ڴ��(�ַ���)��Ѱ��ָ���ַ���
// ������cs - ָ���ڴ���ַ��c - ָ�����ַ���count - �ڴ�鳤�ȡ�
// %0 - edi(__res)��%1 - eax(�ַ�c)��%2 - edi(�ڴ���ַcs)��%3 - ecx(�ֽ���count)��
// ���ص�һ��ƥ���ַ���ָ�룬���û���ҵ�����NULL�ַ���
379 extern inline void * memchr(const void * cs,char c,int count)
380 {
381 register void * __res __asm__("di"); // __res�ǼĴ���������
382 if (!count) // ����ڴ�鳤��==0����NULL��û���ҵ���
383 return NULL;
384 __asm__("cld\n\t" // �巽��λ��
385 "repne\n\t" // �����������ظ�ִ��������䣬
386 "scasb\n\t" // al���ַ���es:[edi]�ַ����Ƚϣ�����edi++��
387 "je 1f\n\t" // ����������ǰ��ת�����1����
388 "movl $1,%0\n" // ����edi����1��
389 "1:\tdecl %0" // ��ediָ���ҵ����ַ�������NULL����
390 :"=D" (__res):"a" (c),"D" (cs),"c" (count)
391 :"cx");
392 return __res; // �����ַ�ָ�롣
393 }
394
//// ���ַ���дָ�������ڴ�顣
// ���ַ�c��дsָ����ڴ�������count�ֽڡ�
// %0 - eax(�ַ�c)��%1 - edi(�ڴ��ַ)��%2 - ecx(�ֽ���count)��
395 extern inline void * memset(void * s,char c,int count)
396 {
397 __asm__("cld\n\t" // �巽��λ��
398 "rep\n\t" // �ظ�ecxָ���Ĵ�����ִ��
399 "stosb" // ��al���ַ�����es:[edi]�У�����edi++��
400 ::"a" (c),"D" (s),"c" (count)
401 :"cx","di");
402 return s;
403 }
404
405 #endif
406