home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / progrmng / xm.lzh / XMFILE.ASM < prev    next >
Assembly Source File  |  1990-10-08  |  4KB  |  256 lines

  1.         .386
  2. O_APPEND    equ    8
  3. O_CREAT        equ    100h
  4. O_TRUNC        equ    200h
  5. O_EXCL        equ    400h
  6.  
  7.  
  8.         include xmhd.inc
  9.         include xmmac.mac
  10.         include    termio.inc
  11.         include    stat.inc
  12.         include    exp.inc
  13.         include xmmsg.def
  14.  
  15. _DATA        segment    para public USE16 'DATA'
  16.         
  17.         
  18.         extrn    EData:byte, EBSS:byte
  19.         extrn    ECode:byte
  20.         extrn    Param:dword
  21.         extrn    DosWorkI: byte, DosWorkO: byte
  22.         extrn    PSPWork: dword
  23.         public    fhdr
  24.  
  25. ;FileName    db    MAXFILENAME+2 dup (0)
  26.  
  27. fhdr        filehdr    <>
  28. comment %
  29. ahdr        aouthdr    <>
  30. shdr        scnhdr    <>
  31. %
  32.  
  33. NotSupported    db    'This format is not supported',13,10,0
  34. Default_ext    db    '.exp',0
  35.  
  36.         public    Handle
  37. Handle        dw    ?
  38.  
  39. _DATA        ends
  40. ;
  41. _TEXT        segment    para public USE16 'CODE'
  42.         assume    cs:_TEXT, ds:_DATA
  43. ;
  44.         public    ReadFOHdr, ReadSHdr
  45.         public    OpenExe, AddExtention
  46.         extrn    OSExit: near
  47. IF FMR70
  48.         extrn    a20_off: near, a20_on:near
  49. ENDIF
  50.  
  51. ReadFOHdr    proc    near        ; read file header 
  52. IF FMR70
  53.         call    a20_off
  54. ENDIF
  55.         mov    bx, Handle
  56.         mov    ah, 3fh
  57.         mov    dx, offset fhdr
  58.         mov    cx, (size fhdr) 
  59.         int    21h
  60.         jc    Error
  61.         cmp    ax, cx
  62.         jc    Error
  63.  
  64.         mov    ax, fhdr.f_magic
  65.         cmp    ax, EXPMAGIC
  66.         je    read_fh_ok
  67.         cmp    ax, AoutMagic
  68.         je    read_fh_ok
  69.         jne    Error
  70. read_fh_ok:
  71.         ret
  72. ReadFOHdr    endp
  73.  
  74. ReadSHdr    proc    near        ; read section headers
  75.         mov    bx, offset fhdr
  76.         cmp    [bx].f_magic, EXPMAGIC
  77.         jne    read_aout
  78. read_exp:        
  79.         mov    dx, [bx].f_nhdr
  80.         shl    dx, 4
  81.         mov    cx, 0
  82.         mov    bx, Handle
  83.         mov    ah, 42h       ; skip header parts
  84.         mov    al, 0        ; seek from top of file
  85.         int    21h        ; 
  86.         jc    Error
  87. ;read code
  88.         movzx    ebx, Handle
  89.         mov    Param[0], ebx
  90.         mov    eax, ECode.Base
  91.         mov    Param[4],eax        ; code area to load in
  92.         push    eax
  93.         movzx    eax, fhdr.f_npage
  94.         shl    eax, 9
  95.         movzx    ebx, fhdr.f_nbyte
  96.         add    eax, ebx
  97.         movzx    ebx, fhdr.f_nhdr
  98.         shl    ebx, 4
  99.         sub    eax, ebx
  100.         mov    Param[8], eax        ; bytes to readin
  101.         mov    si, psp_mem_min
  102.         mov    dword ptr PSPwork[si], eax
  103.         mov    ECode.Hi, eax
  104. ;
  105.         push    eax
  106.         neg    eax
  107.         add    eax, EData.Hi
  108. ;        sub    eax, EData.Base
  109.         mov    si, psp_mem_bss
  110.         mov    dword ptr PSPwork[si], eax
  111.         
  112.         mov    eax, EData.Hi
  113. ;        sub    eax, EData.Base
  114.         mov    si, psp_mem_max
  115. ;
  116. ; allocate communication buffer
  117. ;
  118.         sub    eax, 1000h
  119.         mov    dword ptr PSPwork[si], eax
  120.         mov    EBSS.Hi, eax
  121.         pop    eax
  122. ;        
  123.         pop    ebx
  124.         add    eax, ebx        ; start + size
  125. ;        mov    ECode.Hi, eax
  126.         inc    eax
  127.         mov    EBSS.Lo, eax
  128.         xor    eax,eax
  129. ;        mov    ECode.Lo, eax
  130. ;        mov    EData.Lo, eax
  131.  
  132. ; setup transfer addres in es:edi
  133. ;
  134.         push    es
  135.         mov    ax, ds
  136.         mov    es, ax
  137.         movzx    eax, ax
  138.         shl    eax, 4
  139.         mov    edi, ECode.Base     ;48bit pointer
  140.         sub    edi, eax    
  141.         cld
  142. ;        
  143. ; read file into inner buffer    
  144. .readloop:
  145. IF FMR70
  146.         call    a20_off
  147. ENDIF
  148.         mov    cx, PageSize
  149.         lea    dx, DosWorkI        ;PageBuffer
  150.         mov    ah, 3fh
  151.         mov    bx, Handle
  152.         int    21h
  153.         jc    Error
  154. ;
  155.         mov    cx, ax
  156.         jcxz    .done
  157. IF FMR70
  158.         call    a20_on
  159. ENDIF
  160.         lea    esi, DosWorkI        ;PageBuffer
  161.         prefix32
  162.     rep    movsb
  163.         jmp    .readloop
  164. .done:
  165.         pop    es
  166.         ret 
  167. ReadSHdr    endp
  168.  
  169. AddExtention    proc    near    
  170.         push    di
  171.         push    si
  172.         push    cx
  173.         push    ax
  174.         mov    di, offset fileName
  175.         mov    al, 0
  176.         mov    cx, MAXFILENAME
  177.         cld
  178.     repne    scasb
  179.         cmp    cx, 0
  180.         je    .add_no
  181.         dec    di
  182.         mov    si, offset default_ext
  183.         mov    cx, 5            ; '.xxx'+EOS
  184.     rep    movsb
  185. .add_no:
  186.         pop    ax
  187.         pop    cx
  188.         pop    si
  189.         pop    di
  190.         ret
  191. AddExtention    endp
  192.  
  193. OpenExe        proc    near
  194. IF FMR70
  195.         call    a20_off
  196. ENDIF
  197.         mov    al, byte ptr FileName
  198.         cmp    al, 0
  199.         je    .file_not_found
  200.         mov    dx, offset FileName
  201.         mov    ax, 3d00h    ;open for reading
  202.         je    .file_not_found
  203.         int    21h
  204.         mov    Handle, ax
  205.         jnc    OEDone
  206.         call    AddExtention
  207. ; file not found error
  208.         mov    dx, offset FileName
  209.         mov    ax, 3d00h
  210.         int    21h
  211.         mov    Handle, ax
  212.         jnc    OEDone
  213.  
  214. ; file does not exist        
  215. .file_not_found:
  216.         mov    dx, offset XNameS
  217.         call    Msg
  218.         mov    dx, offset FileName
  219.         call    Msg
  220.         mov    dx, offset NFS
  221.         call    Msg
  222.         jmp    OSExit
  223. OEDone:        ret
  224. OpenExe        endp
  225.         model    small
  226.  
  227. read_aout    proc    near
  228.         cmp    [bx].a_magic, AoutMagic
  229.         jne    Error
  230.         cmp    [bx].a_CPU, AoutI386
  231.         jne    Error
  232.         mov    dx, offset NotSupported
  233.         call    Msg
  234.         mov    ax, [bx].a_CPU
  235.         shl    eax, 16
  236.         mov    ax, [bx].a_magic
  237. ;        
  238.         endp
  239. Error        proc    near
  240.         push    ax
  241.         call    hex32
  242.         mov    dx, offset ErrorS
  243.         mov    ax, _DATA
  244.         mov    ds, ax
  245.         pop    ax
  246.         call    Msg
  247.         jmp    OSExit
  248. Error        endp
  249.  
  250.  
  251.  
  252.  
  253. _TEXT        ends
  254.         end    
  255.  
  256.