home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / asm / MISC_ASM.ZIP / EXECOM.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-02-09  |  14.0 KB  |  297 lines

  1. comment *
  2.    EXECOM 1.2  (c) Copyright 1988  Thomas G. Hanlin III
  3. *
  4.  
  5. Sseg           segment byte stack 'prog'    ; dummy stack segment
  6. Sseg           ends
  7.  
  8. Cseg           segment byte public 'prog'
  9.                assume         cs:Cseg, ds:Cseg, ss:Sseg
  10.  
  11.  
  12.                org            100h
  13.  
  14. MAIN           proc           far
  15.                cld
  16.                mov            si,80h
  17.                lodsb                             ; get parm length
  18.                cbw
  19.                mov            cx,ax
  20.                jcxz           Inform             ;   inform 'em if no parms
  21.  
  22. ScanParms:     lodsb                             ; get a command-line char
  23.                cmp            al," "             ; is it a space?
  24.                jbe            ScanLoop2          ;   yes, ignore it
  25.                mov            di,offset OUTFILE  ; assume output file name
  26.                inc            ah                 ; increment parm number
  27.                cmp            ah,2               ; check it
  28.                ja             Inform             ;   too large, inform 'em
  29.                je             SaveFileName       ;   default OUTFILE is ok
  30.                mov            di,offset INFILE   ; input file name (string)
  31. SaveFileName:  stosb                             ; store a char of the filename
  32.                dec            cx                 ; are we done yet?
  33.                jz             ScanDone           ;   yes, exit
  34.                lodsb                             ; get another char
  35.                cmp            al," "             ; is it blank?
  36.                ja             SaveFileName       ;   no, must be a filename chr
  37. ScanLoop2:     loop           ScanParms          ;   keep looking for parms
  38.  
  39. ScanDone:      or             ah,ah              ; any parms?
  40.                jz             Inform             ;   no, inform 'em
  41.                dec            ah                 ; only one parm?
  42.                jnz            ChangeExts         ;   no, go change extensions
  43.                mov            si,offset INFILE   ; input file name
  44.                mov            di,offset OUTFILE  ; output file name
  45. DupeFile:      lodsb                             ; get an input char
  46.                or             al,al              ; is it null?
  47.                jz             ChangeExts         ;   yes, done- change exts
  48.                stosb                             ; store char in output name
  49.                jmp            DupeFile           ;   keep on truckin'
  50.  
  51. Inform:        mov            dx,offset INFORMATION
  52.                mov            ah,9
  53.                int            21h
  54.                dec            EXITCODE
  55.                jmp            Abort
  56.  
  57. ChangeExts:    mov            si,offset INFILE   ; input file name
  58. Change1:       lodsb                             ; get a char
  59.                or             al,al              ; are we done?
  60.                jz             AddExtIn1          ;   yes, needs extension
  61.                cmp            al,"."             ; is it a period?
  62.                jne            Change1            ;   no, keep looking
  63.  
  64. ChangeExt2:    mov            si,offset OUTFILE  ; output file name
  65. Change2:       lodsb                             ; get a char
  66.                or             al,al              ; are we done?
  67.                jz             AddExtIn2          ;   yes, needs extension
  68.                cmp            al,"."             ; is it a period?
  69.                jne            Change2            ;   no, keep looking
  70.                lodsw                             ; is the extension ".EXE"?
  71.                cmp            ax,"XE"            ;
  72.                jne            OpenFiles          ;   no, go open the files
  73.                lodsb                             ;
  74.                cmp            al,"E"             ;
  75.                jne            OpenFiles          ;   no, go open the files
  76.                sub            si,3               ; pretend there's no ext
  77.  
  78. AddExtIn2:     dec            si                 ; move back to the first null
  79.                mov            di,si
  80.                mov            ax,"C."            ; install a ".COM"...
  81.                stosw                             ; ...file extension
  82.                mov            ax,"MO"            ;
  83.                stosw                             ;
  84.                jmp            OpenFiles
  85.  
  86. AddExtIn1:     dec            si                 ; move back to the first null
  87.                mov            di,si
  88.                mov            ax,"E."            ; install an ".EXE"...
  89.                stosw                             ; ...file extension
  90.                mov            ax,"EX"            ;
  91.                stosw                             ;
  92.                jmp            ChangeExt2
  93.  
  94.  
  95. ;------------------- Everything's set up, let's open the files ----------------
  96.  
  97.  
  98. OpenFiles:     mov            ax,3D00h           ; open file for read
  99.                mov            dx,offset INFILE   ; input file name
  100.                int            21h
  101.                jnc            InputOpenOk
  102.                mov            cl,1
  103.                jmp            ErrVector
  104.  
  105. InputOpenOk:   mov            INHANDLE,ax        ; save input file handle
  106.                mov            bx,ax
  107.                mov            ah,3Fh             ; read from file
  108.                mov            cx,24              ; 24 bytes
  109.                mov            dx,offset LASTBYTE ; data buffer
  110.                int            21h
  111.                jnc            ReadOk
  112.                mov            cl,9
  113.                jmp            ErrVector
  114.  
  115. ReadOk:        mov            si,dx
  116.                mov            di,offset EXE_SIG
  117.                mov            cx,5       ; set EXE_SIG, EXE_EXCESS, EXE_PAGES,
  118.                rep movsw                 ;     EXE_RELOCCOUNT, EXE_HDRSIZE
  119.                add            si,4
  120.                movsw                             ; set EXE_SS
  121.                movsw                             ; set EXE_SP
  122.                add            si,2
  123.                movsw                             ; set EXE_IP
  124.                movsw                             ; set EXE_CS
  125.  
  126.                mov            cl,3               ; default error code
  127.                cmp            EXE_SIG,"ZM"       ; is the signature ok?
  128.                jne            ErrVector          ;   nope, error
  129.                inc            cx                 ; inc error code
  130.                cmp            EXE_RELOCCOUNT,0   ; any relocations needed?
  131.                jne            ErrVector          ;   yes, error
  132.                inc            cx                 ; inc error code
  133.                cmp            EXE_SS,0           ; is there a defined SS?
  134.                jne            ErrVector          ;   yes, error
  135.                cmp            EXE_SP,0           ; is there a defined SP?
  136.                jne            ErrVector          ;   yes, error
  137.                inc            cx                 ; inc error code
  138.                cmp            EXE_CS,0           ; is there a defined CS?
  139.                jne            ErrVector          ;   yes, error
  140.                inc            cx                 ; inc error code
  141.                cmp            EXE_IP,0           ; is the IP = 0?
  142.                je             ChecksOk           ;   yes, it's ok
  143.                cmp            EXE_IP,0100h       ; is the IP = 0100h?
  144.                jne            ErrVector          ;   no, error
  145.  
  146. ChecksOk:      mov            bx,EXE_HDRSIZE
  147.                mov            cl,4
  148.                shl            bx,cl              ; CodeStart in BX ****
  149.                mov            ax,EXE_PAGES
  150.                dec            ax
  151.                mov            cx,512
  152.                mul            cx
  153.                sub            ax,bx
  154.                sbb            dx,0               ; CodeSize in DX:AX ***
  155.                mov            cx,EXE_EXCESS
  156.                jcxz           NoDribble
  157.                add            ax,cx
  158.                jmp            GotCodeSize
  159. NoDribble:     add            ax,512
  160. GotCodeSize:   adc            dx,0
  161.                or             dx,dx              ; is it too large?
  162.                jz             CheckIP            ;   no, keep on truckin'
  163.                mov            cl,8
  164. ErrVector:     jmp            ErrorExit
  165.  
  166. CheckIP:       push           ax
  167.                cmp            EXE_IP,0100h       ; is IP right for .COM files?
  168.                je             IPok               ;   yes...
  169.                mov            dx,offset WARNING
  170.                mov            ah,9               ; display warning message
  171.                int            21h
  172. IPok:          mov            dx,bx              ; CodeStart
  173.                xor            cx,cx
  174.                add            dx,EXE_IP
  175.                adc            cx,cx
  176.                mov            bx,INHANDLE
  177.                mov            ax,4200h           ; move file ptr from start
  178.                int            21h
  179.                mov            ah,3Ch             ; create file for write
  180.                mov            dx,offset OUTFILE  ; output file name
  181.                xor            cx,cx              ; normal file attribute
  182.                int            21h
  183.                pop            si
  184.                jnc            OutputOpenOk
  185.                mov            cl,2
  186.                jmp            ErrVector
  187. OutputOpenOk:  mov            OUTHANDLE,ax       ; save output file handle
  188.                sub            si,EXE_IP          ; ignore IP bytes
  189.                mov            dx,offset LASTBYTE ; buffer location
  190.                mov            cx,32 * 1024       ; buffer size: 32K
  191.  
  192.  
  193. ; ----------------- Just copy the code from the EXE to the COM file -----------
  194.  
  195.  
  196. WriteLoop:     or             si,si
  197.                jz             Done
  198.                cmp            si,cx
  199.                ja             ReadBlock
  200.                mov            cx,si
  201. ReadBlock:     mov            ah,3Fh             ; read from file
  202.                mov            bx,INHANDLE
  203.                int            21h
  204.                jc             ReadError
  205.                mov            ah,40h             ; write to file
  206.                mov            bx,OUTHANDLE
  207.                int            21h
  208.                jc             WriteError
  209.                sub            si,cx
  210.                jmp            WriteLoop
  211.  
  212. ReadError:     mov            cl,9
  213.                jmp            ErrorExit0
  214. WriteError:    mov            cl,10
  215.  
  216.  
  217. ;---------------------------- Error exit handler ------------------------------
  218.  
  219.  
  220. ErrorExit0:    mov            ah,3Eh             ; close a file
  221.                mov            bx,OUTHANDLE       ; ...output file
  222.                int            21h
  223.  
  224. ErrorExit:     mov            EXITCODE,cl
  225.                mov            ah,9               ; display error message
  226.                mov            dx,offset FATALERR ; "Fatal error: "
  227.                int            21h
  228.                mov            si,offset CANTOPEN ; first error message
  229.                xor            ch,ch
  230.                jmp            FindErr1
  231. FindErr:       lodsb                             ; get a char
  232.                cmp            al,"$"             ; end of message?
  233.                jne            FindErr            ;   no, keep looking
  234. FindErr1:      loop           FindErr            ;   get the message we want
  235.                mov            dx,si              ; pointer to error message
  236. ErrDone:       mov            ah,9
  237.                int            21h
  238.                cmp            EXITCODE,1         ; did we open the input file?
  239.                je             Abort              ;   no, don't close or delete
  240.  
  241. Done:          mov            ah,3Eh             ; close a file
  242.                mov            bx,INHANDLE        ; input file
  243.                int            21h
  244.                cmp            EXITCODE,0         ; successful conversion?
  245.                jnz            Abort              ;   no, just exit
  246.                mov            ah,41h             ; delete file
  247.                mov            dx,offset INFILE   ; ...the original input file
  248.                int            21h
  249. Abort:         mov            ah,4Ch             ; terminate program
  250.                mov            al,EXITCODE        ; error exit code
  251.                int            21h
  252. MAIN           endp
  253.  
  254.  
  255. WARNING        db "WARNING: The default IP is not 0100h.",13,10
  256.                db "The output file will not be executable.",13,10,"$"
  257.  
  258. FATALERR       db "Fatal error: $"
  259. CANTOPEN       db "Unable to open input file",13,10,"$"
  260. CANTMAKE       db "Unable to create output file",13,10,"$"
  261. BADSIG         db "Input file is not a valid .EXE file",13,10,"$"
  262. RELITEMS       db "There are items requiring relocation",13,10,"$"
  263. BADSTACK       db "A stack is defined",13,10,"$"
  264. BADCSEG        db "A code segment is defined",13,10,"$"
  265. BADCOFS        db "The code offset is neither 0 nor 0100h",13,10,"$"
  266. TOOBIG         db "File is too large (.COM would be over 64K)",13,10,"$"
  267. CANTREAD       db "Unable to read input file",13,10,"$"
  268. CANTWRITE      db "Unable to write to output file",13,10,"$"
  269.  
  270.  
  271. INFORMATION    db "EXECOM 1.2  Copyright (c) 1989  Thomas G. Hanlin III",13,10
  272.                db "  Purpose: converts an .EXE file to a .COM file.",13,10
  273.                db "  Syntax : EXECOM inputfile[.EXE] [outputfile[.COM]]",13,10
  274.                db "$"
  275.  
  276. INHANDLE       dw ?
  277. OUTHANDLE      dw ?
  278. EXE_SIG        dw ?       ; EXE signature
  279. EXE_EXCESS     dw ?       ; bytes in excess of the 512b EXE_PAGES count
  280. EXE_PAGES      dw ?       ; code size, in 512b pages
  281. EXE_RELOCCOUNT dw ?       ; count of items in the relocation table
  282. EXE_HDRSIZE    dw ?       ; header size, in 16b paragraphs
  283. EXE_SS         dw ?       ; default SS
  284. EXE_SP         dw ?       ; default SP
  285. EXE_IP         dw ?       ; default IP
  286. EXE_CS         dw ?       ; default CS
  287.  
  288. EXITCODE       db 0       ; program error exit code
  289.  
  290. INFILE         db 80 dup (0)   ; input file name
  291. OUTFILE        db 80 dup (0)   ; output file name
  292.  
  293. LASTBYTE:                 ; start of file buffer area
  294.  
  295. Cseg           ends
  296.                end            MAIN
  297.