home *** CD-ROM | disk | FTP | other *** search
/ norge.freeshell.org (192.94.73.8) / 192.94.73.8.tar / 192.94.73.8 / pub / computers / cpm / alphatronic / DRIPAK.ZIP / CPM_3-0 / SOURCES / RANDOM.ASM < prev    next >
Assembly Source File  |  1982-12-31  |  11KB  |  359 lines

  1. ;***************************************************
  2. ;*                                                 *
  3. ;* sample random access program for cp/m 3         *
  4. ;*                                                 *
  5. ;***************************************************
  6.         org     100h    ;base of tpa
  7. ;
  8. reboot  equ     0000h   ;system reboot
  9. bdos    equ     0005h   ;bdos entry point
  10. ;
  11. coninp  equ     1       ;console input function
  12. conout  equ     2       ;console output function
  13. pstring equ     9       ;print string until '$'
  14. rstring equ     10      ;read console buffer
  15. version equ     12      ;return version number
  16. openf   equ     15      ;file open function
  17. closef  equ     16      ;close function
  18. makef   equ     22      ;make file function
  19. readr   equ     33      ;read random
  20. writer  equ     34      ;write random
  21. wrtrzf    equ    40    ;write random zero fill
  22. parsef  equ     152    ;parse function
  23. ;
  24. fcb     equ     005ch   ;default file control block
  25. ranrec  equ     fcb+33  ;random record position
  26. ranovf  equ     fcb+35  ;high order (overflow) byte
  27. buff    equ     0080h   ;buffer address
  28. ;
  29. cr      equ     0dh     ;carriage return
  30. lf      equ     0ah     ;line feed
  31. ;
  32. ;***************************************************
  33. ;*                                                 *
  34. ;* load SP, set-up file for random access          *
  35. ;*                                                 *
  36. ;***************************************************
  37.         lxi     sp,stack
  38. ;
  39. ;       version 3.1?
  40.         mvi     c,version
  41.         call    bdos
  42.         cpi     31h     ;version 3.1 or better?
  43.         jnc     versok
  44. ;       bad version, message and go back
  45.         lxi     d,badver
  46.         call    print
  47.         jmp     reboot
  48. ;
  49. versok:
  50. ;       correct version for random access
  51.         mvi     c,openf ;open default fcb
  52. rdname: lda     fcb+1
  53.     cpi    ' '
  54.     jnz    opfile
  55.     lxi    d,entmsg
  56.     call     print
  57.     call     parse
  58.     jmp    versok
  59. opfile:    lxi    d,fcb
  60.     call    bdos
  61.         inr     a       ;err 255 becomes zero
  62.         jnz     ready
  63. ;
  64. ;       cannot open file, so create it
  65.         mvi     c,makef
  66.         lxi     d,fcb
  67.         call    bdos
  68.         inr     a       ;err 255 becomes zero
  69.         jnz     ready
  70. ;
  71. ;       cannot create file, directory full
  72.         lxi     d,nospace
  73.         call    print
  74.         jmp     reboot  ;back to ccp
  75. ;
  76. ;***************************************************
  77. ;*                                                 *
  78. ;*  loop back to "ready" after each command        *
  79. ;*                                                 *
  80. ;***************************************************
  81. ;
  82. ready:
  83. ;       file is ready for processing
  84. ;
  85.         call    readcom ;read next command
  86.         shld    ranrec  ;store input record#
  87.         lxi     h,ranovf
  88.         mov     m,c     ;set ranrec high byte
  89.         cpi     'Q'     ;quit?
  90.         jnz     notq
  91. ;
  92. ;       quit processing, close file
  93.         mvi     c,closef
  94.         lxi     d,fcb
  95.         call    bdos
  96.         inr     a       ;err 255 becomes 0
  97.         jz      error   ;error message, retry
  98.         jmp     reboot  ;back to ccp
  99. ;
  100. ;***************************************************
  101. ;*                                                 *
  102. ;* end of quit command, process write              *
  103. ;*                                                 *
  104. ;***************************************************
  105. notq:
  106. ;       not the quit command, random write?
  107.         cpi     'W'
  108.         jnz     notw
  109. ;
  110. ;       this is a random write, fill buffer until cr
  111.         lxi     d,datmsg
  112.         call    print   ;data prompt
  113.         mvi     c,127   ;up to 127 characters
  114.         lxi     h,buff  ;destination
  115. rloop:  ;read next character to buff
  116.         push    b       ;save counter
  117.         push    h       ;next destination
  118.         call    getchr  ;character to a
  119.         pop     h       ;restore counter
  120.         pop     b       ;restore next to fill
  121.         cpi     cr      ;end of line?
  122.         jz      erloop
  123. ;       not end, store character
  124.         mov     m,a
  125.         inx     h       ;next to fill
  126.         dcr     c       ;counter goes down
  127.         jnz     rloop   ;end of buffer?
  128. erloop:
  129. ;       end of read loop, store 00
  130.         mvi     m,0
  131. ;
  132. ;       write the record to selected record number
  133.         mvi     c,writer
  134.         lxi     d,fcb
  135.         call    bdos
  136.         ora     a       ;error code zero?
  137.         jnz     error   ;message if not
  138.         jmp     ready   ;for another record
  139. ;
  140. ;
  141. ;********************************************************
  142. ;*                                                      *
  143. ;* end of write command, process write random zero fill *
  144. ;*                                                      *
  145. ;********************************************************
  146. notw:
  147. ;       not the quit command, random write zero fill?
  148.         cpi     'F'
  149.         jnz     notf
  150. ;
  151. ;       this is a random write, fill buffer until cr
  152.         lxi     d,datmsg
  153.         call    print   ;data prompt
  154.         mvi     c,127   ;up to 127 characters
  155.         lxi     h,buff  ;destination
  156. rloop1: ;read next character to buff
  157.         push    b       ;save counter
  158.         push    h       ;next destination
  159.         call    getchr  ;character to a
  160.         pop     h       ;restore counter
  161.         pop     b       ;restore next to fill
  162.         cpi     cr      ;end of line?
  163.         jz      erloop1
  164. ;       not end, store character
  165.         mov     m,a
  166.         inx     h       ;next to fill
  167.         dcr     c       ;counter goes down
  168.         jnz     rloop1  ;end of buffer?
  169. erloop1:
  170. ;       end of read loop, store 00
  171.         mvi     m,0
  172. ;
  173. ;       write the record to selected record number
  174.         mvi     c,wrtrzf
  175.         lxi     d,fcb
  176.         call    bdos
  177.         ora     a       ;error code zero?
  178.         jnz     error   ;message if not
  179.         jmp     ready   ;for another record
  180. ;
  181. ;***************************************************
  182. ;*                                                 *
  183. ;* end of write commands, process read             *
  184. ;*                                                 *
  185. ;***************************************************
  186. notf:
  187. ;       not a write command, read record?
  188.         cpi     'R'
  189.         jnz     error   ;skip if not
  190. ;
  191. ;       read random record
  192.         mvi     c,readr
  193.         lxi     d,fcb
  194.         call    bdos
  195.         ora     a       ;return code 00?
  196.         jnz     error
  197. ;
  198. ;       read was successful, write to console
  199.         call    crlf    ;new line
  200.         mvi     c,128   ;max 128 characters
  201.         lxi     h,buff  ;next to get
  202. wloop:
  203.         mov     a,m     ;next character
  204.         inx     h       ;next to get
  205.         ani     7fh     ;mask parity
  206.         jz      ready   ;for another command if 00
  207.         push    b       ;save counter
  208.         push    h       ;save next to get
  209.         cpi     ' '     ;graphic?
  210.         cnc     putchr  ;skip output if not
  211.         pop     h
  212.         pop     b
  213.         dcr     c       ;count=count-1
  214.         jnz     wloop
  215.         jmp     ready
  216. ;
  217. ;***************************************************
  218. ;*                                                 *
  219. ;* end of read command, all errors end-up here     *
  220. ;*                                                 *
  221. ;***************************************************
  222. ;
  223. error:
  224.         lxi     d,errmsg
  225.         call    print
  226.         jmp     ready
  227. ;
  228. ;***************************************************
  229. ;*                                                 *
  230. ;* utility subroutines for console i/o             *
  231. ;*                                                 *
  232. ;***************************************************
  233. getchr:
  234.         ;read next console character to a
  235.         mvi     c,coninp
  236.         call    bdos
  237.         ret
  238. ;
  239. putchr:
  240.         ;write character from a to console
  241.         mvi     c,conout
  242.         mov     e,a     ;character to send
  243.         call    bdos    ;send character
  244.         ret
  245. ;
  246. crlf:
  247.         ;send carriage return line feed
  248.         mvi     a,cr    ;carriage return
  249.         call    putchr
  250.         mvi     a,lf    ;line feed
  251.         call    putchr
  252.         ret
  253. ;
  254. parse:
  255.     ;read and parse filespec
  256.     lxi    d,conbuf
  257.     mvi    c,rstring
  258.     call     bdos
  259.     lxi    d,pfncb
  260.     mvi    c,parsef
  261.     call     bdos
  262.     ret
  263. ;
  264. print:
  265.         ;print the buffer addressed by de until $
  266.         push    d
  267.         call    crlf
  268.         pop     d       ;new line
  269.         mvi     c,pstring
  270.         call    bdos    ;print the string
  271.         ret
  272. ;
  273. readcom:
  274.         ;read the next command line to the conbuf
  275.         lxi     d,prompt
  276.         call    print   ;command?
  277.         mvi     c,rstring
  278.         lxi     d,conbuf
  279.         call    bdos    ;read command line
  280. ;       command line is present, scan it
  281.     mvi    c,0    ;start with 00
  282.         lxi     h,0     ;           0000
  283.         lxi     d,conlin;command line
  284. readc:  ldax    d       ;next command character
  285.         inx     d       ;to next command position
  286.         ora     a       ;cannot be end of command
  287.         rz
  288. ;       not zero, numeric?
  289.         sui     '0'
  290.         cpi     10      ;carry if numeric
  291.         jnc     endrd
  292. ;       add-in next digit
  293.     push     psw
  294.     mov    a,c    ;value = ahl
  295.     dad    h
  296.     adc    a    ;*2
  297.     push    a    ;save value * 2
  298.     push    h
  299.         dad     h       ;*4
  300.     adc    a
  301.     dad    h    ;*8
  302.     adc    a
  303.     pop    b    ;*2 + *8 = *10
  304.     dad    b
  305.     pop    b
  306.     adc    b
  307.     pop    b      ;+digit
  308.     mov    c,b
  309.     mvi    b,0
  310.     dad    b
  311.     aci    0
  312.     mov    c,a
  313.     jnc    readc
  314.         jmp     readcom
  315. endrd:
  316. ;       end of read, restore value in a
  317.         adi     '0'     ;command
  318.         cpi     'a'     ;translate case?
  319.         rc
  320. ;       lower case, mask lower case bits
  321.         ani     101$1111b
  322.         ret        ;return with value in chl
  323. ;
  324. ;***************************************************
  325. ;*                                                 *
  326. ;* string data area for console messages           *
  327. ;*                                                 *
  328. ;***************************************************
  329. badver:
  330.         db      'sorry, you need cp/m version 3$'
  331. nospace:
  332.         db      'no directory space$'
  333. datmsg:
  334.         db      'type data: $'
  335. errmsg:
  336.         db      'error, try again.$'
  337. prompt:
  338.         db      'next command? $'
  339. entmsg:
  340.     db    'enter filename: $' 
  341. ;
  342. ;***************************************************
  343. ;*                                                 *
  344. ;* fixed and variable data area                    *
  345. ;*                                                 *
  346. ;***************************************************
  347. conbuf: db      conlen  ;length of console buffer
  348. consiz: ds      1       ;resulting size after read
  349. conlin: ds      32      ;length 32 buffer
  350. conlen  equ     $-consiz
  351. ;
  352. pfncb:
  353.     dw    conlin
  354.     dw    fcb
  355. ;
  356.         ds      32      ;16 level stack
  357. stack:
  358.         end
  359.