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

  1. ;
  2. ; *** Listing 11-10 ***
  3. ;
  4. ; Counts the number of times the letter 'A'
  5. ; appears in a byte-sized array, using REPNZ SCASB.
  6. ;
  7.     jmp    Skip
  8. ;
  9. ByteArray    label    byte
  10.     db    'ARRAY CONTAINING THE LETTER ''A'' 4 TIMES'
  11. ARRAY_LENGTH    equ    ($-ByteArray)
  12. ;
  13. ; Counts the number of occurrences of the specified byte
  14. ; in the specified byte-sized array.
  15. ;
  16. ; Input:
  17. ;    AL = byte of which to count occurrences
  18. ;    CX = array length (0 means 64K)
  19. ;    DS:DI = array to count byte occurrences in
  20. ;
  21. ; Output:
  22. ;    DX = number of occurrences of the specified byte
  23. ;
  24. ; Registers altered: CX, DX, DI, ES
  25. ;
  26. ; Direction flag cleared
  27. ;
  28. ; Note: Does not handle arrays that are longer than 64K
  29. ;    bytes or cross segment boundaries. Does not handle
  30. ;    overlapping strings.
  31. ;
  32. ByteCount:
  33.     push    ds
  34.     pop    es        ;SCAS uses ES:DI
  35.     sub    dx,dx        ;set occurrence counter to 0
  36.     cld
  37.     and    cx,cx        ;64K long?
  38.     jnz    ByteCountLoop    ;no
  39.     dec    cx        ;yes, so handle first byte
  40.                 ; specially, since JCXZ will
  41.                 ; otherwise conclude that
  42.                 ; we're done right away
  43.     scasb            ;is first byte a match?
  44.     jz    ByteCountCountOccurrence
  45.                 ;yes, so count it
  46. ByteCountLoop:
  47.     jcxz    ByteCountDone    ;if there's nothing left to
  48.                 ; search, we're done
  49.     repnz    scasb        ;search for the next byte
  50.                 ; occurrence or the end of
  51.                 ; the array
  52.     jnz    ByteCountDone    ;no match
  53. ByteCountCountOccurrence:
  54.     inc    dx        ;count this occurrence
  55.     jmp    ByteCountLoop    ;check the next byte, if any
  56. ByteCountDone:
  57.     ret
  58. ;
  59. Skip:
  60.     call    ZTimerOn
  61.     mov    al,'A'        ;byte of which we want a
  62.                 ; count of occurrences
  63.     mov    di,offset ByteArray
  64.                 ;array we want a count for
  65.     mov    cx,ARRAY_LENGTH    ;# of bytes to check
  66.     call    ByteCount    ;get the count
  67.     call    ZTimerOff
  68.