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

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