home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / jsage / znode3 / z3util / zwc.lbr / ZWC.ZZ0 / ZWC.Z80
Encoding:
Text File  |  1993-06-07  |  5.9 KB  |  243 lines

  1. Z3ENV    DEFL    0FE00H
  2. VERS    EQU    10
  3. ; 5/31/86
  4. ; Adapted for ZCPR3 by Bruce Morgen, with display
  5. ; enhancements, now requires a REL-capable assembler
  6. ;    W C . Z 8 0   (word counter)
  7. ;    David Coons (cis [72435,136])
  8. ;    V1.0 February 1984
  9. ;
  10. ;    Abstract
  11. ;    --------
  12. ;       Counts words in any file and displays
  13. ;    count on con: in the form "FILE CONTAINS
  14. ;    00000 WORDS.".    A word is defined as any
  15. ;    character sequence ended by spaces, tabs,
  16. ;    a carriage return, or line feed.  ignores
  17. ;    high order bit in any character.
  18. ;       Uses word count algorithm from "THE C
  19. ;    PROGRAMMING LANGUAGE" (BW Kernighan and DM
  20. ;    Ritchie, Englewood Cliffs, NJ: Prentice-
  21. ;    Hall, 1978), page 18.
  22. ;       written in Z80 assembly language
  23. ;    (assemble with Z80ASM, zas or zasm)
  24. ;
  25. ;    usage:
  26. ;        zwc [du: or dir:]filename.ext
  27. ;        (ambiguous file names not allowed)
  28. ;    execution timing:
  29. ;        a 3200-word file was counted in
  30. ;        approximately seven seconds on a
  31. ;        floppy-based system.
  32. ; --------------------------------------------------
  33.     EXTRN FENV,PRNDU    ;separate modules
  34.     EXTRN Z3INIT,Z3LOG,GETMDISK,GETMUSER,GETCRT,DUTDIR
  35.     EXTRN EPRINT,RETUD,PFN2,SETDMA,CRLF,COUT,PHLFDC
  36. ;
  37. YES    EQU    -1
  38. NO    EQU    NOT YES
  39. ZERO    EQU    0
  40. BDOS    EQU    0005H        ; Bdos entry addr
  41. FOPEN    EQU    15        ; Open file
  42. FREAD    EQU    20        ; Sequential read
  43. FCB    EQU    005CH        ; Fcb addr
  44. DRIVE    EQU    FCB        ; Drive
  45. FILNAM    EQU    FCB+01H        ; File name
  46. EXTENT    EQU    FCB+0CH        ; Extent number
  47. CURREC    EQU    FCB+20H        ; Current record
  48. BUFFER    EQU    0080H        ; Disk i/o buffer
  49. TAB    EQU    09H        ; Horizontal tab
  50. CR    EQU    0DH        ; Carriage return
  51. LF    EQU    0AH        ; Line feed
  52. SPACE    EQU    ' '        ; Space
  53. MASK    EQU    01111111B    ; Saves 7 bits
  54. ; --------------------------------------------------
  55. START:
  56. ; verify a file name is present in command tail
  57.     LD    HL,ZERO        ; Zero out HL
  58.     LD    (NW),HL        ; Initialize word count
  59.     ADD    HL,SP        ; Acquire system stack
  60.     LD    (STACK),HL    ; Store its address
  61.     LD    SP,STACK    ; Set local stack
  62.     LD    HL,BUFFER+80H    ; Initial buffer address
  63.     LD    (BUFADR),HL    ; into its data area
  64.     XOR    A        ; Zero out A
  65.     LD    (INWORD),A    ; Init in-the-word flag
  66.     CALL    FENV        ; Acquire ENV address
  67.     JP    NZ,ENVOK    ; Give up if not found
  68.     CALL    EPRINT
  69.     DB    'Apparently this is not a ZCPR3 system: Aborting!'0
  70.     JP    FINIS
  71. ENVOK:    CALL    Z3INIT        ; Pass to Z3LIB
  72.     CALL    EPRINT
  73.     DB    'ZWC, Version '
  74.     DB    [VERS/10]+'0','.',[VERS    MOD 10]+'0',2CH
  75.     DB    ' a ZCPR3 word counter',CR,LF,LF,0
  76.     LD    A,(FCB+1)
  77.     CP    '/'
  78.     JP    Z,ZHELP
  79.     CP    ' '
  80.     JP    NZ,CHKDRV
  81. ;
  82.     CALL    EPRINT
  83.     DB    CR,LF,'No file specified,',CR,LF,LF0
  84.     JP    ZHELP
  85. CHKDRV:
  86. ; verify specified drive with the ZCPR3 environment.
  87.     LD    A,(DRIVE)    ; Get drive #
  88.     LD    B,A
  89.     CALL    GETMDISK
  90.     CP    B
  91.     JP    NC,CHKUSR
  92.     CALL    EPRINT
  93.     DB    'Drive code is not valid',CR,LF,LF,0
  94.     JP    FINIS        ; End program
  95. CHKUSR:
  96. ; does Z3 approve of user area?
  97.     LD    A,(FCB+13)
  98.     LD    B,A
  99.     CALL    GETMUSER
  100.     CP    B
  101.     JP    NC,CHKNAM
  102.     CALL    EPRINT
  103.     DB    'User code is not valid',CR,LF,LF,0
  104.     JP    FINIS
  105. CHKNAM:
  106. ; verify that file name includes no '?'
  107.     LD    HL,FILNAM-1    ; Load addr of name
  108.     LD    B,11        ; Chk name(8)+ext(3)
  109.     LD    A,'?'        ; Search argument
  110. LOOP1:
  111.     INC    HL        ; Point to next char
  112.     CP    (HL)        ; '?' present?
  113.     JP    Z,NOAMB        ; Yes; print err msg
  114.     DEC    B        ; Decrement counter
  115.     JP    NZ,LOOP1    ; Jmp if more chars
  116.     JP    NAMOK        ; Check done and ok
  117. NOAMB:    CALL    EPRINT
  118.     DB    'Ambiguous file names not allowed'
  119.     DB    CR,LF,LF,0
  120.     JP    ZHELP        ; End program
  121. NAMOK:
  122. ; open file
  123.     XOR    A        ; Zero accumulator
  124.     LD    (EXTENT),A    ; Set extent to zero
  125.     LD    (CURREC),A    ; Set curr rec to 0
  126.     LD    DE,FCB        ; Load fcb addr
  127.     CALL    Z3LOG
  128.     CALL    RETUD
  129.     CALL    PRNDU
  130.     INC    DE
  131.     CALL    PFN2
  132.     CALL    DUTDIR
  133.     JP    Z,OPENIT
  134.     CALL    EPRINT
  135.     DB    2CH,' in the ',0
  136.     LD    B,8
  137. NDRLOP:    LD    A,(HL)
  138.     CP    SPACE
  139.     JP    Z,NDRMSG
  140.     CALL    COUT
  141.     INC    HL
  142.     DEC    B
  143.     JP    NZ,NDRLOP
  144. NDRMSG:    CALL    EPRINT
  145.     DB    ' directory',2CH,0
  146.     CALL    GETCRT
  147.     LD    A,(HL)
  148.     CP    52
  149.     CALL    C,CRLF
  150. OPENIT:    DEC    DE
  151.     LD    C,FOPEN        ; Bdos function 15
  152.     CALL    BDOS        ; Attempt to open
  153.     CP    0FFH        ; Open ok?
  154.     JP    NZ,PREP        ; Yes
  155. ;
  156.     CALL    EPRINT
  157.     DB    ' does not exist.'
  158.     DB    CR,LF,LF,0
  159.     JP    FINIS        ; End program
  160. PREP:
  161.     LD    HL,BUFFER    ; Load buffer addr
  162.     CALL    SETDMA        ; Set buffer addr
  163. MLOOP:
  164. ; main program loop
  165.     LD    HL,(BUFADR)    ; Get pointer
  166.     LD    A,L        ; Get lsb of pointer
  167.     OR    A        ; End of buffer?
  168.     JP    NZ,GETBYT    ; No, process byte
  169. ;
  170.     LD    HL,BUFFER    ; Load buffer start
  171.     LD    (BUFADR),HL    ; Reset buffer ptr
  172.     LD    DE,FCB        ; Load fcb addr
  173.     LD    C,FREAD        ; Bdos function 20
  174.     CALL    BDOS        ; Get next record
  175.     OR    A        ; More of file?
  176.     JP    NZ,RESULT    ; No, end
  177. GETBYT:
  178.     LD    A,(HL)        ; Get curr byte
  179.     AND    MASK        ; Mask off 8th bit
  180.     LD    HL,(BUFADR)    ; Load pointer
  181.     INC    HL        ; Point to next byte
  182.     LD    (BUFADR),HL    ; Save pointer
  183. ; check for white space
  184.     CP    SPACE        ; Space?
  185.     JP    Z,ENDWRD    ; Yes, check word
  186.     CP    CR        ; Carriage return?
  187.     JP    Z,ENDWRD    ; Yes, check word
  188.     CP    LF        ; Line feed?
  189.     JP    Z,ENDWRD    ; Yes, check word
  190.     CP    TAB        ; Tab?
  191.     JP    Z,ENDWRD    ; Yes, check word
  192. ; check indicator; if no, set and count
  193.     LD    A,(INWORD)    ; Get indicator
  194.     CP    NO        ; Is it reset?
  195.     JP    NZ,MLOOP    ; No, repeat loop
  196. ;
  197.     LD    BC,YES        ; Load 'yes'
  198.     LD    A,B        ; Move to accum
  199.     LD    (INWORD),A    ; Store in indicator
  200. ; increment counter and repeat loop
  201.     LD    HL,(NW)        ; Load counter
  202.     INC    HL        ; Increment
  203.     LD    (NW),HL        ; Save counter
  204.     JP    MLOOP
  205. ;
  206. ENDWRD:
  207. ; at end of word; reset indicator
  208.     LD    BC,NO        ; Load 'no'
  209.     LD    A,B        ; Move to accum
  210.     LD    (INWORD),A    ; Reset indicator
  211.     JP    MLOOP        ; Repeat loop
  212. ;
  213. RESULT:
  214. ; display first half of results message
  215.     CALL    EPRINT
  216.     DB    ' contains ',0
  217. ; display number of words
  218.     LD    HL,(NW)        ; Load word count
  219.     CALL    PHLFDC
  220. ; display second half of results message
  221.     CALL    EPRINT
  222.     DB    ' words.',CRRRE
  223. FINIS:
  224.     LD    HL,(STACK)
  225.     LD    SP,HL
  226.     RET
  227. ; --------------------------------------------------
  228. ZHELP:    CALL    EPRINT
  229.     DB    'Syntax:'
  230.     DB    CR,LF,TAB
  231.     DB    'ZWC [du: or dir:]ufn',CR,LF,0
  232.     JP    FINIS
  233. ; --------------------------------------------------
  234. ; storage areas
  235. BUFADR:    DS    2        ; Buffer pointer
  236. INWORD:    DS    1        ; Yes = curr in word
  237.                 ; No  ,CRRbetween words
  238. NW:    DS    2        ; Word count
  239. ;
  240.     DS    32
  241. STACK:    DS    2
  242.     END
  243.