Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. .386
  2. .model flat
  3. .code
  4.  
  5. ; Algoritmo in pseudocodice
  6. ; int salvaparolaspiazzamento (char *s, char *d, unsigned char* parola1)
  7. ; *** USED subroutines (at the end of this file): SEARCH, STRLEN, STRCPY ***
  8. ; ------------------------------------------------------------------------------
  9. ; src = s
  10. ; dst = d
  11. ;
  12. ; riempire d con blank;
  13. ; len = strlen(parola1);
  14. ; if(len < 4) return(-2);       // parola1's length is less than 4 characters
  15. ; while(1){
  16. ;   pos = search(src,parola1)
  17. ;   if(pos == -1){
  18. ;       if(src==s)  return(-1); //  parola1 doesn't occur in s
  19. ;       else        return(0)//  parola1 occurs one or more times in s
  20. ;   }
  21. ;   else{
  22. ;       src += len + pos;
  23. ;       dst += pos;
  24. ;       strcpy(parola1, dst)
  25. ;       dst += len;
  26. ;   }
  27. ; }
  28. ; ------------------------------------------------------------------------------
  29. ;int salvaparolaspiazzamento (char *s, char *d, unsigned char* parola1)
  30.  
  31. _salvaparolaspiazzamento proc
  32.     push    ebp
  33.     mov     ebp,esp
  34.                                     ; save regs
  35.     push    ebx                     ;
  36.     push    ecx                     ;
  37.     push    edi                     ;
  38.     push    esi                     ;
  39. ;
  40.     mov     esi, dword ptr[ebp+8]   ; src = s
  41.     mov     edi, dword ptr[ebp+12]  ; dst = d
  42. ;
  43.                                     ; fill d string with spaces:
  44.     mov     ebx,esi                 ;   eax = strlen(ebx=d)
  45.     call    strlen                  ;  
  46.                                     ;   build string d
  47.     mov     byte ptr[edi+eax],0     ;   add NULL at the end of the d string, termination
  48.     dec     eax                     ;
  49. blfill:                             ;
  50.     mov     byte ptr[edi+eax],' '   ;   fill with blank
  51.     dec     eax                     ;
  52.     jge     blfill                  ;
  53.                                     ; if(strlen(parola1) < 4) return(-2)
  54.     mov     ebx, dword ptr[ebp+16]  ;   *parola1
  55.     call    strlen                  ;
  56.     mov     ecx,eax                 ;   save strlen in to ecx                          
  57.     cmp     eax,4                   ;
  58.     jge     cerca                   ;
  59.     mov     eax,-2                  ;
  60.     jmp     fine                    ;
  61.                                     ;
  62. cerca:                              ; while(1){
  63.     call    search                  ;   pos (stored in to eax) = search(src,parola1);
  64.     cmp     eax, -1                 ;   if(pos == -1){
  65.     jne     trovata                 ;  
  66.     cmp     esi,dword ptr[ebp+8]    ;       if(src==s)  return(-1);
  67.     je      fine                    ;  
  68.     mov     eax,0                   ;       else        return(0);
  69.     jmp     fine                    ;   }
  70. trovata:                            ;   else{
  71.     add     esi,eax                 ;       src += len + pos;
  72.     add     esi,ecx                 ;
  73.     add     edi,eax                 ;       dst += len + pos;
  74.     call    strcpy                  ;       strcpy(parola1, dst);
  75.     add     edi,ecx                 ;       dst += len;
  76.     jmp     cerca                   ;   }
  77.                                     ; }
  78. fine:
  79.     pop     esi                     ;   restore regs
  80.     pop     edi                     ;
  81.     pop     ecx                     ;
  82.     pop     ebx                     ;
  83.  
  84.     mov     esp,ebp                 ;
  85.     pop     ebp                     ;
  86.     ret                             ;
  87. _salvaparolaspiazzamento endp
  88.  
  89.  
  90. search proc
  91. ; *********************************
  92. ;
  93. ;   search
  94. ;       Search for dst string in to src string,
  95. ;       returns the position of the first match.
  96. ;
  97. ;       input   esi, src string pointer; string to search in;
  98. ;               ebx, dst string pointer; string to look for;
  99. ;       output  eax, position of the first occurrence of dst in src; -1 if no match is found;
  100. ;                    
  101. ; *********************************
  102. ; --------------------------------;
  103. ; i = 0, j = 0
  104. ; while(1){
  105. ;   if (src[j] == NULL)     return(-1);  // No match is found
  106. ;   if (dst[i] == NULL)     return(j-i); // Match found, return match position of dst string in to src string
  107. ;   if (src[j] == dst[i])   i++;
  108. ;   else                    i = 0;
  109. ;   j++;
  110. ; }    
  111. ;
  112. ; --------------------------------
  113.     push    ecx                 ;
  114.     push    edx                 ;
  115. ;
  116.     mov     eax,0               ; j = 0, index of source string
  117.     mov     edx,0               ; i = 0, index of destination string
  118. ;
  119. ripeti:                         ; while(1) {
  120.     mov ch, byte ptr[ebx+edx]   ;   if (dst[i] == NULL) return(i-j);
  121.     cmp ch,0                    ;
  122.     jne riparti                 ;
  123. ;
  124.     sub eax,edx ; j <-(j-i)     ;
  125.     pop edx                     ;
  126.     pop ecx                     ;
  127.     ret                         ;
  128. ;
  129. riparti:
  130.     mov cl, byte ptr[esi+eax]   ;   if (src[j] == NULL) return(-1);
  131.     cmp cl,0                    ;
  132.     jne compare                 ;
  133.     mov eax,-1      ;return(-1) ;
  134.     pop edx                     ;
  135.     pop ecx                     ;
  136.     ret                         ;
  137. ;
  138. compare:
  139. ;
  140.     cmp cl,ch                   ;   if (src[j] == dst[i])   i++;
  141.     jne notrov                  ;   else                    i = 0;
  142.                                 ;   j++
  143.     inc edx         ; i++       ;
  144.     inc eax         ; j++       ;
  145.     jmp ripeti                  ; }
  146. ;
  147. notrov:
  148.     inc eax         ; j++       ;
  149.     cmp edx,0                   ;
  150.     je riparti                  ;
  151.     mov edx,0       ; i = 0     ;
  152.     jmp ripeti                  ;
  153. ;
  154. search endp
  155.  
  156.  
  157. strcpy proc
  158. ; *********************************
  159. ;
  160. ;   strcpy: copy src string to dst
  161. ;           (Null is not copyed)
  162. ;   input  ebx = *src, edi = *dst
  163. ;
  164. ; *********************************
  165.     push    ecx                     ;
  166.     push    eax                     ;
  167. ;
  168.     mov     eax,0                   ;
  169. inizio:
  170.     mov     cl,byte ptr[ebx+eax]    ;
  171.     cmp     cl,0                    ;
  172.     je      fine                    ;
  173.     mov     byte ptr[edi+eax], cl   ;
  174.     inc     eax                     ;
  175.     jmp     inizio                  ;
  176. fine:
  177.     pop     eax                     ;
  178.     pop     ecx                     ;
  179.     ret                             ;
  180. ;
  181. strcpy endp
  182. ;----------------------------------
  183.  
  184.  
  185.  
  186. strlen proc
  187. ; *********************************
  188. ;   strlen
  189. ;       input ebx = string pointer
  190. ;       output eax str length
  191. ; *********************************
  192.     mov eax,0
  193. inizio:
  194.     cmp byte ptr[ebx+eax],0
  195.     je  fine
  196.     inc eax
  197.     jmp inizio
  198. fine:
  199.     ret
  200. ;
  201. strlen endp
  202.  
  203.  
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement