home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / dos_ency / 7 / myfile.asm < prev    next >
Encoding:
Assembly Source File  |  1988-08-11  |  2.6 KB  |  63 lines

  1. file1   db      'MYFILE.DAT',0
  2. file2   db      'MYFILE.BAK',0
  3.  
  4. handle1 dw      ?               ; handle for MYFILE.DAT
  5. handle2 dw      ?               ; handle for MYFILE.BAK
  6.  
  7. buff    db      512 dup (?)     ; buffer for file I/O
  8.         .
  9.         .
  10.         .
  11.                                 ; open MYFILE.DAT...
  12.         mov     dx,seg file1    ; DS:DX = address of filename
  13.         mov     ds,dx
  14.         mov     dx,offset file1
  15.         mov     ax,3d00h        ; Function 3DH = open (read-only)
  16.         int     21h             ; transfer to MS-DOS
  17.         jc      error           ; jump if open failed
  18.         mov     handle1,ax      ; save handle for file
  19.  
  20.                                 ; create MYFILE.BAK...
  21.         mov     dx,offset file2 ; DS:DX = address of filename
  22.         mov     cx,0            ; CX = normal attribute 
  23.         mov     ah,3ch          ; Function 3CH = create
  24.         int     21h             ; transfer to MS-DOS
  25.         jc      error           ; jump if create failed
  26.         mov     handle2,ax      ; save handle for file
  27.  
  28. loop:                           ; read MYFILE.DAT
  29.         mov     dx,offset buff  ; DS:DX = buffer address
  30.         mov     cx,512          ; CX = length to read
  31.         mov     bx,handle1      ; BX = handle for MYFILE.DAT
  32.         mov     ah,3fh          ; Function 3FH = read
  33.         int     21h             ; transfer to MS-DOS
  34.         jc      error           ; jump if read failed
  35.         or      ax,ax           ; were any bytes read?
  36.         jz      done            ; no, end of file reached
  37.  
  38.                                 ; write MYFILE.BAK
  39.         mov     dx,offset buff  ; DS:DX = buffer address
  40.         mov     cx,ax           ; CX = length to write
  41.         mov     bx,handle2      ; BX = handle for MYFILE.BAK
  42.         mov     ah,40h          ; Function 40H = write
  43.         int     21h             ; transfer to MS-DOS
  44.         jc      error           ; jump if write failed
  45.         cmp     ax,cx           ; was write complete?
  46.         jne     error           ; jump if disk full
  47.         jmp     loop            ; continue to end of file
  48.  
  49. done:                           ; now close files...
  50.         mov     bx,handle1      ; handle for MYFILE.DAT
  51.         mov     ah,3eh          ; Function 3EH = close file
  52.         int     21h             ; transfer to MS-DOS
  53.         jc      error           ; jump if close failed
  54.  
  55.         mov     bx,handle2      ; handle for MYFILE.BAK
  56.         mov     ah,3eh          ; Function 3EH = close file
  57.         int     21h             ; transfer to MS-DOS
  58.         jc      error           ; jump if close failed
  59.  
  60.         .
  61.         .
  62.         .
  63.