home *** CD-ROM | disk | FTP | other *** search
/ ftp.shrubbery.net / 2015-02-07.ftp.shrubbery.net.tar / ftp.shrubbery.net / pub / pc / unix / unx.arc / FIO.ASM < prev    next >
Assembly Source File  |  1985-06-10  |  3KB  |  159 lines

  1. page 66, 132
  2. ;   Narrow on printer
  3. page
  4.  
  5. ;---- fio.asm -------------------------------------------------------------
  6. ; Buffered I/O for lazy programmers.
  7. ; Expects user to set these provided variables:
  8. ;    f_ihand, f_ohand: handles of input & output files, respectively
  9. ;       f_ibuf, f_obuf  : word pointers to buffer areas;
  10. ;    f_ilen, f_olen  : word lengths of buffer areas;
  11. ; Provides:
  12. ;       f_ibufend, f_obufend: word ptrs to current ends of buffer areas;
  13. ;       f_imore, f_omore    : routines to flush input & output buffers;
  14. ;       f_getc, f_putc      : Get/put al; flags same as imore & omore.
  15. ;    f_io_init           : Given f_ilen & f_olen, calls new & sets up.
  16. ;
  17. ; All these routines reserve SI and DI for their io pointers.
  18. ; Getc and imore detect physical EOF; you'll have to compare with
  19. ; ^Z yourself if you need logical EOF.
  20. ;
  21. ; Version of 23 Aug 1984; DRK.
  22.  
  23.  
  24.     extrn    new: near;
  25.  
  26.     public    f_ihand, f_ohand, f_ibuf, f_obuf, f_ilen, f_olen;
  27.     public    f_ibufend, f_obufend, f_imore, f_omore;
  28.     public    f_getc, f_putc;
  29.     public    f_io_init
  30.  
  31.  
  32. dos    macro    fn
  33.     mov    ah, fn
  34.     int    21h
  35.     endm
  36.  
  37. code    segment para public 'CODE'
  38. assume cs:code,ds:code
  39.  
  40. f_ihand        dw    ?
  41. f_ohand        dw    ?
  42.  
  43. f_ibuf        dw    ?    ; where input buffer starts
  44. f_ibufend    dw    ?    ; points to just past last byte
  45. f_ilen        dw    ?
  46. f_obuf        dw    ?    ; where output buffer starts
  47. f_obufend    dw    ?
  48. f_olen        dw    ?
  49.  
  50. ;-- f_io_init -----------
  51. f_io_init    proc    near
  52.     mov    cx, f_ilen
  53.     call    new
  54.     mov    f_ibuf, bx
  55.     mov    f_ibufend, bx
  56.     mov    si, bx
  57.  
  58.     mov    cx, f_olen
  59.     call    new
  60.     mov    f_obuf, bx
  61.     mov    di, bx
  62.     add    bx, cx
  63.     mov    f_obufend, bx
  64.  
  65.     ret
  66. f_io_init    endp
  67.  
  68.  
  69. ;-- f_imore -----------------------------------------------
  70. ; Reads from file into f_ibuf.
  71. ; Resets f_ibufend.
  72. ; Loads SI with new f_ibufptr.
  73. ; Returns Z=true if at EOF, CY=true if error reading file.
  74.  
  75. f_imore    proc    near
  76.  
  77.     mov    bx, f_ihand
  78.     mov    cx, f_ilen
  79.     mov    dx, f_ibuf
  80.     DOS    3fh        ; read from file
  81.     jc    rerr
  82.  
  83.     mov    si, dx
  84.     add    dx, ax
  85.     mov    f_ibufend, dx
  86.  
  87.     or    ax, ax        ; check physical EOF
  88.     clc
  89.  
  90. rerr:    ret
  91.  
  92. f_imore    endp
  93.  
  94. ;-----------------------------------
  95.  
  96. ;-- f_omore ---------------------------------------
  97. ; Expects DI to have f_obufptr.
  98. ; Writes f_obuffer to output file.
  99. ; Resets f_obufend
  100. ; Loads DI with new f_obufptr.
  101.  
  102. f_omore    proc    near
  103.  
  104.     mov    dx, f_obuf
  105.     mov    bx, f_ohand
  106.     mov    cx, di
  107.     sub    cx, dx
  108.     
  109.     DOS    40H        ; write to file
  110.     jc    werr
  111.     sub    cx, ax
  112.     jz    afinished
  113.     dec    cx
  114.     mov    ax, -1
  115.     jnz    werr        ; account for WIERDNESS in DOS:
  116.                 ; when writing to device, the ^Z is not
  117.                 ; transferred.  Ignore error if all but one
  118.                 ; char was written.
  119. afinished:
  120.     mov    di, dx
  121.     clc
  122.  
  123. wdone:    ret
  124.  
  125. werr:    stc
  126.     jmp    wdone
  127.  
  128. f_omore    endp
  129.  
  130.  
  131. ;-----------------------------------
  132. f_getc    proc    near
  133.     cmp    si, f_ibufend
  134.     jnz    f_gcok
  135.         call    f_imore
  136. f_gcok:    lodsb
  137.     ret
  138. f_getc    endp
  139.  
  140.  
  141. f_putc    proc    near
  142.     cmp    di, f_obufend
  143.     jnz    pok
  144.         push    ax
  145.         call    f_omore
  146.         pop    ax
  147. pok:    stosb
  148.     ret
  149. f_putc    endp
  150.  
  151.  
  152.  
  153. code    ends
  154.  
  155.     end
  156.  
  157.  
  158.  
  159.