Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * both functions do the same thing, find repeating pattern in a string
- * in assembly, uses 7 different instructions, occupies 31 lines
- * in C, uses (a lot) of stack instructions, also occupies 31 lines
- */
- /*
- * speed comparison, two different tests were done (different strings)
- * results (in cycles):
- * 1) c version - 782 | asm version - 357
- * 2) c version - 1127 | asm version - 425
- */
- /***********************************************************************************/
- .globl _asm_find_pattern
- _asm_find_pattern:
- movq $0, %rcx
- fp_out_loop:
- addq $1, %rcx
- movq %rsi, %rdx
- fp_inf_loop:
- movq %rcx, %rbx
- movq %rsi, %r8
- movq %rdx, %r9
- fp_cmp_loop:
- addq $1, %r8
- addq $1, %r9
- movb -1(%r8), %al
- cmpb %al, -1(%r9)
- jne fp_out_loop
- subq $1, %rbx
- jne fp_cmp_loop
- addq %rcx, %rdx
- cmpb $0, (%rdx)
- jne fp_inf_loop
- subq %rcx, %rdx
- fp_cpy_loop:
- movb (%rdx), %al
- movb %al, (%rdi)
- addq $1, %rdx
- addq $1, %rdi
- testb %al, %al
- jne fp_cpy_loop
- movb $0, (%rdi)
- ret
- /***********************************************************************************/
- void new_find_pattern(char *output, char *input)
- {
- int i = 0, j;
- char *s1, *s2;
- char *c1, *c2;
- while (1) {
- i++;
- c1 = input;
- while (1) {
- j = i;
- s1 = c1;
- s2 = input;
- while (j) {
- if (*s1++ != *s2++)
- break;
- j--;
- }
- if (j) {
- break;
- }
- c1 += i;
- if (!*c1) {
- c1 -= i;
- for (c2 = output; *c1; c2++)
- *c2 = *c1++;
- *c2 = '\0';
- return;
- }
- }
- }
- }
- /***********************************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment