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

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