Skip to content

Instantly share code, notes, and snippets.

@skhokhlov
Created June 17, 2016 14:39
Show Gist options
  • Save skhokhlov/46169fdb20d3d0612dfb715b15de8087 to your computer and use it in GitHub Desktop.
Save skhokhlov/46169fdb20d3d0612dfb715b15de8087 to your computer and use it in GitHub Desktop.
int main() {
const int lens = 11;
char source[lens] = {'e', 'e', 's', 't', ' ', 't', 't', 'l', 'i', 'n', 'g'};
const int lenp = 2;
char p[lenp] = {'t', 't'};
int i, j;
int d[lenp];
int res[lenp];
int resi = 0;
asm {
;prefix
mov d[0], 0
mov eax, 0 ; j=0
Loop1:
mov ecx, 0 ; i=0
jmp Loop1While1 ; j != 0 && p[j] != p[i]
Loop1While1Body:
mov eax, d[eax*eax - 1]
jmp Loop1While1
Loop1While1:
cpm eax, 0 ; j == 0
jne Loop1While2
jmpLoop1If1
Loop1While2:
mov edx, p[edx * eax]
cpm edx, p[edx * ecx]
jne Loop1While1Body
jmp Loop1If1
Loop1If1:
mov edx, p[edx*eax]
cmp edx, p[edx*ecx] ; p[j] == p[i]
je Loop1If1Body
jmp Loop1Body
Loop1If1Body:
inc eax
Loop1Body:
mov d[esi * ecx], eax
cmp ecx, lenp ; i < lenp
je Loop1End
inc ecx
jmp Loop1
Loop1End:
;search
mov eax,0 ; j=0
Loop2:
mov ecx,0 ; i=0
jmp Loop2While1
Loop2While1Body:
mov eax, d[eax*eax - 1]
jmp Loop2While1
Loop2While1:
cpm eax, 0 ; j == 0
jne Loop2While2
jmp Loop2If1
Loop2While2:
mov edx, source[edx * eax]
cpm edx, p[edx * ecx]
jne Loop2While1Body
jmp Loop2If1
Loop2If1:
mov edx, p[edx*eax]
cmp edx, source[edx*ecx] ; p[j] == source[i]
je Loop2If1Body
jmp Loop2If2
Loop2If1Body:
inc eax
Loop2If2:
mov edx, lenp
cmp edx, eax ; j == lenp
je Loop2If2Body
jmp Loop2Body
Loop2If2Body:
mov edx, resi
add edx, 1
mov ebx, ecx
sub ebx, eax
add ebx, 1 ; i - j + 1
mov res[esi*edx], ebx
jmp Loop2Body
Loop2Body:
mov d[esi * ecx], eax
cmp ecx, lens ; i < lens
je Loop2End
inc ecx
jmp Loop2
Loop2End:
}
// /* Вычисление префикс-функции */
// d[0] = 0;
// j = 0;
// for (i = 1; i < lenp; i++) {
// while (j > 0 && p[j] != p[i])
// j = d[j - 1];
//
// if (p[j] == p[i])
// j++;
//
// d[i] = j;
// }
//
// /* поиск */
// j = 0;
// for (i = 0; i < lens; i++) {
// while (j > 0 && p[j] != source[i])
// j = d[j - 1];
//
// if (p[j] == source[i])
// j++;
//
// if (j == lenp) {
// res[resi++] = i - j + 1;
// }
// }
for(i=0;i<resi;i++) printf('%d',res[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment