memcpy

pattern

Mar 17th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. /*
  2.  * both functions do the same thing, find repeating pattern in a string
  3.  * in assembly, uses 7 different instructions, occupies 31 lines
  4.  * in C, uses (a lot) of stack instructions, also occupies 31 lines
  5.  */
  6.  
  7. /*
  8.  * speed comparison, two different tests were done (different strings)
  9.  * results (in cycles):
  10.  * 1) c version - 782 | asm version - 357
  11.  * 2) c version - 1127 | asm version - 425
  12.  */
  13.  
  14. /***********************************************************************************/
  15.  
  16. .globl _asm_find_pattern
  17. _asm_find_pattern:
  18.  
  19.     movq    $0, %rcx
  20. fp_out_loop:
  21.     addq    $1, %rcx
  22.     movq    %rsi, %rdx
  23. fp_inf_loop:
  24.     movq    %rcx, %rbx
  25.     movq    %rsi, %r8
  26.     movq    %rdx, %r9
  27. fp_cmp_loop:
  28.     addq    $1, %r8
  29.     addq    $1, %r9
  30.     movb    -1(%r8), %al
  31.     cmpb    %al, -1(%r9)
  32.     jne fp_out_loop
  33.     subq    $1, %rbx
  34.     jne fp_cmp_loop
  35.     addq    %rcx, %rdx
  36.     cmpb    $0, (%rdx)
  37.     jne fp_inf_loop
  38.     subq    %rcx, %rdx
  39. fp_cpy_loop:
  40.     movb    (%rdx), %al
  41.     movb    %al, (%rdi)
  42.     addq    $1, %rdx
  43.     addq    $1, %rdi
  44.     testb   %al, %al
  45.     jne fp_cpy_loop
  46.     movb    $0, (%rdi)
  47.     ret    
  48.  
  49. /***********************************************************************************/
  50.  
  51. void new_find_pattern(char *output, char *input)
  52. {
  53.     int i = 0, j;
  54.     char *s1, *s2;
  55.     char *c1, *c2;
  56.  
  57.     while (1) {
  58.         i++;
  59.         c1 = input;
  60.         while (1) {
  61.             j = i;
  62.             s1 = c1;
  63.             s2 = input;
  64.             while (j) {
  65.                 if (*s1++ != *s2++)
  66.                     break;
  67.                 j--;
  68.             }
  69.             if (j) {
  70.                 break;
  71.             }
  72.             c1 += i;
  73.             if (!*c1) {
  74.                 c1 -= i;
  75.                 for (c2 = output; *c1; c2++)
  76.                     *c2 = *c1++;
  77.                 *c2 = '\0';
  78.                 return;
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. /***********************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment