2023年12月7日发(作者:)

dos下的exe文件格式解析

dos下的exe文件格式解析:

1、源代码

data segment

Msg DB 'Hello 李国雄!$'

data ends

code segment

Start:

MOV AX,data

MOV DS,AX

MOV DX,OFFSET Msg

MOV AH,9

INT 21h

MOV AH,4ch

INT 21h

code ends

END Start

我用的是masm6.15编译生成exe文件

2、exe文件头内容

exe文件头的具体格式可在以下链接得到/djgpp/doc/exe/

以下是本文件exe文件头的具体内容

0000-0001h 4D5A(MZ)

0002-0003h 2000

0004-0005h 0200(the size of file is 1 * 512 + 2 * 16 = 544byte)

0006-0007h 0100(number of relocation)

0008-0009h 2000(the header size of file is 2 * 256 = 512byte)

000A-000Bh 0000

000C-000Dh FFFF

000E-000Fh 0000(initialize the SS register)

0010-0011h 0000(initialize the SP register)

0012-0013h 0000

0014-0015h 0000(initialize the IP register)

0016-0017h 0100(initialize the CS register)

0018-0019h 1E00(offset of the first relocation item in the file)

001A-001Bh 0000

3、debug反汇编exe文件

打开cmd,输入debug *.exe

-r查看ds、es的段地址,即psp段地址,该地址有程序加载器根据内存情况决定,程序事先并不知道,ss的值由psp段地址+0010(psp的

大小)+0000(0E-0Fh的值)得到,cs可相应得到,本文件有一个重定位项,在文件偏移001E处,重定位处的内容修改为psp段地址

+0010(psp的大小)+重定位处原先的内容。