home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / zendisk2.zip / LST14-6.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  2KB  |  82 lines

  1. ;
  2. ; *** Listing 14-6 ***
  3. ;
  4. ; Searches for the first appearance of a character, in any
  5. ; case, in a byte-sized array by using JZ and LOOP.
  6. ;
  7.     jmp    Skip
  8. ;
  9. ByteArray    label    byte
  10.     db    'Array Containing Both Upper and Lowercase'
  11.     db    ' Characters And Blanks'
  12. ARRAY_LENGTH    equ    ($-ByteArray)
  13. ;
  14. ; Finds the first occurrence of the specified character, in
  15. ; any case, in the specified byte-sized array.
  16. ;
  17. ; Input:
  18. ;    AL = character for which to perform a
  19. ;        case-insensitive search
  20. ;    CX = array length (0 means 64K long)
  21. ;    DS:SI = array to search
  22. ;
  23. ; Output:
  24. ;    SI = pointer to first case-insensitive match, or 0
  25. ;        if no match is found
  26. ;
  27. ; Registers altered: AX, CX, SI
  28. ;
  29. ; Direction flag cleared
  30. ;
  31. ; Note: Does not handle arrays that are longer than 64K
  32. ;    bytes or cross segment boundaries.
  33. ;
  34. ; Note: Do not pass an array that starts at offset 0 (SI=0),
  35. ;    since a match on the first byte and failure to find
  36. ;    the byte would be indistinguishable.
  37. ;
  38. CaseInsensitiveSearch:
  39.     cld
  40.     cmp    al,'a'
  41.     jb    CaseInsensitiveSearchBegin
  42.     cmp    al,'z'
  43.     ja    CaseInsensitiveSearchBegin
  44.     and    al,not 20h    ;make sure the search byte
  45.                 ; is uppercase
  46. CaseInsensitiveSearchBegin:
  47.     mov     ah,al        ;put the search byte in AH
  48.                 ; so we can use AL to hold
  49.                 ; the bytes we're checking
  50. CaseInsensitiveSearchLoop:
  51.     lodsb            ;get the next byte from the
  52.                 ; array being searched
  53.     cmp    al,'a'
  54.     jb    CaseInsensitiveSearchIsUpper
  55.     cmp    al,'z'
  56.     ja    CaseInsensitiveSearchIsUpper
  57.     and    al,not 20h    ;make sure the array byte is
  58.                 ; uppercase
  59. CaseInsensitiveSearchIsUpper:
  60.     cmp    al,ah        ;do we have a
  61.                 ; case-insensitive match?
  62.     jz    CaseInsensitiveSearchMatchFound    ;yes
  63.     loop    CaseInsensitiveSearchLoop
  64.                 ;check the next byte, if any
  65.     sub    si,si        ;no match found
  66.     ret
  67. CaseInsensitiveSearchMatchFound:
  68.     dec    si        ;point back to the matching
  69.                 ; array byte
  70.     ret
  71. ;
  72. Skip:
  73.     call    ZTimerOn
  74.     mov    al,'K'        ;character to search for
  75.     mov    si,offset ByteArray ;array to search
  76.     mov    cx,ARRAY_LENGTH    ;# of bytes to search
  77.                 ; through
  78.     call    CaseInsensitiveSearch
  79.                 ;perform a case-insensitive
  80.                 ; search for 'K'
  81.     call    ZTimerOff
  82.