home *** CD-ROM | disk | FTP | other *** search
/ Computer Installation Guide - Dragon Clan Series / CD2.iso / DOSTOOLS / EXE2COM / C2E.ASM next >
Encoding:
Assembly Source File  |  1997-08-18  |  6.9 KB  |  270 lines

  1.     .286
  2.  
  3. Code    SEGMENT para PUBLIC 'CODE'
  4.  
  5.         Assume  CS:Code, DS:Code
  6.         ORG     100h
  7.  
  8. ;====================================================================
  9. cr    equ    10
  10. lf    equ    13
  11.  
  12. ;====================================================================
  13. Start:
  14.     Call    Header          ;logo
  15.     Call    CmdLine
  16.     cmp    al,-1
  17.     je    _usage             
  18.     Call    OpenRFile       ;open readfile
  19.     cmp    al,-1              
  20.     je    _norfile       
  21.     Call    OpenWFile    ;open writefile
  22.     cmp    al,-1
  23.     je    _nowfile
  24.     Call    HeadOn        ;write header
  25. _closewfile: Call CloseWFile    ;close writefile
  26.     jmp    _closerfile
  27. _nowfile: Call    NoFile
  28. _closerfile: Call CloseRFile    ;close readfile
  29.            jmp    _ende              
  30. _norfile: Call    NoFile
  31.     jmp    _ende                       
  32. _usage:    Call    Usage           ;display usage
  33. _ende:    Call    Footer          ;display ---------------------
  34.         mov     ax,4c00h           
  35.         int     21h                
  36.                                       
  37. ;====================================================================
  38. HeadOn    PROC near
  39.     pusha
  40.     mov    cx,32        ;bytes
  41.     mov    ah,40h        ;write to file
  42.     mov    bx,WFileHandle    ;handle of file
  43.     lea    dx,exe_header    ;data: where at
  44.     int    21h
  45.     jc    headon_fail
  46.                         
  47. headon_lop:
  48.     mov    ah,3fh        ;read    
  49.     mov    bx,RFileHandle    ;handle  
  50.     mov    cx,63000    ;bytes
  51.     lea    dx,loadword    ;where at
  52.     int    21h                      
  53.     or    ax,ax        ;0 bytes ?    !!!!
  54.     jz    headon_end    ;        !!!!
  55.     add    filesize,ax
  56.     mov    cx,ax        ;bytes
  57.           mov    ah,40h        ;write
  58.     mov    bx,WFileHandle    ;handle
  59.     lea    dx,loadword    ;where at
  60.     int    21h     
  61.     jc    headon_fail
  62.     jmp    headon_lop
  63. headon_end:              
  64.     add    filesize,32
  65.     xor    cx,cx
  66.     mov    dx,2        ;where at (cx:dx)
  67.     mov    ax,4200h    ;move filepointer to (from files begin)
  68.     mov    bx,WFileHandle    ;handle
  69.     int    21h                      
  70.     mov    ax,filesize           
  71.     and    ax,511                         
  72.     mov    loadword,ax    ;length mod 512
  73.     mov    ax,filesize                    
  74.     mov    cl,9                           
  75.     shr    ax,cl         ;div 512       
  76.     inc    ax
  77.     mov    [loadword+2],ax    ;pages
  78.     mov    cx,4        ;bytes
  79.           mov    ah,40h        ;write
  80.     mov    bx,WFileHandle    ;handle
  81.     lea    dx,loadword    ;where at
  82.     int    21h     
  83.     lea      dx,headon_
  84.     Call     Print
  85.     popa 
  86.           ret  
  87. headon_fail:
  88.     lea    dx,headon_fail_
  89.     Call    Print                   
  90.     CALL    FileName
  91.     popa                            
  92.     mov    al,-1
  93.     ret                             
  94. headon_ db      'CREATED EXE-FILE',cr,lf,'$'
  95. headon_fail_ db 'CANNOT WRITE TO $'
  96. exe_header db    'MZ'        ;offset 00 (EXE-signature)
  97.     dw    0,0        ;bytes on last page, pages
  98.     dw    0        ;relocations
  99.     dw    2        ;size of header in paragraphs
  100.     dw    1000h,-1    ;minimum, maximum memory
  101.     dw    0fff0h,0fffeh    ;ss,sp values (ss=PSP)
  102.     dw    0        ;checksum
  103.     dw    100h,0fff0h    ;ip,cs values (cs=PSP)
  104.     dw    1ch        ;offset to reloc table
  105.     dw    0,0,0        ;overlay number, 0,0 (fill-ups)
  106. HeadOn    ENDP
  107.  
  108. ;====================================================================
  109. CloseRFile PROC    near
  110.           pusha      
  111.     mov       bx,RFileHandle
  112.     mov    ah,3eh
  113.     int    21h 
  114.     popa         
  115.     ret          
  116. CloseRFile ENDP       
  117.  
  118. ;====================================================================
  119. CloseWFile PROC    near
  120.           pusha      
  121.           mov       bx,WFileHandle
  122.     mov    ah,3eh        
  123.     int    21h           
  124.     popa                  
  125.           ret                   
  126. CloseWFile ENDP
  127.  
  128. ;====================================================================
  129. NoFile    PROC    near
  130.        pusha
  131.     lea      dx,nofile_
  132.     Call    Print     
  133.     Call    FileName  
  134.     popa              
  135.     ret               
  136. nofile_    db    'CANNOT OPEN FILE $'
  137. NoFile    ENDP              
  138.                           
  139. ;====================================================================
  140. OpenRFile PROC    near 
  141.          pusha        
  142.     mov    dx,82h        ;asciiz = cmdline
  143.     mov    ax,3d00h    ;open it for read
  144.     int    21h  
  145.     jc       openrfile_fail
  146.     mov    RFileHandle,ax    
  147.     lea    dx,openrfile_
  148.     Call    Print   
  149.     Call    FileName
  150.     popa              
  151.     ret               
  152. openrfile_fail:
  153.     popa
  154.     mov    al,-1
  155.     ret
  156. openrfile_ db    'OPENED FILE $'
  157. OpenRFile ENDP
  158.  
  159. ;====================================================================
  160. OpenWFile PROC    near
  161.          pusha
  162.          xor      ah,ah
  163.          mov    al,ds:80h
  164.          mov    di,ax
  165.     mov    ds:[di+80h-2],'XE'
  166.     mov    byte ptr ds:[di+80h],'E'
  167.          mov    dx,82h        ;asciiz = cmdline
  168.     mov    ah,3ch        ;open it for write
  169.     mov    cx,0        ;attribute
  170.     int    21h  
  171.     cmp    ax,0
  172.     je       openwfile_fail
  173.     mov    WFileHandle,ax    
  174.     lea    dx,openwfile_
  175.     Call    Print   
  176.     Call    FileName
  177.     popa              
  178.     ret               
  179. openwfile_fail:      
  180.          popa         
  181.     mov     al,-1
  182.     ret          
  183. openwfile_ db    'OPENED FILE $'
  184. OpenWFile ENDP       
  185.                      
  186. ;====================================================================
  187. CmdLine    PROC    near 
  188.     pusha        
  189.     xor      ah,ah
  190.     mov    al,ds:80h
  191.     cmp    ax,6         ;less than 5 chars (cmd-LINE) incl. ret?
  192.       jl    cmdline_fail
  193.     mov    di,ax                                                        
  194.     mov    word ptr ds:[di+81h],'$'*256
  195.     popa
  196.     ret
  197. cmdline_fail:
  198.     popa
  199.     mov    al,-1
  200.     ret           
  201. CmdLine    ENDP
  202.  
  203. ;====================================================================
  204. Header    PROC    near
  205.     pusha             
  206.     lea    dx,header_
  207.     Call    Print
  208.     popa         
  209.     ret          
  210. header_    db    cr,lf
  211.     db    '∙∙--==--∙∙ COM2EXE ∙∙--==--∙∙ by HENDR¡X of OBSESSION',cr,lf,'$'
  212. Header    ENDP                            
  213.                                               
  214. ;====================================================================
  215. Footer    PROC    near                          
  216.           pusha                                 
  217.     lea      dx,footer_         
  218.     Call    Print                         
  219.     popa                                  
  220.     ret                                   
  221. footer_    db        '--==**==-- COM2EXE --==**==--',cr,lf,'$'
  222. Footer    ENDP                                  
  223.                                                   
  224. ;====================================================================
  225. Usage    PROC    near                              
  226.           pusha                                     
  227.     lea      dx,usage_
  228.     Call    Print                             
  229.     popa                                      
  230.          ret                                       
  231. usage_     db    'USAGE: C2E PROGRAM.COM',cr,lf,'$'
  232. Usage    ENDP                                      
  233.                    
  234. ;====================================================================
  235. Print    PROC    near
  236.     mov    ah,09h
  237.     int    21h
  238.     ret        
  239. Print    ENDP       
  240.  
  241. ;====================================================================
  242. Return    PROC    near
  243.     pusha
  244.     lea    dx,return_
  245.     Call    Print
  246.     popa
  247.     ret 
  248. return_    db    cr,lf,'$'
  249. Return    ENDP
  250.  
  251. ;====================================================================
  252. FileName PROC    near
  253.      pusha
  254.     mov      dx,82h
  255.     Call    Print 
  256.     Call    Return
  257.     popa
  258.     ret
  259. FileName ENDP
  260.  
  261. ;====================================================================
  262. filesize    dw    0
  263. RFileHandle    dw    ?
  264. WFileHandle    dw    ?
  265. loadword    dw    ?
  266.                      
  267. ;====================================================================
  268. Code    ENDS
  269. END     Start      
  270.