Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #include "common.h"
  2.  
  3. char * strcat(char *s, const char *append)
  4. {
  5.  int d0, d1, d2, d3;
  6.  __asm__ __volatile__(
  7.         "repne\n\t"
  8.         "scasb\n\t"
  9.         "decl %1\n"
  10.         "1:\tlodsb\n\t"
  11.         "stosb\n\t"
  12.         "testb %%al,%%al\n\t"
  13.         "jne 1b"
  14.         : "=&S" (d0), "=&D" (d1), "=&a" (d2), "=&c" (d3)
  15.         : "0" (append),"1"(s),"2"(0),"3" (0xffffffff):"memory");
  16.  return s;
  17. }
  18.  
  19. int
  20. memcmp(const void *s1, const void *s2, size_t n)
  21. {
  22.   if (n != 0)
  23.   {
  24.     const unsigned char *p1 = s1, *p2 = s2;
  25.  
  26.     do {
  27.       if (*p1++ != *p2++)
  28.         return (*--p1 - *--p2);
  29.     } while (--n != 0);
  30.   }
  31.   return 0;
  32. }
  33.  
  34. void * memcpy(void * _dest, const void *_src, size_t _n)
  35. {
  36. int d0, d1, d2;
  37.  __asm__ __volatile__(
  38.         "jcxz 1f\n\t"
  39.         "rep ; movsl\n\t"
  40.         "1:\t"
  41.         "testb $2,%b4\n\t"
  42.         "je 1f\n\t"
  43.         "movsw\n"
  44.         "1:\ttestb $1,%b4\n\t"
  45.         "je 2f\n\t"
  46.         "movsb\n"
  47.         "2:"
  48.         : "=&c" (d0), "=&D" (d1), "=&S" (d2)
  49.         :"0" (_n/4), "q" (_n),"1" ((long)_dest),"2" ((long)_src)
  50.         : "memory");
  51.  return (_dest);
  52. }
  53.  
  54. char * strcpy(char *to, const char *from)
  55. {
  56. int d0, d1, d2;
  57. __asm__ __volatile__(
  58.         "1:\tlodsb\n\t"
  59.         "stosb\n\t"
  60.         "testb %%al,%%al\n\t"
  61.         "jne 1b"
  62.         : "=&S" (d0), "=&D" (d1), "=&a" (d2)
  63.         :"0" (from),"1" (to) : "memory");
  64.  return to;
  65. }
  66.  
  67. int strcmp(const char *s1, const char *s2)
  68. {
  69. int d0, d1;
  70. register int __res;
  71. __asm__ __volatile__(
  72.         "1:\tlodsb\n\t"
  73.         "scasb\n\t"
  74.         "jne 2f\n\t"
  75.         "testb %%al,%%al\n\t"
  76.         "jne 1b\n\t"
  77.         "xorl %%eax,%%eax\n\t"
  78.         "jmp 3f\n"
  79.         "2:\tsbbl %%eax,%%eax\n\t"
  80.         "orb $1,%%al\n"
  81.         "3:"
  82.         :"=a" (__res), "=&S" (d0), "=&D" (d1)
  83.                      :"1" (s1),"2" (s2));
  84. return __res;
  85. }
  86.  
  87. size_t strlen(const char *str)
  88. {
  89. int d0;
  90. register int __res;
  91. __asm__ __volatile__(
  92.         "repne\n\t"
  93.         "scasb\n\t"
  94.         "notl %0\n\t"
  95.         "decl %0"
  96.         :"=c" (__res), "=&D" (d0) :"1" (str),"a" (0), "0" (0xffffffff));
  97. return __res;
  98. }
  99.  
  100. char * strdup(const char *_s)
  101. {
  102.   char *rv;
  103.   if (_s == 0)
  104.     return 0;
  105.   rv = (char *)kmalloc(strlen(_s) + 1);
  106.   if (rv == 0)
  107.     return 0;
  108.   strcpy(rv, _s);
  109.   return rv;
  110. }
  111.  
  112. char * strchr(const char *s, int c)
  113. {
  114.  int d0;
  115.  register char * __res;
  116.  __asm__ __volatile__(
  117.         "movb %%al,%%ah\n"
  118.         "1:\tlodsb\n\t"
  119.         "cmpb %%ah,%%al\n\t"
  120.         "je 2f\n\t"
  121.         "testb %%al,%%al\n\t"
  122.         "jne 1b\n\t"
  123.         "movl $1,%1\n"
  124.         "2:\tmovl %1,%0\n\t"
  125.         "decl %0"
  126.         :"=a" (__res), "=&S" (d0) : "1" (s),"0" (c));
  127.  return __res;
  128. }
  129.