Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4113303
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: LINUX

2009-05-12 12:36:48

#include <stdio.h>

inline char * my_strcpy(char * dest,const char *src)
{
int d0, d1, d2;
__asm__ __volatile__(
    "1:\tlodsb\n\t"
    "stosb\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b"
    : "=&S" (d0), "=&D" (d1), "=&a" (d2)
    :"0" (src),"1" (dest) : "memory");
return dest;
}

inline char * my_strncpy(char * dest,const char *src,size_t count)
{
int d0, d1, d2, d3;
__asm__ __volatile__(
    "1:\tdecl %2\n\t"
    "js 2f\n\t"
    "lodsb\n\t"
    "stosb\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b\n\t"
    "rep\n\t"
    "stosb\n"
    "2:"
    : "=&S" (d0), "=&D" (d1), "=&c" (d2), "=&a" (d3)
    :"0" (src),"1" (dest),"2" (count) : "memory");
return dest;
}

inline char * my_strcat(char * dest,const char * src)
{
int d0, d1, d2, d3;
__asm__ __volatile__(
    "repne\n\t"
    "scasb\n\t"
    "decl %1\n"
    "1:\tlodsb\n\t"
    "stosb\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b"
    : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
    : "0" (src), "1" (dest), "2" (0), "3" (0xffffffffu):"memory");
return dest;
}

inline char * my_strncat(char * dest,const char * src,size_t count)
{
int d0, d1, d2, d3;
__asm__ __volatile__(
    "repne\n\t"
    "scasb\n\t"
    "decl %1\n\t"
    "movl %8,%3\n"
    "1:\tdecl %3\n\t"
    "js 2f\n\t"
    "lodsb\n\t"
    "stosb\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b\n"
    "2:\txorl %2,%2\n\t"
    "stosb"
    : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
    : "0" (src),"1" (dest),"2" (0),"3" (0xffffffffu), "g" (count)
    : "memory");
return dest;
}

inline int my_strcmp(const char * cs,const char * ct)
{
int d0, d1;
register int __res;
__asm__ __volatile__(
    "1:\tlodsb\n\t"
    "scasb\n\t"
    "jne 2f\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b\n\t"
    "xorl %%eax,%%eax\n\t"
    "jmp 3f\n"
    "2:\tsbbl %%eax,%%eax\n\t"
    "orb $1,%%al\n"
    "3:"
    :"=a" (__res), "=&S" (d0), "=&D" (d1)
    :"1" (cs),"2" (ct)
    :"memory");
return __res;
}

inline int my_strncmp(const char * cs,const char * ct,size_t count)
{
register int __res;
int d0, d1, d2;
__asm__ __volatile__(
    "1:\tdecl %3\n\t"
    "js 2f\n\t"
    "lodsb\n\t"
    "scasb\n\t"
    "jne 3f\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b\n"
    "2:\txorl %%eax,%%eax\n\t"
    "jmp 4f\n"
    "3:\tsbbl %%eax,%%eax\n\t"
    "orb $1,%%al\n"
    "4:"
    :"=a" (__res), "=&S" (d0), "=&D" (d1), "=&c" (d2)
    :"1" (cs),"2" (ct),"3" (count)
    :"memory");
return __res;
}

inline char* my_strchr(const char * s, int c)
{
int d0;
register char * __res;
__asm__ __volatile__(
    "movb %%al,%%ah\n"
    "1:\tlodsb\n\t"
    "cmpb %%ah,%%al\n\t"
    "je 2f\n\t"
    "testb %%al,%%al\n\t"
    "jne 1b\n\t"
    "movl $1,%1\n"
    "2:\tmovl %1,%0\n\t"
    "decl %0"
    :"=a" (__res), "=&S" (d0)
    :"1" (s),"0" (c)
    :"memory");
return __res;
}

inline char* my_strrchr(const char * s, int c)
{
int d0, d1;
register char * __res;
__asm__ __volatile__(
    "movb %%al,%%ah\n"
    "1:\tlodsb\n\t"
    "cmpb %%ah,%%al\n\t"
    "jne 2f\n\t"
    "leal -1(%%esi),%0\n"
    "2:\ttestb %%al,%%al\n\t"
    "jne 1b"
    :"=g" (__res), "=&S" (d0), "=&a" (d1)
    :"0" (0),"1" (s),"2" (c)
    :"memory");
return __res;
}

inline int my_strlen(const char * s)
{
int d0;
register int __res;
__asm__ __volatile__(
    "repne\n\t"
    "scasb\n\t"
    "notl %0\n\t"
    "decl %0"
    :"=c" (__res), "=&D" (d0)
    :"1" (s),"a" (0), "0" (0xffffffffu)
    :"memory");
return __res;
}

inline void* my_memcpy(void * to, const void * from, size_t n)
{
int d0, d1, d2;
__asm__ __volatile__(
    "rep ; movsl\n\t"
    "movl %4,%%ecx\n\t"
    "andl $3,%%ecx\n\t"
#if 1    /* want to pay 2 byte penalty for a chance to skip microcoded rep? */
    "jz 1f\n\t"
#endif
    "rep ; movsb\n\t"
    "1:"
    : "=&c" (d0), "=&D" (d1), "=&S" (d2)
    : "0" (n/4), "g" (n), "1" ((long) to), "2" ((long) from)
    : "memory");
return (to);
}

int main()
{
 char string[50];
 char* str1 = "Hello world\n";
 char* str2 = "Hello jimmy\n";

 my_strcpy(string,str1);
 printf("string is %s after my_strcpy str1 %s\n",string,str1);

 my_strncpy(string,str1,11);
 printf("string is %s after my_strcpy str1 %s len 10\n",string,str1);

 my_strcat(string,str2);
 printf("string is %s after my_strcat str2 %s\n",string,str2);

 my_strncat(string,str1,5);
 printf("string is %s after my_strcpy str1 %s len 5\n",string,str1);

 printf("str1 %s compare str2 %s is %d\n",str1,str2,my_strcmp(str1,str2));

 printf("str1 %s compare str2 %s len 5 is %d\n",str1,str2,my_strncmp(str1,str2,5));

 printf("strchr str1 %s char 'o' is %s\n",str1,my_strchr(str1,'o'));
 printf("strrchr str1 %s char 'o' is %s\n",str1,my_strrchr(str1,'o'));

 printf("strlen str1 %s is %d\n",str1,my_strlen(str1));
 
 my_memcpy(string,str1,11);
 printf("string is %s after my_memcpy str1 %s\n",string,str1);



 return 0;
}

阅读(1399) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

jimmyixy2010-07-16 15:58:30

linux 内核0.01中的string.h的源码嘛