Advertisement
Guest User

Untitled

a guest
May 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. global _main
  2. bits 32
  3.  
  4. section .data
  5.   msg1 db "Enter string:", 0xa, 0
  6.   msg1_blen equ $-msg1
  7.   msg2 db "Enter substring:", 0xa, 0
  8.   msg2_blen equ $-msg2
  9.  
  10. section .bss
  11.   string resb 255
  12.   string_blen equ $-string
  13.   string_len resb 10
  14.  
  15.   substring resd 255
  16.   substring_len resb 10
  17.   substring_blen equ $-substring
  18.  
  19.   mas resd 255
  20.   i resb 1
  21.   j resb 2
  22.  
  23. section .text
  24. _main:
  25.   ; arr test
  26.   ; mov dword[mas + 4 * 0], 5
  27.   ; mov dword[mas + 4 * 1], 9
  28.   ; mov dword[mas + 4 * 2], 10
  29.  
  30.   ; write msg2
  31.   ; sub esp, 4
  32.   ; push dword msg2_blen
  33.   ; push dword msg2
  34.   ; push dword 1
  35.   ; call sys_write
  36.   ; add esp, 16
  37.  
  38.   ; read substring
  39.   push dword substring_blen
  40.   push dword substring
  41.   push dword 1
  42.   call sys_read
  43.   add esp, 16
  44.  
  45.   ; save substring len
  46.   dec eax
  47.   mov [substring_len], eax
  48.   mov eax, 0
  49.  
  50.   ; определяем первый элемент массива, i, j
  51.   mov dword[mas + 4 * 0], 0
  52.   mov dword[ebp-4], 1 ;i=1
  53.   mov dword[ebp-8], 0 ;j=0
  54.  
  55.   ; for ($i = 1, $j = 0; $i < $m; $i++)
  56.   for:
  57.   ; $i < $m (+)
  58.   mov ebx, dword[ebp-4]
  59.   cmp ebx, [substring_len]
  60.   jnl for_end
  61.  
  62.   ; while($j > 0 && $substr[$j] != $substr[$i])
  63.   while:
  64.   ; определяем адрес $substr[$j]
  65.   mov ebx, substring
  66.   mov ecx, dword[ebp-8]
  67.   cmp dword[ebp-8], 0
  68.   mov ebx, 0
  69.   jne inc_address
  70.   mov eax, ebx ; сохраняем адрес в eax
  71.  
  72.   ; определяем адрес $substr[$i]
  73.   mov ebx, substring
  74.   mov ecx, dword[ebp-4]
  75.   jmp inc_address
  76.   mov edx, ebx ; сохраняем адрес в edx
  77.  
  78.   ; $substr[$j] != $substr[$i]
  79.   cmp eax, edx
  80.   je if
  81.   ; $j > 0
  82.   mov ebx, dword[ebp-8]
  83.   cmp ebx, 0
  84.   jng if
  85.   ; $j = $d[$j - 1];
  86.   dec ebx
  87.   mov ebx, dword[mas + ebx]
  88.   mov dword[ebp-8], ebx
  89.   jmp while
  90.  
  91.   if:
  92.   ; определяем адрес $substr[$j]
  93.   mov ebx, substring
  94.   mov ecx, dword[ebp-8]
  95.   cmp dword[ebp-8], 0
  96.   mov ebx, 0
  97.   jne inc_address
  98.   mov eax, ebx ; сохраняем адрес в eax
  99.  
  100.   ; определяем адрес $substr[$i]
  101.   mov ebx, substring
  102.   mov ecx, dword[ebp-4]
  103.   jmp inc_address
  104.   mov edx, ebx ; сохраняем адрес в edx
  105.  
  106.   cmp eax, edx
  107.   jne appropriation
  108.  
  109.   ; $d[$i] = $j;
  110.   appropriation:
  111.   mov ebx, dword[ebp-4]
  112.   mov ecx, dword[ebp-8]
  113.   mov dword[mas + ebx], ecx
  114.  
  115.   ; увеличить i
  116.   inc ebx
  117.   mov dword[ebp-4], ebx
  118.  
  119.   jmp for
  120.  
  121.   ; выход из for
  122.   for_end:
  123.   jmp sys_exit
  124.  
  125. sys_write:
  126.   pop ebx ; save link on next segment
  127.   mov eax, 0x4 ; sys_write
  128.   sub esp, 4 ; change head of stack
  129.   int 0x80 ; interrupt
  130.   push ebx ; push link on next segment to stack
  131.   ret
  132.  
  133. sys_read:
  134.   pop ebx ; save link on next segment
  135.   mov eax, 0x3
  136.   sub esp, 4
  137.   int 0x80
  138.   push ebx ; push link on next segment to stack
  139.   ret
  140.  
  141. inc_address:
  142.   inc ebx
  143.   loop inc_address
  144.   ret
  145.  
  146. sys_exit:
  147.   push dword 0 ; status
  148.   mov eax, 0x1 ; sys_exit
  149.   sub esp, 4 ; change head of stack
  150.   int 0x80 ; interrupt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement