home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / ml_os2 / szsearch.asm < prev   
Encoding:
Assembly Source File  |  1992-11-12  |  3.1 KB  |  121 lines

  1. ;
  2. ; szSearch - An example of 32 bit assembly programming using MASM 6.1
  3. ;
  4. ; Purpose: search a buffer (rgbSearch) of length cbSearch for the
  5. ;          first occurance of szTok (null terminated string).
  6. ;
  7. ; Method: A variation of the Boyer-Moore method.
  8. ;        Determine length of szTok (n)
  9. ;        set array of flags (rgfInTok) to true for each character in
  10. ;                szTok
  11. ;        set current position of search to rgbSearch (pbCur)
  12. ;    
  13. ;        compare current position to szTok by searching backwards from
  14. ;                nth position. When a comparison fails at position
  15. ;                (m), check to see if the current character in
  16. ;                rgbSearch is in szTok by using rgfInTok. If not, set
  17. ;                pbCur to pbCur+(m)+1 and restart compare. If pbCur is
  18. ;                reached, increment pbCur and restart compare.
  19. ;
  20. ;        reset rgfInTok to all 0 for next instantiation of the routine.
  21.  
  22. .386
  23. .model flat,stdcall
  24.  
  25. FALSE    EQU    0
  26. TRUE    EQU    NOT FALSE
  27.  
  28. .data
  29.     ; flags buffer - data initialized to FALSE. We will 
  30.     ; set the appropriate flags to TRUE during initialization
  31.     ; of szSearch and reset them to FALSE before exit.
  32.     rgfInTok    db 256 dup(FALSE);
  33.  
  34. .code
  35.  
  36. PBYTE    TYPEDEF PTR BYTE
  37.  
  38. szSearch PROC PUBLIC USES esi edi, 
  39.     rgbSearch:PBYTE, 
  40.     cbSearch:DWORD, 
  41.     szTok:PBYTE
  42.  
  43.     ; Initialize flags buffer. This tells us if a character is in
  44.     ; the search token - Note how we use eax as an index
  45.     ; register. This can be done with all extended registers.
  46.     mov    esi,szTok
  47.     xor    eax,eax
  48.     .REPEAT
  49.         lodsb
  50.         mov    BYTE PTR rgfInTok[eax],TRUE
  51.     .UNTIL (!AL)
  52.  
  53.     ; save count of szTok bytes in edx
  54.     mov    edx,esi
  55.     sub    edx,szTok
  56.     dec    edx
  57.  
  58.     ; esi will always point to beginning of szTok
  59.     mov    esi,szTok
  60.  
  61.     ; edi will point to current search position
  62.     ; it will also contain the return value
  63.     mov    edi,rgbSearch
  64.  
  65.     ; store pointer to end of rgbSearch in ebx
  66.     mov    ebx,edi
  67.     add    ebx,cbSearch
  68.     sub    ebx,edx
  69.  
  70.     ; initialize ecx with length of szTok    
  71.     mov    ecx,edx
  72.     .WHILE ( ecx != 0 )
  73.         ; mov index to current characters to compare
  74.         dec    ecx
  75.         mov    al,[edi+ecx]
  76.  
  77.         ; if the current byte in the buffer doesn't exist in the 
  78.         ; search token, increment buffer pointer to current position 
  79.         ; +1 and start over. This can skip up to 'edx'
  80.         ; bytes and reduce search time.
  81.         .IF  !(rgfInTok[eax])
  82.             add    edi,ecx
  83.             inc    edi
  84.             ; initialize ecx with length of szTok
  85.             mov    ecx,edx
  86.         ; otherwise, if the characters match, continue on as if
  87.         ; we have a matching token
  88.         .ELSEIF (al == [esi+ecx])
  89.               .CONTINUE
  90.         ; finally, if we have searched all szTok  characters, 
  91.         ; and land here, we have a mismatch and we increment 
  92.         ; our pointer into rgbSearch by one and start over.
  93.         .ELSEIF (!ecx)
  94.             inc    edi
  95.             mov    ecx,edx
  96.         .ENDIF
  97.         
  98.         ; verify that we haven't searched beyond the buffer.
  99.         .IF    (edi > ebx) 
  100.             ; error value
  101.             mov    edi,0
  102.             .BREAK
  103.         .ENDIF
  104.     .ENDW
  105.  
  106.     ; restore flags in rgfInTok to 0 (for next time).
  107.     mov    esi,szTok
  108.     xor    eax,eax
  109.     .REPEAT
  110.         lodsb
  111.         mov    BYTE PTR rgfInTok[eax],FALSE
  112.     .UNTIL !AL
  113.  
  114.     ; put return value in eax
  115.     mov    eax,edi
  116.     ret
  117. szSearch ENDP
  118.  
  119. end
  120.  
  121.