;reverse.asm    copy file to reverse order

.model tiny
.386
.stack
.data

filnam  db      13 dup (0)              ;first file name
filnam1 db      13 dup (0)              ;second file name
crlf    db      13,10,36
handle  dw      ?                       ;file handle
handle1 dw      ?
filptr  dd      ?
buffer  db      1024 dup (?)
banner  db      'Reverse File, by Mike Carpenter, 1997',13,10,36
err0    db      'Source file not found.',13,10,36
err1    db      'Can not reverse to source file.',13,10,36
err2    db      'Usage:  reverse file1 file2',13,10
        db      '        file1 = source file to reverse',13,10
        db      '        file2 = destination file name',13,10,36
cterr   db      0


.code


main:
        mov     ax,@data                ;set default data segment
        mov     ds,ax

        push    ds
        pop     fs

        mov     dx,offset banner
        mov     ah,9
        int     21h

        call    ctail                   ;get command tail
        cmp     cterr,0
        je      m00a
        jmp     errx2

m00a:
        mov     ax,3D10h                ;open source file with read only
        mov     dx,offset filnam        ;  private to current process
        int     21h
        jnc     m00
        jmp     errx0
m00:
        mov     handle,ax

        mov     ax,4202h                ;set eof
        mov     bx,handle
        mov     cx,0
        mov     dx,0
        int     21h
        mov     word ptr filptr[0],ax   ;set file my file ptr
        mov     word ptr filptr[2],dx


        mov     ah,3Ch                  ;create destination file
        mov     cx,0
        mov     dx,offset filnam1
        int     21h
        jnc     m0
        jmp     errx1
m0:
        mov     handle1,ax

        mov     esi,filptr              ;load my file ptr

m1:
        dec     esi                     ;decrement it
        mov     filptr,esi

        mov     ax,4200h                ;decrement dos file ptr
        mov     bx,handle
        mov     dx,word ptr filptr[0]
        mov     cx,word ptr filptr[2]
        int     21h
                                        ;read a byte
        mov     ah,3Fh
        mov     bx,handle
        mov     cx,1
        mov     dx,offset buffer
        int     21h

        mov     ah,40h                  ;write a byte
        mov     bx,handle1
        mov     cx,1
        mov     dx,offset buffer
        int     21h

        cmp     filptr,0
        je      xit

        jmp     m1
xit:
        mov     ah,3Eh                  ;close source
        mov     bx,handle
        int     21h

        mov     ah,3Eh                  ;close destination
        mov     bx,handle1
        int     21h


xxit:
        mov     ax,4C00h                ;exit to dos
        int     21h

errx0:
        mov     dx,offset err0
        mov     ah,9
        int     21h
        jmp     xxit


errx1:
        mov     ah,3Eh                  ;close source
        mov     bx,handle
        int     21h

        mov     dx,offset err1
        mov     ah,9
        int     21h
        jmp     xxit
errx2:
        mov     dx,offset err2
        mov     ah,9
        int     21h
        jmp     xxit



ctail   proc    near                    ;get file name from command tail

        push    ds                      ;save seg registers
        push    es                      ;

        mov     di,offset filnam        ;destination offset is file name
        mov     ax,@data                ;segment is datarea
        mov     es,ax                   ;

        mov     si,82h                  ;source offset is command tail
        mov     ah,62h                  ;get PSP segment
        int     21h                     ;
        mov     ds,bx                   ;

        cld                             ;clear direction for byte copy
        mov     cx,12                   ;max 13 bytes
ctail1:
        lodsb                           ;copy file name til cr/
        cmp     al,13
        jne     ctail2
        jmp     ctailerr

ctail2:
        cmp     al,32                   ;
        je      ctailx                  ;
        stosb                           ;
        loop    ctail1                  ;
ctailx:
        mov     al,0                    ;terminate with 0
        stosb                           ;

        mov     di,offset filnam1       ;destination offset is file name

        mov     cx,12                   ;max 13 bytes
ctail1a:
        lodsb                           ;copy file name til cr/
        cmp     al,13                   ;
        je      ctailxa                 ;
        stosb                           ;
        loop    ctail1a                 ;
ctailxa:
        mov     al,0                    ;terminate with 0
        stosb                           ;

        pop     es
        pop     ds
        ret
ctailerr:
        mov     cterr,1
        ret
ctail   endp



        end main

