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

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