前面讲的汇编编辑有两种方式,一种是通过DEBUG的A命令,另一种是通过文本编辑器,这两种编辑器编辑好的命令是不一样的,前一种可以直接通过DEBUG的t命令进行直接执行,后一种还需要经过MASM编译后才能用DEBUG调试,这里有一点点不同,不同有两点,每一点在于[0]表示上,DEBUG中[0]表示偏移量,而MASM中只表十进制0 第二点,数值默认在DEBUG是十六进制,而MASM是十进制。
对同一段代码示例如下:
【1】使用DEBUG命令实现
D:\Temp\npp.5.9.3.bin\tmp>debug
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0100 NV UP EI PL NZ NA PO NC
13F9:0100 0000 ADD [BX+SI],AL DS:0000=CD
-a 13F9:0
13F9:0000 mov ax,2000
13F9:0003 mov ds,ax
13F9:0005 mov al,[0]
13F9:0008 mov bl,[1]
13F9:000C mov cl,[2]
13F9:0010 mov dl,[3]
13F9:0014
-r ip
IP 0100
:0
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0000 NV UP EI PL NZ NA PO NC
13F9:0000 B80020 MOV AX,2000
-u
13F9:0000 B80020 MOV AX,2000
13F9:0003 8ED8 MOV DS,AX
13F9:0005 A00000 MOV AL,[0000]
13F9:0008 8A1E0100 MOV BL,[0001]
13F9:000C 8A0E0200 MOV CL,[0002]
13F9:0010 8A160300 MOV DL,[0003]
13F9:0014 5D POP BP
13F9:0015 0E PUSH CS
13F9:0016 1404 ADC AL,04
13F9:0018 0304 ADD AX,[SI]
13F9:001A 050002 ADD AX,0200
13F9:001D FFFF ??? DI
13F9:001F FFFF ??? DI
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0003 NV UP EI PL NZ NA PO NC
13F9:0003 8ED8 MOV DS,AX
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0005 NV UP EI PL NZ NA PO NC
13F9:0005 A00000 MOV AL,[0000] DS:0000=00
-d 2000:0
2000:0000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0010 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0040 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0008 NV UP EI PL NZ NA PO NC
13F9:0008 8A1E0100 MOV BL,[0001] DS:0001=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=000C NV UP EI PL NZ NA PO NC
13F9:000C 8A0E0200 MOV CL,[0002] DS:0002=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0010 NV UP EI PL NZ NA PO NC
13F9:0010 8A160300 MOV DL,[0003] DS:0003=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0014 NV UP EI PL NZ NA PO NC
13F9:0014 5D POP BP
-
【2】使用MASM实现
D:\Temp\npp.5.9.3.bin\tmp>debug
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0100 NV UP EI PL NZ NA PO NC
13F9:0100 0000 ADD [BX+SI],AL DS:0000=CD
-a 13F9:0
13F9:0000 mov ax,2000
13F9:0003 mov ds,ax
13F9:0005 mov al,[0]
13F9:0008 mov bl,[1]
13F9:000C mov cl,[2]
13F9:0010 mov dl,[3]
13F9:0014
-r ip
IP 0100
:0
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0000 NV UP EI PL NZ NA PO NC
13F9:0000 B80020 MOV AX,2000
-u
13F9:0000 B80020 MOV AX,2000
13F9:0003 8ED8 MOV DS,AX
13F9:0005 A00000 MOV AL,[0000]
13F9:0008 8A1E0100 MOV BL,[0001]
13F9:000C 8A0E0200 MOV CL,[0002]
13F9:0010 8A160300 MOV DL,[0003]
13F9:0014 5D POP BP
13F9:0015 0E PUSH CS
13F9:0016 1404 ADC AL,04
13F9:0018 0304 ADD AX,[SI]
13F9:001A 050002 ADD AX,0200
13F9:001D FFFF ??? DI
13F9:001F FFFF ??? DI
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0003 NV UP EI PL NZ NA PO NC
13F9:0003 8ED8 MOV DS,AX
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0005 NV UP EI PL NZ NA PO NC
13F9:0005 A00000 MOV AL,[0000] DS:0000=00
-d 2000:0
2000:0000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0010 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0040 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0008 NV UP EI PL NZ NA PO NC
13F9:0008 8A1E0100 MOV BL,[0001] DS:0001=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=000C NV UP EI PL NZ NA PO NC
13F9:000C 8A0E0200 MOV CL,[0002] DS:0002=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0010 NV UP EI PL NZ NA PO NC
13F9:0010 8A160300 MOV DL,[0003] DS:0003=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0014 NV UP EI PL NZ NA PO NC
13F9:0014 5D POP BP
-
从上面可以看出,这两个代码想要结果完全不同,因此,要实现我们想要的结果,也就是说取得偏移地址内存的值,我们应该这样写
D:\Temp\npp.5.9.3.bin\tmp>debug
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0100 NV UP EI PL NZ NA PO NC
13F9:0100 0000 ADD [BX+SI],AL DS:0000=CD
-a 13F9:0
13F9:0000 mov ax,2000
13F9:0003 mov ds,ax
13F9:0005 mov al,[0]
13F9:0008 mov bl,[1]
13F9:000C mov cl,[2]
13F9:0010 mov dl,[3]
13F9:0014
-r ip
IP 0100
:0
-r
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0000 NV UP EI PL NZ NA PO NC
13F9:0000 B80020 MOV AX,2000
-u
13F9:0000 B80020 MOV AX,2000
13F9:0003 8ED8 MOV DS,AX
13F9:0005 A00000 MOV AL,[0000]
13F9:0008 8A1E0100 MOV BL,[0001]
13F9:000C 8A0E0200 MOV CL,[0002]
13F9:0010 8A160300 MOV DL,[0003]
13F9:0014 5D POP BP
13F9:0015 0E PUSH CS
13F9:0016 1404 ADC AL,04
13F9:0018 0304 ADD AX,[SI]
13F9:001A 050002 ADD AX,0200
13F9:001D FFFF ??? DI
13F9:001F FFFF ??? DI
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=13F9 ES=13F9 SS=13F9 CS=13F9 IP=0003 NV UP EI PL NZ NA PO NC
13F9:0003 8ED8 MOV DS,AX
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0005 NV UP EI PL NZ NA PO NC
13F9:0005 A00000 MOV AL,[0000] DS:0000=00
-d 2000:0
2000:0000 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0010 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0020 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0030 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0040 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
2000:0070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0008 NV UP EI PL NZ NA PO NC
13F9:0008 8A1E0100 MOV BL,[0001] DS:0001=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=000C NV UP EI PL NZ NA PO NC
13F9:000C 8A0E0200 MOV CL,[0002] DS:0002=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0010 NV UP EI PL NZ NA PO NC
13F9:0010 8A160300 MOV DL,[0003] DS:0003=00
-t
AX=2000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=2000 ES=13F9 SS=13F9 CS=13F9 IP=0014 NV UP EI PL NZ NA PO NC
13F9:0014 5D POP BP
-
前面讲到循环结果,对一个内存ffff:0-ffff:b之间内存中的所有数据进行相加,一般来说不会超过一个AX代表的16进制的值,但是能否将每段内存中存入AX中进行累加呢?显然不行,AX是16位,内存是8位,类型不匹配。但是用AL也不行,会带来进位错误。但是两个16进制的寄存器相加是可以的。
D:\Temp\NPP593~1.BIN\tmp>debug 6.exe
-r
AX=0000 BX=0000 CX=001C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=148B ES=148B SS=149B CS=149B IP=0000 NV UP EI PL NZ NA PO NC
149B:0000 B8FF0F MOV AX,0FFF
-u
149B:0000 B8FF0F MOV AX,0FFF
149B:0003 8ED8 MOV DS,AX
149B:0005 BB0000 MOV BX,0000
149B:0008 BA0000 MOV DX,0000
149B:000B B90C00 MOV CX,000C
149B:000E 8A07 MOV AL,[BX]
149B:0010 B400 MOV AH,00
149B:0012 03D0 ADD DX,AX
149B:0014 43 INC BX
149B:0015 E2F7 LOOP 000E
149B:0017 B8004C MOV AX,4C00
149B:001A CD21 INT 21
149B:001C 007437 ADD [SI+37],DH
149B:001F A15827 MOV AX,[2758]
-t
AX=0FFF BX=0000 CX=001C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=148B ES=148B SS=149B CS=149B IP=0003 NV UP EI PL NZ NA PO NC
149B:0003 8ED8 MOV DS,AX
-t
AX=0FFF BX=0000 CX=001C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0005 NV UP EI PL NZ NA PO NC
149B:0005 BB0000 MOV BX,0000
-t
AX=0FFF BX=0000 CX=001C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0008 NV UP EI PL NZ NA PO NC
149B:0008 BA0000 MOV DX,0000
-t
AX=0FFF BX=0000 CX=001C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=000B NV UP EI PL NZ NA PO NC
149B:000B B90C00 MOV CX,000C
-t
AX=0FFF BX=0000 CX=000C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=000E NV UP EI PL NZ NA PO NC
149B:000E 8A07 MOV AL,[BX] DS:0000=A1
-t
AX=0FA1 BX=0000 CX=000C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0010 NV UP EI PL NZ NA PO NC
149B:0010 B400 MOV AH,00
-t
AX=00A1 BX=0000 CX=000C DX=0000 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0012 NV UP EI PL NZ NA PO NC
149B:0012 03D0 ADD DX,AX
-t
AX=00A1 BX=0000 CX=000C DX=00A1 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0014 NV UP EI PL NZ NA PO NC
149B:0014 43 INC BX
-t
AX=00A1 BX=0001 CX=000C DX=00A1 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0015 NV UP EI PL NZ NA PO NC
149B:0015 E2F7 LOOP 000E
-p
AX=001D BX=000C CX=0000 DX=0576 SP=0000 BP=0000 SI=0000 DI=0000
DS=0FFF ES=148B SS=149B CS=149B IP=0017 NV UP EI PL NZ NA PE NC
149B:0017 B8004C MOV AX,4C00
-
前面通过MASM与DEBUG对汇编程序处理的比较,我们得出两者的一些差异点,其中有一点特别重要,就是用ds:[0]来表示内存地址,这一点扩展开来,不仅仅DS可以用在这里,CS,ES,SS都可以用在这里,mov ax,ss:[BX]就是将一个内存单元的内容送入ax中,单元长度2个字,偏移地址存放在BX中,段地址存放在SS中。
另外操作系统对内存空间作了用途划分,当在实模式下进行汇编操作时,很容易将一些非法数据写入操作系统禁止写入的内存空间,这些空间是不允许写入其它内容的,不过所有的操作系统对0:200~2ff这一段空间是没有使用的。因些我们开发时可以放以大胆的使用这一段内容。使用段前缀可以对代码进行优化。如下所示: