memcpy

replace

Mar 22nd, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .globl asm_str_rep
  2. asm_str_rep:
  3.     testq   %rsi, %rsi
  4.     je  rp_out
  5.     testq   %rdx, %rdx
  6.     je  rp_out
  7. rp_loop:
  8.     xorq    %rbx, %rbx
  9. rp_comp:
  10.     movb    (%rdx, %rbx), %al
  11.     testb   %al, %al
  12.     je  rp_bgrp
  13.     cmpb    %al, (%rsi, %rbx)
  14.     jne rp_copy
  15.     incq    %rbx
  16.     jmp rp_comp
  17. rp_bgrp:
  18.     addq    %rbx, %rsi
  19.     xorq    %rbx, %rbx
  20. rp_repl:
  21.     movb    (%rcx, %rbx), %al
  22.     testb   %al, %al
  23.     je  rp_loop
  24.     movb    %al, (%rdi)
  25.     incq    %rbx
  26.     incq    %rdi
  27.     jmp rp_repl
  28. rp_copy:
  29.     movb    (%rsi), %al
  30.     movb    %al, (%rdi)
  31.     incq    %rsi
  32.     incq    %rdi
  33.     testb   %al, %al
  34.     jne rp_loop
  35. rp_out:
  36.     ret
  37.  
  38. /* C version */
  39.  
  40. void new_str_rep(char *dst, const char *src, const char *find, const char *rep)
  41. {
  42.     if (!src || !find)
  43.         return;
  44.  
  45.     size_t i;
  46.  
  47.     while (*src) {
  48.         for (i = 0; find[i] == src[i] && find[i]; ) {
  49.             i++;
  50.         }
  51.         if (!find[i]) {
  52.             src += i;
  53.             for (i = 0; rep[i]; i++)
  54.                 *dst++ = rep[i];
  55.             continue;
  56.         }
  57.         *dst++ = *src++;
  58.     }
  59.     *dst = '\0';
  60. }
Advertisement
Add Comment
Please, Sign In to add comment