public :汇编伪指令,用于说明程序模块中的某个标号是可以被其他程序模块调用的。格式是public 标号
extrn:汇编伪指令,用于说明程序模块中用到的标号是其他程序模块的。格式是extrn 标号:类型,类型有near,far,byte,word,dword等。
这两个伪指令是互相补充的,这些标号是需要在前面先集中申明的。
注意,这里还要明确一个定义,什么是程序模块?程序模块是指一个完整的独立的程序。
这里用一个例子来讲一下,两个程序模块——test:
assume cs:code
public cube——申明cube是其他模块要调用的。
code segment
start: mov ax,3
push ax
mov ax,1
push ax
call cube
mov ax,4c00h
int 21h
cube:
push bp
mov bp,sp
mov ax,[bp+6]
sub ax,[bp+4]
mov bp,ax
mul bp
mul bp
pop bp
ret 4
code ends
end start
test1:
assume cs:code
extrn cube:near——申明cube是调用其他模块的,near是类型。
code segment
start: mov ax,5
push ax
mov ax,2
push ax
call cube
mov ax,4c00h
int 21h
code ends
end start
然后,分别编译,输入编译操作即可:
C:\MASM>masm test;
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.
50254 + 409890 Bytes symbol space free
0 Warning Errors
0 Severe Errors
C:\MASM>masm test1;
Microsoft (R) Macro Assembler Version 5.00
Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved.
50406 + 409738 Bytes symbol space free
0 Warning Errors
0 Severe Errors
连接时,由于test1用到了test中的标号,连接test1时,要加上test,而test的连接不用这么麻烦,直接连接就行了。
C:\MASM>link test1+test;
Microsoft (R) 8086 Object Linker Version 3.05
Copyright (C) Microsoft Corp 1983, 1984, 1985. All rights reserved.
Warning: no stack segment
C:\MASM>link test;
Microsoft (R) 8086 Object Linker Version 3.05
Copyright (C) Microsoft Corp 1983, 1984, 1985. All rights reserved.
Warning: no stack segment
最后生成test.exe和test1.exe,在debug中,我们看一下
C:\MASM>debug test.exe
-u
157C:0000 B80300 MOV AX,0003
157C:0003 50 PUSH AX
157C:0004 B80100 MOV AX,0001
157C:0007 50 PUSH AX
157C:0008 E80500 CALL 0010
157C:000B B8004C MOV AX,4C00
157C:000E CD21 INT 21
157C:0010 55 PUSH BP
157C:0011 8BEC MOV BP,SP
157C:0013 8B4606 MOV AX,[BP+06]
157C:0016 2B4604 SUB AX,[BP+04]
157C:0019 8BE8 MOV BP,AX
157C:001B F7E5 MUL BP
157C:001D F7E5 MUL BP
157C:001F 5D POP BP
C:\MASM>debug test1.exe
-u
157C:0000 B80500 MOV AX,0005
157C:0003 50 PUSH AX
157C:0004 B80200 MOV AX,0002
157C:0007 50 PUSH AX
157C:0008 E81500 CALL 0020
157C:000B B8004C MOV AX,4C00
157C:000E CD21 INT 21
157C:0010 B80300 MOV AX,0003
157C:0013 50 PUSH AX
157C:0014 B80100 MOV AX,0001
157C:0017 50 PUSH AX
157C:0018 E80500 CALL 0020
157C:001B B8004C MOV AX,4C00
157C:001E CD21 INT 21