免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 873 | 回复: 0
打印 上一主题 下一主题

汇编写 linux 字符串函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-12 12:36 |只看该作者 |倒序浏览

               
               
                #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;
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/76292/showart_1925320.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP