home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / CBUFF.AZM / CBUFF.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  3.8 KB  |  144 lines

  1. ;----------------------------------------------------------------
  2. ;        This is a module in the ASMLIB library.
  3. ;
  4. ; This module reads a line of input from the console and puts it into
  5. ; a standard CP/M console buffer pointed to by DE on entry. This is
  6. ; a little nicer that CP/M as it allows buffers to be pre-initialized 
  7. ; so that it is printed when the buffer is input so that defaults can
  8. ; be loaded before entry of data.
  9. ;
  10. ;            Written        R.C.H.          22/10/83
  11. ;            Last Update    R.C.H.          22/10/83
  12. ;----------------------------------------------------------------
  13. ;
  14.     name    'cbuff'
  15.     public    cbuff
  16.     extrn    bell,cie,coe            ; get and put a byte to screen
  17. ;
  18.     maclib    z80
  19. ;
  20. cbuff:
  21.     push    psw
  22.     ldax    d            ; get buffer size in bytes
  23.     ora    a
  24.     jrz    cbuff$end
  25.     push    h
  26.     push    b
  27.     push    d
  28.     xchg                ; put string address into HL
  29.     mov    c,a            ; Now C = buffer maximum size
  30. init:
  31.     mvi    b,00            ; character read = 0
  32.     inx    h            ; hl -> size of character read now
  33. ; Here we detect if there is some data in the buffer to be pre printed
  34. ; and if there is the we print it.
  35.     mov    a,m            ; get number of chars. in the buffer
  36.     inx    h            ; point to string space now.
  37.     ora    a
  38.     jrz    rdloop
  39. ; Print the initialized character string, save the size for later
  40.     mov    b,a
  41.     push    b            ; save
  42. init2:
  43.     mov    a,m            ; get the character
  44.     inx    h            ; point to next string space byte
  45.     call    dspchr            ; print it, maybe control character
  46.     djnz    init2            ; print all characters
  47.     pop    b            ; restore # of characters
  48. ;
  49. ; On entry here HL-> string space, next free byte, B = number of characters
  50. ; in the string. C = number of bytes in the buffer.
  51.  
  52.  
  53. rdloop:
  54.     call    cie            ; get a character
  55.     cpi    0dh            ; end if carriage return
  56.     jrz    exitrd        ; exit
  57.     cpi    0ah
  58.     jrz    exitrd
  59.     cpi    08            ; backspace ??
  60.     jrnz    rdlp1            ; if not then continue
  61.     call    backsp            ; else backspace
  62.     jr    rdloop            ; keep on backspacing
  63. rdlp1:
  64.     cpi    018h            ; delete line ?
  65.     jrnz    rdlp2
  66. del1:
  67.     call    backsp            ; delete a character
  68.     jrnz    del1            ; keep on till all character deaded
  69.     jr    rdloop            ; start again ebonettes
  70. ;
  71. ; If here we check if the buffer is full. If so we ring the bell
  72. rdlp2:
  73.     mov    e,a            ; save the character
  74.     mov    a,b            ; load byte count
  75.     cmp    c            ; is it equal to the maximum ?
  76.     jrc    strch            ; store the character if not full
  77.     call    bell            ; else ring the bell
  78.     jr    rdloop            ; get more characters
  79. ;
  80. ; Buffer not full so save the character
  81. strch:
  82.     mov    a,e            ; get character
  83.     mov    m,a            ; save it
  84.     inx    h            ; point to next buffer byte
  85.     inr    b            ; increment byte count
  86.     call    dspchr        ; display the (maybe control) character
  87.     jr    rdloop            ; do again, more characters
  88. ;
  89. ; Display a control character by preceeding it with a  '^'
  90. ;
  91. dspchr:
  92.     cpi    020h            ; was it a space ?
  93.     jnc     coe             ; if not then print & return
  94.     mov    e,a            ; else save character
  95.     mvi    a,'^'            ; indicate a control character
  96.     call    coe
  97.     mov    a,e            ; restore character
  98.     adi    040h            ; make printable
  99.     jmp     coe
  100. ;
  101. ; Send a backspace and detect if at the start of the line.
  102. ;
  103. backsp:
  104.     mov    a,b            ; get character count
  105.     ora    a
  106.     rz                ; return if line empty
  107.     dcx    h            ; decrement byte pointer
  108.     mov    a,m            ; get the character
  109.     cpi    020h            ; is it a control character ?
  110.     jrnc    bsp1            ; if not then delete 1 char only
  111.     call    bsp            ; send a backspace
  112. bsp1:
  113.     call    bsp            ; backspace 1
  114.     dcr    b            ; one less string byte
  115.     ret
  116. ;
  117. ; Send the backspace
  118. bsp:
  119.     mvi    a,08
  120.     call    coe
  121.     mvi    a,' '            ; erase the character
  122.     call    coe
  123.     mvi    a,08
  124.     jmp    coe            ; send and return
  125. exitrd:
  126. ; Set the number of bytes read into the buffer byte at DE + 1.
  127. ;
  128.     pop    d            ; restore all registers (buffer addr)
  129.     mov    a,b            ; get # of characters
  130.     inx    d
  131.     stax    d            ; save in characters read byte
  132.     dcx    d            ; restore de
  133. ;
  134.     pop    b
  135.     pop    h
  136. cbuff$end:
  137.     pop    psw
  138.     ret
  139.  
  140.     end
  141.  
  142.  
  143.  
  144.