home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / asm / wasm / file1.inc < prev    next >
Text File  |  1988-03-04  |  11KB  |  388 lines

  1. ;=============================================================================
  2. ; File Routines
  3. ;
  4. ; These are routines open, close, read, and write to files.  All reading and
  5. ; writing is sequential and buffered.  Information for each open file is
  6. ; contained in a special record which consists of 12 consecutive bytes.  A
  7. ; record must be declared in the calling program for each open file.  The
  8. ; information within a record is assigned and maintained by the file routines.
  9. ;
  10. ; The file record format is:
  11. ;
  12. ;   dw      ?       ;status                     offset 0
  13. ;   dw      ?       ;buffer size                offset 2
  14. ;   dw      ?       ;buffer data offset         offset 4
  15. ;   dw      ?       ;buffer segment             offset 6
  16. ;   dw      ?       ;file handle                offset 8
  17. ;   dw      ?       ;bytes in buffer            offset 10
  18. ;
  19. ; All all registers are preserved except those used to return parameters. All
  20. ; parameters are passed through registers.  It is assumed that DS = ES = CS. 
  21.  
  22. ;================================================
  23. ; Allocate a read/write buffer.
  24. ;
  25. ; In: BX= file record offset; [BX+2]= buffer
  26. ; size.
  27. ;
  28. ; Out: CY= set if error; AX= error number.
  29.  
  30. File_Alloc PROC NEAR
  31.         push    cx
  32.         push    dx
  33.         push    bx
  34.         mov     bx, [bx+2]      ;size in bytes
  35.         mov     cl, 4           ;paragraph shift
  36.         shr     bx, cl          ;get size in paragraphs
  37.         inc     bx              ;round up
  38.         mov     dx, bx
  39.         shl     dx, cl          ;shift back to byte form
  40.         mov     ah, 48h         ;function
  41.         int     21h             ;execute
  42.         pop     bx
  43.         jc      filall1
  44.         mov     [bx+2], dx      ;save size
  45.         mov     WORD [bx+4], 0  ;clear data pointer
  46.         mov     [bx+6], ax      ;save segment
  47.         mov     WORD [bx+10], 0 ;clear bytes in buffer
  48. filall1 pop     dx
  49.         pop     cx
  50.         ret
  51.         ENDP                    ;File_Alloc
  52.  
  53. ;================================================
  54. ; Deallocate a read/write buffer.
  55. ;
  56. ; In: BX= file record offset.
  57.  
  58. File_Free PROC  NEAR
  59.         push    ax
  60.         push    es
  61.         mov     es, [bx+6]      ;segment
  62.         mov     ah, 49h         ;function
  63.         int     21h             ;execute
  64.         pop     es
  65.         pop     ax
  66.         ret
  67.         ENDP                    ;File_Free
  68.  
  69. ;================================================
  70. ; Create or truncate a file. File is opened for
  71. ; writing.
  72. ;
  73. ; In: BX= file record offset; CX= buffer size;
  74. ; DX= offset of filespec.
  75. ;
  76. ; Out: CY= set if error; AX= returns error code.
  77.  
  78. File_Create PROC NEAR
  79.         mov     WORD [bx], 1    ;set status
  80.         mov     [bx+2], cx      ;save size
  81.         call    File_Alloc      ;allocate buffer
  82.         jc      filcre1         ;jump if error
  83.         mov     ah, 3ch         ;function
  84.         sub     cx, cx          ;file attribute
  85.         int     21h             ;execute
  86.         mov     [bx+8], ax      ;save handle
  87.         jnc     filcre1
  88.         call    File_Free       ;deallocate buffer
  89.         stc
  90. filcre1 ret
  91.         ENDP                    ;File_Create
  92.  
  93. ;================================================
  94. ; Open a file for reading.
  95. ;
  96. ; In: BX= file record offset; CX= buffer size;
  97. ; DX= offset of filespec.
  98. ;
  99. ; Out: CY= set if error; AX= returns error code.
  100.  
  101. File_OpenR PROC NEAR
  102.         mov     WORD [bx+0], 0  ;set status
  103.         mov     [bx+2], cx      ;save size
  104.         call    File_Alloc      ;allocate buffer
  105.         jc      filopn1         ;jump if error
  106.         mov     ax, 3d00h       ;function and access
  107.         int     21h             ;execute
  108.         mov     [bx+8], ax      ;save handle
  109.         jnc     filopn1
  110.         call    File_Free       ;deallocate
  111.         stc
  112. filopn1 ret
  113.         ENDP                    ;File_OpenR
  114.  
  115. ;================================================
  116. ; Open a file for writing.
  117. ;
  118. ; In: BX= file record offset; CX= buffer size;
  119. ; DX= offset of filespec.
  120. ;
  121. ; Out: CY= set if error; AX= returns error code.
  122.  
  123. File_OpenW PROC NEAR
  124.         mov     WORD [bx], 1    ;set status
  125.         mov     [bx+2], cx      ;save size
  126.         call    File_Alloc      ;allocate buffer
  127.         jc      filopw1         ;jump if error
  128.         mov     ax, 3d01h       ;function and access
  129.         int     21h             ;execute
  130.         mov     [bx+8], ax      ;save handle
  131.         jnc     filopw1
  132.         call    File_Free       ;deallocate
  133.         stc
  134. filopw1 ret
  135.         ENDP                    ;File_OpenW
  136.  
  137. ;================================================
  138. ; Fill a read buffer. 
  139. ;
  140. ; In: BX= file record offset.
  141. ;
  142. ; Out: CY= set if error; AX= error code (AX= 0 if
  143. ; EOF and 0 bytes are read).
  144.  
  145.  
  146. File_Fill PROC  NEAR
  147.         push    cx
  148.         push    dx
  149.         push    bx
  150.         push    ds
  151.         mov     ax, [bx+8]      ;file handle
  152.         mov     cx, [bx+2]      ;buffer size
  153.         sub     dx, dx          ;start of segment
  154.         mov     [bx+4], dx      ;set data offset
  155.         mov     ds, [bx+6]      ;buffer segment
  156.         mov     bx, ax
  157.         mov     ah, 3fh         ;function
  158.         int     21h             ;execute
  159.         pop     ds
  160.         pop     bx
  161.         mov     [bx+10], ax     ;save bytes read
  162.         jc      filfil1
  163.         or      ax, ax          ;check if no bytes
  164.         jnz     filfil1
  165.         stc
  166. filfil1 pop     dx
  167.         pop     cx
  168.         ret
  169.         ENDP                    ;File_Fill
  170.  
  171. ;================================================
  172. ; Read from a file.
  173. ;
  174. ; In: BX= file record offset; CX= number of
  175. ; bytes; DI= location of bytes.
  176. ;
  177. ; Out: CY= set if error; AX= error code (CY= set
  178. ; and AX= 0 if EOF); CX= bytes read.
  179.  
  180. File_Read PROC  NEAR
  181.         pushf
  182.         push    cx
  183.         push    dx
  184.         push    di
  185.         push    si
  186.         cld
  187.         mov     dx, [bx+6]      ;buffer segment
  188.  
  189. ;--- read bytes from buffer
  190.  
  191. filred1 mov     ax, [bx+10]     ;bytes in buffer
  192.         mov     si, [bx+4]      ;data pointer
  193.         sub     ax, si          ;bytes available
  194.         jz      filred3         ;jump if none
  195.  
  196.         push    ds
  197.         push    cx
  198.         cmp     ax, cx          ;check if enough
  199.         jae     filred2
  200.         mov     cx, ax          ;read what's available
  201.  
  202. filred2 push    cx
  203.         mov     ds, dx
  204.         rep
  205.         movsb                   ;copy bytes
  206.         pop     ax
  207.         pop     cx
  208.         pop     ds
  209.         mov     [bx+4], si      ;save data pointer
  210.         sub     cx, ax          ;get bytes not yet read
  211.         jz      filred4         ;jump if finished
  212.  
  213. ;--- read into buffer
  214.  
  215. filred3 call    File_Fill       ;read a buffer full
  216.         jnc     filred1
  217.  
  218. ;--- finished
  219.  
  220. filred4 pop     si
  221.         pop     di
  222.         pop     dx
  223.         pop     cx
  224.         jc      filred5
  225.         popf
  226.         clc
  227.         ret
  228.  
  229. filred5 popf
  230.         stc
  231.         ret
  232.         ENDP                    ;File_Read
  233.  
  234. ;================================================
  235. ; Flush the a write buffer.
  236. ;
  237. ; In: BX= file record offset; CX= file record
  238. ; offset.
  239. ;
  240. ; Out: CY= set if error; AX= error code (0 if
  241. ; disk full).
  242.  
  243. File_Flush PROC NEAR
  244.         push    cx
  245.         push    dx
  246.         push    ds
  247.  
  248. ;--- check if buffer needs flushing
  249.  
  250.         mov     ax, [bx+8]      ;handle
  251.         lds     cx, [bx+4]      ;data pointer
  252.         or      cx, cx          ;check if any data
  253.         jz      filflu3
  254.  
  255. ;--- write buffer
  256.  
  257.         es:
  258.         mov     WORD [bx+4], 0  ;clear data pointer
  259.         push    bx
  260.         mov     bx, ax
  261.         sub     dx, dx          ;start of segment
  262.         mov     ah, 40h         ;function
  263.         int     21h             ;execute
  264.         pop     bx
  265.         jc      filflu2         ;jump if error
  266.         cmp     ax, cx          ;check if all bytes written
  267.         je      filflu3
  268.  
  269. ;--- finished, error writing buffer
  270.  
  271. filflu1 sub     ax, ax          ;code 0 if bytes not all written
  272. filflu2 stc
  273.         pop     ds
  274.         pop     dx
  275.         pop     cx
  276.         ret
  277.  
  278. ;--- finished, no error
  279.  
  280. filflu3 clc
  281.         pop     ds
  282.         pop     dx
  283.         pop     cx
  284.         ret
  285.         ret
  286.         ENDP                    ;File_Flush
  287.  
  288. ;================================================
  289. ; Write to a file.
  290. ;
  291. ; In: BX= file record offset; CX= number of
  292. ; bytes; SI= location of bytes.
  293. ;
  294. ; Out: CY= set if error; AX= error code (CY= set
  295. ; and AX= 0 is disk full).
  296.  
  297. File_Write PROC NEAR
  298.         pushf
  299.         push    cx
  300.         push    dx
  301.         push    di
  302.         push    si
  303.         cld
  304.         mov     dx, [bx+6]      ;buffer segment
  305.  
  306. ;--- copy bytes to buffer
  307.  
  308. Filwrt1 mov     ax, [bx+2]      ;buffer size
  309.         mov     di, [bx+4]      ;data pointer
  310.         sub     ax, di          ;buffer space available
  311.         jz      filwrt3         ;jump if none, must flush first
  312.  
  313.         push    es
  314.         push    cx
  315.         cmp     ax, cx          ;check if all bytes fit
  316.         jae     filwrt2
  317.         mov     cx, ax          ;set to maximum
  318.  
  319. filwrt2 push    cx
  320.         mov     es, dx
  321.         rep
  322.         movsb
  323.         pop     ax
  324.         pop     cx
  325.         pop     es
  326.         mov     [bx+4], di      ;save data pointer
  327.         sub     cx, ax          ;get bytes not yet written
  328.         jz      filwrt4         ;jump if finished
  329.  
  330. ;--- write buffer
  331.  
  332. filwrt3 call    File_Flush      ;flush buffer first
  333.         jnc     filwrt1         ;loop back if no error
  334.  
  335. ;--- finished
  336.  
  337. filwrt4 pop     si
  338.         pop     di
  339.         pop     dx
  340.         pop     cx
  341.         jc      filwrt5
  342.         popf
  343.         clc
  344.         ret
  345.  
  346. filwrt5 popf
  347.         stc
  348.         ret
  349.         ENDP                    ;File_Write
  350.  
  351. ;================================================
  352. ; Close a file.
  353. ;
  354. ; In: BX= file record offset.
  355. ;
  356. ; Out: CY= set if error; AX= error code (0 if
  357. ; disk full).
  358.  
  359. File_Close PROC NEAR
  360.         push    bx
  361.  
  362. ;--- flush file buffer
  363.  
  364.         test    WORD [bx], 1    ;test if open for writing
  365.         jz      filclo1
  366.         call    File_Flush      ;flush buffer
  367.         jc      filclo2
  368.  
  369. ;--- close file
  370.  
  371. filclo1 mov     ah, 3eh         ;function
  372.         mov     bx, [bx+8]      ;handle
  373.         int     21h             ;execute
  374.         pop     bx
  375.         ret
  376.  
  377. ;--- close file, error on flush
  378.  
  379. filclo2 push    ax
  380.         mov     ah, 3eh         ;function
  381.         mov     bx, [bx+8]      ;handle
  382.         int     21h             ;execute
  383.         pop     ax
  384.         pop     bx
  385.         stc
  386.         ret
  387.         ENDP                    ;File_Close
  388.