home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / GRDBDL17.ZIP / FILEIO.ASM < prev    next >
Encoding:
Assembly Source File  |  1998-10-26  |  4.2 KB  |  232 lines

  1. ;
  2. ; GRDP
  3. ;
  4. ; Copyright(c) LADsoft
  5. ;
  6. ; David Lindauer, camille@bluegrass.net
  7. ;
  8. ;
  9. ; FILEIO.ASM
  10. ;
  11. ; Function: Handle File read/wrote commands
  12. ;
  13.     ;MASM MODE
  14.     .MODEL SMALL
  15.     .386
  16.  
  17.  
  18. include  eprints.inc 
  19. include  einput.inc 
  20. include  emtrap.inc 
  21. include  ebreaks.inc 
  22. include  eloader.inc
  23. include  edos.inc
  24. include eexec.inc
  25.  
  26.     PUBLIC dofileread, dofilewrite, FileLoadErr
  27.  
  28.     .data
  29. EXEfileFlag    db    0        ;if EXE file, can't write it
  30. writelen    dd    0        ;length to write
  31. writeseg    dw    0        ;segment and
  32. writeofs    dw    0        ; offset to write
  33. newaddr        db    0        ;if @addr used
  34. writeFileName    db    256 DUP (0)    ;name of file to write
  35.  
  36.     .CODE
  37. ;
  38. ; read from file
  39. ;
  40. doFileRead PROC
  41.     movzx    ax,[lastexe]    ; use state of last load if no params
  42.     xor    al,1
  43.     push    ax
  44.     call    WadeSpace
  45.     jz    PureReadFile    ; branch if reloading same program
  46.     pop    ax        ; else assume anything goes
  47.     push    0
  48.     cmp    byte ptr [si],'@' ; else see if the load-exe-as-com flag set
  49.     jnz    comonly        ; branch if so
  50.     inc    si        ; else anything goes
  51.     pop    ax        ; com only!!!
  52.     push    1
  53. comonly:
  54.     cmp    byte ptr [si],'-' ; see if they want to unload
  55.     jnz    newfile
  56.     pop    ax        ; clear stack
  57.     call    UnLoadProgram    ; yes do it
  58.     clc
  59.     ret
  60. newfile:
  61.     call    WadeSpace    ; if no prog, go read the old one again
  62.     jz    PureReadFile
  63.     call    ParseProgName
  64. PureReadFile:
  65.     pop    ax        ; com/exe flag
  66.     call    LoadProgram    ; call the com/exe loader
  67.     call    FileLoadErr
  68.     ret
  69. doFileRead ENDP
  70. FileLoadErr    PROC
  71.     jc    lperr        ; handle errors
  72.     PRINT_MESSAGE    <10,13,"Size: ">
  73.     mov    eax,FileLen
  74.     call    Printdword
  75.     clc
  76.     ret
  77. lperr:
  78.     PRINT_MESSAGE    <10,13,"Read error">
  79.     clc
  80.  
  81.     ret
  82. FileLoadErr    ENDP
  83. ;
  84. ; file write subroutine
  85. ;
  86. WriteProgram    PROC
  87.     mov ax,3c00h            ; Open the file
  88.     mov dx,offset writeFileName
  89.     sub    cx,cx
  90.     int 21h
  91.     mov bx,ax
  92.     jc failure
  93.  
  94.     mov    ecx,[writelen]
  95.     mov    si,[writeseg]
  96.     mov    dx,[writeofs]
  97.     push    ds
  98. wdlp:
  99.     push    ecx
  100.     mov    ds,si
  101.     mov ax,4000h            ; Read the file
  102.     cmp    ecx,8000h
  103.     jc    cursize
  104.     mov cx,8000h
  105. cursize:
  106.     int 21h
  107.     jc failure2
  108.     pop    ecx
  109.     add    si,800h
  110.     sub    ecx,8000h
  111.     jnc    wdlp
  112.     clc
  113. failure2:
  114.     pop    ds
  115. failure:
  116.     pushf
  117.     mov    ax,3e00H
  118.     int    21h
  119.     popf
  120.     ret
  121. WriteProgram ENDP
  122. ;
  123. ; file write command
  124. ;
  125. doFileWrite PROC
  126.     push    si
  127.     mov    [newaddr],0
  128.     mov    si,offset loadfile    ; move the original file name
  129.     mov    di,offset writeFileName
  130. fnl_lp:
  131.     lodsb
  132.     stosb
  133.     or    al,al
  134.     jnz    fnl_lp
  135.     mov    al,[exeflag]             ; set default params
  136.     mov    [EXEfileFlag],al             ; can't write EXE
  137.     mov    eax,[filelen]
  138.     mov    [writelen],eax
  139.     mov    ax,[userbasepsp]
  140.     add    ax,10H
  141.     mov    [writeseg],ax
  142.     mov    [writeofs],0
  143.     pop    si
  144.     call    WadeSpace            ; if no args go to write routine
  145.     jz    dowrite
  146.     cmp    al,'@'                ; see if address
  147.     jnz    gname
  148.     mov    [EXEfileFlag],0            ; yes update params
  149.     mov    [writelen],0
  150.     mov    [writeFileName],0
  151.     mov    [newaddr],1
  152.     inc    si
  153.     call    ReadAddress
  154.     jc    wn_err
  155.     call    defDS                ; get DS
  156.     mov    eax,ebx
  157.     shr    eax,4
  158.     add    edx,eax
  159.     and    ebx,15
  160.     mov    [writeofs],bx
  161.     mov    [writeseg],dx
  162.     test    edx,0FFF00000H
  163.     jnz    wn_err
  164. gname:
  165.     call    WadeSpace             ; now see if there is a name
  166.     jz    dowrite
  167.     cmp    al,','                ; noname, get a length
  168.     jz    getnum
  169.     mov    [EXEfileFlag],0            ; else get name
  170.     mov    di,offset writeFileName
  171. fclp:
  172.     mov    byte ptr [di],0
  173.     lodsb
  174.     cmp    al,13
  175.     jz    dowrite
  176.     cmp    al,' '
  177.     jz    getnum
  178.     cmp    al,','
  179.     jz    getnum
  180.     stosb
  181.     jmp    fclp
  182. getnum:
  183.     call    WadeSpace             ; now see if len
  184.     jz    dowrite
  185.     mov    [EXEfileFlag],0
  186.     call    ReadNumber
  187.     jc    wn_err
  188.     mov    ebx,eax
  189.     call    WadeSpace
  190.     jnz    wn_err
  191.     mov    [WriteLen],ebx
  192. dowrite:
  193.     test    [EXEfileFlag],0FFH        ; all params got, see if exe
  194.     jnz    noexewrite            ; yes, can't write
  195.     test    [writelen],0FFFFFFFFH        ; see if anything
  196.     jz    wn_err                ; no,err
  197.     movzx    eax,[writeseg]
  198.     shl    eax,4
  199.     add    eax,[writelen]
  200.     add    eax,0FH
  201.     test    eax,0FFF00000H            ; 1 MB limit
  202.     jnz    wn_err
  203.     call    WriteProgram            ; write prog
  204.     jnc    nowriterr
  205.     PRINT_MESSAGE    <10,13,"Write error">
  206. nowriterr:
  207.     test    [newaddr],1            ; update params
  208.     jnz    nochange
  209.     mov    eax,[writelen]
  210.     mov    [FileLen],eax
  211.     mov    al,[EXEfileFlag]
  212.     mov    [exeflag],al
  213.     mov    si,offset writeFileName
  214.     mov    di,offset loadfile
  215. fnl_lp1:
  216.     lodsb
  217.     stosb
  218.     or    al,al
  219.     jnz    fnl_lp1
  220.  
  221. nochange:
  222.     clc
  223.     ret
  224. noexewrite:
  225.     PRINT_MESSAGE    <10,13,"Can't write back an EXE file">
  226.     clc
  227.     ret
  228. wn_err:
  229.     stc
  230.     ret
  231. doFileWrite ENDP
  232. end