;==========================================================
HeadOn PROC near
pusha
mov cx,32+xtra_bytes ;bytes
mov ah,40h ;write to file
mov bx,WFileHandle ;handle of file
lea dx,exe_header ;data: where at
int 21h
jc headon_fail
headon_lop:
mov ah,3fh ;read
mov bx,RFileHandle ;handle
mov cx,63000 ;bytes
lea dx,loadword ;where at
int 21h
or ax,ax ;0 bytes ? !!!!
jz headon_end ; !!!!
add filesize,ax
mov cx,ax ;bytes
mov ah,40h ;write
mov bx,WFileHandle ;handle
lea dx,loadword ;where at
int 21h
jc headon_fail
jmp headon_lop
headon_end:
add filesize,32+xtra_bytes
xor cx,cx
mov dx,2 ;where at (cx:dx)
mov ax,4200h ;move filepointer to (from files begin)
mov bx,WFileHandle ;handle
int 21h
mov ax,filesize
and ax,511
mov loadword,ax ;length mod 512
mov ax,filesize
mov cl,9
shr ax,cl ;div 512
inc ax
mov [loadword+2],ax ;pages
mov cx,4 ;bytes
mov ah,40h ;write
mov bx,WFileHandle ;handle
lea dx,loadword ;where at
int 21h
lea dx,headon_
Call Print
popa
ret
headon_fail:
lea dx,headon_fail_
Call Print
CALL FileName
popa
mov al,-1
ret
headon_ db 'CREATED EXE-FILE',cr,lf,'$'
headon_fail_ db 'CANNOT WRITE TO $'
exe_header db 'MZ' ;offset 00 (EXE-signature)
dw 0,0 ;bytes on last page, pages
dw 0 ;relocations
dw 2+(xtra_bytes shr 4) ;size of header in paragraphs
dw 1000h,-1 ;minimum, maximum memory
dw 0fff0h,0fffeh ;ss,sp values (ss=PSP)
dw 0 ;checksum
dw 100h,0fff0h ;ip,cs values (cs=PSP)
dw 1ch ;offset to reloc table
dw 0,0,0 ;overlay number, 0,0 (fill-ups)
db xtra_bytes dup(0)
HeadOn ENDP
;==========================================================
CloseRFile PROC near
pusha
mov bx,RFileHandle
mov ah,3eh
int 21h
popa
ret
CloseRFile ENDP
;==========================================================
CloseWFile PROC near
pusha
mov bx,WFileHandle
mov ah,3eh
int 21h
popa
ret
CloseWFile ENDP
;==========================================================
NoFile PROC near
pusha
lea dx,nofile_
Call Print
Call FileName
popa
ret
nofile_ db 'CANNOT OPEN FILE $'
NoFile ENDP
;==========================================================
OpenRFile PROC near
pusha
mov dx,82h ;asciiz = cmdline
mov ax,3d00h ;open it for read
int 21h
jc openrfile_fail
mov RFileHandle,ax
lea dx,openrfile_
Call Print
Call FileName
popa
ret
openrfile_fail:
popa
mov al,-1
ret
openrfile_ db 'OPENED FILE $'
OpenRFile ENDP
;==========================================================
OpenWFile PROC near
pusha
xor ah,ah
mov al,ds:80h
mov di,ax
mov ds:[di+80h-2],'XE'
mov byte ptr ds:[di+80h],'E'
mov dx,82h ;asciiz = cmdline
mov ah,3ch ;open it for write
mov cx,0 ;attribute
int 21h
cmp ax,0
je openwfile_fail
mov WFileHandle,ax
lea dx,openwfile_
Call Print
Call FileName
popa
ret
openwfile_fail:
popa
mov al,-1
ret
openwfile_ db 'OPENED FILE $'
OpenWFile ENDP
;==========================================================
CmdLine PROC near
pusha
xor ah,ah
mov al,ds:80h
cmp ax,6 ;less than 5 chars (cmd-LINE) incl. ret?
jl cmdline_fail
mov di,ax
mov word ptr ds:[di+81h],'$'*256
popa
ret
cmdline_fail:
popa
mov al,-1
ret
CmdLine ENDP
;==========================================================
Header PROC near
pusha
lea dx,header_
Call Print
popa
ret
header_ db cr,lf
db '--==-- COM2EXE --==-- by HENDR璛 of OBSESSION',cr,lf,'$'
Header ENDP
;==========================================================
Footer PROC near
pusha
lea dx,footer_
Call Print
popa
ret
footer_ db '--==**==-- COM2EXE --==**==--',cr,lf,'$'
Footer ENDP
;==========================================================
Usage PROC near
pusha
lea dx,usage_
Call Print
popa
ret
usage_ db 'USAGE: C2E PROGRAM.COM',cr,lf,'$'
Usage ENDP
;==========================================================
Print PROC near
mov ah,09h
int 21h
ret
Print ENDP
;==========================================================
Return PROC near
pusha
lea dx,return_
Call Print
popa
ret
return_ db cr,lf,'$'
Return ENDP
;==========================================================
FileName PROC near
pusha
mov dx,82h
Call Print
Call Return
popa
ret
FileName ENDP
;==========================================================
Code ENDS
END Start
[ Last edited by 本是 on 2007-1-21 at 01:22 AM ]作者: 本是 时间: 2007-1-21 01:43 通过C2E把COM转换成EXE的实验,我发觉EXE文件最小的长度应该是33字节!
先把5楼的首行equ后的256改成0,存为c2e.asm,
masm c2e;
link c2e;
exe2bin c2e
再在debug下生成0、1、2、3、4字节的COM文件:
debug
n0.com
rcx
0
w
a100
ret
[ Last edited by enjoyer on 2007-1-21 at 10:06 AM ]作者: 本是 时间: 2007-1-22 04:58 这一点我已经试验并思考多次:EXE文件头02h和14h的word值都改成负值、再把执行代码藏在文件头的某些位置,但都不成功。原因可能是EXE格式本身规定所致:我用俄罗斯跟踪工具INSIGHT跟踪的结果是----如果填入0、0FFFEh,并把C3填入文件头的第1Fh字节,载入时预备运行位置是在无符号值0FFFEh而不是有符号值的-1处!也就是说执行代码位置只能为正,为0时执行内存映象----通常为上次运行过的程序映象!如果我的观察没有出错,确实EXE文件最小值就是33字节!!!
[ Last edited by 本是 on 2007-1-22 at 04:59 AM ]作者: nyuser 时间: 2007-1-25 01:37 强! 编程是艺术啊!!!作者: Jneny 时间: 2007-1-25 01:52 我要学习到这个地步得多少年呀,想下就晕作者: atoms 时间: 2010-3-7 14:59 标题: aaaaa