home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / lambda / soundpot / f / freebase.lbr / RANDOM.AZM / RANDOM.ASM
Encoding:
Assembly Source File  |  1993-10-26  |  10.9 KB  |  369 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,32
  131.       ;fill with dots
  132.  
  133.          inx    h
  134.         dcr     c
  135.  
  136.         jnz     erloop
  137.  
  138.         mvi     m,0      ;end of record
  139.  
  140.  
  141. ;
  142. ;       write the record to selected record number
  143.         mvi     c,writer
  144.         lxi     d,fcb
  145.         call    bdos
  146.         ora     a       ;error code zero?
  147.         jnz     error   ;message if not
  148.         jmp     ready   ;for another record
  149. ;
  150. ;
  151. ;********************************************************
  152. ;*                                                      *
  153. ;* end of write command, process write random zero fill *
  154. ;*                                                      *
  155. ;********************************************************
  156. notw:
  157. ;       not the quit command, random write zero fill?
  158.         cpi     'F'
  159.         jnz     notf
  160. ;
  161. ;       this is a random write, fill buffer until cr
  162.         lxi     d,datmsg
  163.         call    print   ;data prompt
  164.         mvi     c,127   ;up to 127 characters
  165.         lxi     h,buff  ;destination
  166. rloop1: ;read next character to buff
  167.         push    b       ;save counter
  168.         push    h       ;next destination
  169.         call    getchr  ;character to a
  170.         pop     h       ;restore counter
  171.         pop     b       ;restore next to fill
  172.         cpi     cr      ;end of line?
  173.         jz      erloop1
  174. ;       not end, store character
  175.         mov     m,a
  176.         inx     h       ;next to fill
  177.         dcr     c       ;counter goes down
  178.         jnz     rloop1  ;end of buffer?
  179. erloop1:
  180. ;       end of read loop, store 00
  181.         mvi     m,0
  182. ;
  183. ;       write the record to selected record number
  184.         mvi     c,wrtrzf
  185.         lxi     d,fcb
  186.         call    bdos
  187.         ora     a       ;error code zero?
  188.         jnz     error   ;message if not
  189.         jmp     ready   ;for another record
  190. ;
  191. ;***************************************************
  192. ;*                                                 *
  193. ;* end of write commands, process read             *
  194. ;*                                                 *
  195. ;***************************************************
  196. notf:
  197. ;       not a write command, read record?
  198.         cpi     'R'
  199.         jnz     error   ;skip if not
  200. ;
  201. ;       read random record
  202.         mvi     c,readr
  203.         lxi     d,fcb
  204.         call    bdos
  205.         ora     a       ;return code 00?
  206.         jnz     error
  207. ;
  208. ;       read was successful, write to console
  209.         call    crlf    ;new line
  210.         mvi     c,128   ;max 128 characters
  211.         lxi     h,buff  ;next to get
  212. wloop:
  213.         mov     a,m     ;next character
  214.         inx     h       ;next to get
  215.         ani     7fh     ;mask parity
  216.         jz      ready   ;for another command if 00
  217.         push    b       ;save counter
  218.         push    h       ;save next to get
  219.         cpi     ' '     ;graphic?
  220.         cnc     putchr  ;skip output if not
  221.         pop     h
  222.         pop     b
  223.         dcr     c       ;count=count-1
  224.         jnz     wloop
  225.         jmp     ready
  226. ;
  227. ;***************************************************
  228. ;*                                                 *
  229. ;* end of read command, all errors end-up here     *
  230. ;*                                                 *
  231. ;***************************************************
  232. ;
  233. error:
  234.         lxi     d,errmsg
  235.         call    print
  236.         jmp     ready
  237. ;
  238. ;***************************************************
  239. ;*                                                 *
  240. ;* utility subroutines for console i/o             *
  241. ;*                                                 *
  242. ;***************************************************
  243. getchr:
  244.         ;read next console character to a
  245.         mvi     c,coninp
  246.         call    bdos
  247.         ret
  248. ;
  249. putchr:
  250.         ;write character from a to console
  251.         mvi     c,conout
  252.         mov     e,a     ;character to send
  253.         call    bdos    ;send character
  254.         ret
  255. ;
  256. crlf:
  257.         ;send carriage return line feed
  258.         mvi     a,cr    ;carriage return
  259.         call    putchr
  260.         mvi     a,lf    ;line feed
  261.         call    putchr
  262.         ret
  263. ;
  264. parse:
  265.     ;read and parse filespec
  266.     lxi    d,conbuf
  267.     mvi    c,rstring
  268.     call     bdos
  269.     lxi    d,pfncb
  270.     mvi    c,parsef
  271.     call     bdos
  272.     ret
  273. ;
  274. print:
  275.         ;print the buffer addressed by de until $
  276.         push    d
  277.         call    crlf
  278.         pop     d       ;new line
  279.         mvi     c,pstring
  280.         call    bdos    ;print the string
  281.         ret
  282. ;
  283. readcom:
  284.         ;read the next command line to the conbuf
  285.         lxi     d,prompt
  286.         call    print   ;command?
  287.         mvi     c,rstring
  288.         lxi     d,conbuf
  289.         call    bdos    ;read command line
  290. ;       command line is present, scan it
  291.     mvi    c,0    ;start with 00
  292.         lxi     h,0     ;           0000
  293.         lxi     d,conlin;command line
  294. readc:  ldax    d       ;next command character
  295.         inx     d       ;to next command position
  296.         ora     a       ;cannot be end of command
  297.         rz
  298. ;       not zero, numeric?
  299.         sui     '0'
  300.         cpi     10      ;carry if numeric
  301.         jnc     endrd
  302. ;       add-in next digit
  303.     push     psw
  304.     mov    a,c    ;value = ahl
  305.     dad    h
  306.     adc    a    ;*2
  307.     push    a    ;save value * 2
  308.     push    h
  309.         dad     h       ;*4
  310.     adc    a
  311.     dad    h    ;*8
  312.     adc    a
  313.     pop    b    ;*2 + *8 = *10
  314.     dad    b
  315.     pop    b
  316.     adc    b
  317.     pop    b      ;+digit
  318.     mov    c,b
  319.     mvi    b,0
  320.     dad    b
  321.     aci    0
  322.     mov    c,a
  323.     jnc    readc
  324.         jmp     readcom
  325. endrd:
  326. ;       end of read, restore value in a
  327.         adi     '0'     ;command
  328.         cpi     'a'     ;translate case?
  329.         rc
  330. ;       lower case, mask lower case bits
  331.         ani     101$1111b
  332.         ret        ;return with value in chl
  333. ;
  334. ;***************************************************
  335. ;*                                                 *
  336. ;* string data area for console messages           *
  337. ;*                                                 *
  338. ;***************************************************
  339. badver:
  340.         db      'sorry, you need cp/m version 3$'
  341. nospace:
  342.         db      'no directory space$'
  343. datmsg:
  344.         db      'type data: $'
  345. errmsg:
  346.         db      'error, try again.$'
  347. prompt:
  348.         db      'next command? $'
  349. entmsg:
  350.     db    'enter filename: $' 
  351. ;
  352. ;***************************************************
  353. ;*                                                 *
  354. ;* fixed and variable data area                    *
  355. ;*                                                 *
  356. ;***************************************************
  357. conbuf: db      conlen  ;length of console buffer
  358. consiz: ds      1       ;resulting size after read
  359. conlin: ds      32      ;length 32 buffer
  360. conlen  equ     $-consiz
  361. ;
  362. pfncb:
  363.     dw    conlin
  364.     dw    fcb
  365. ;
  366.         ds      32      ;16 level stack
  367. stack:
  368.         end
  369.