汇编语言多文件程序设计
2011-11-7 晴 于韶关仁化
变量的共享
关键字externdef跟C语言中的extern相似。在定义与引用模块(源文件)中的声明的一致的。
声明格式:
EXTERNDEF [[langtype]] name:type [[, [[langtype]] name:type]]...
MSDN Remars:
If name is defined in the module, it is treated as PUBLIC. If name is referenced in the module, it is treated as EXTERN. If name is not referenced, it is ignored. The type can be ABS, which imports name as a constant. Normally used in include files.
函数的共享
函数在调用之前必须使用proto语句提前声明。否则MASM会提示" error A2190:INVOKE requires prototype for procedure"。要在本模块中使用其它模块中的文件时,只需在调用模块作个proto声明就行了。
程序示例:
;-------------------main.asm--------------------------
.386
.model stdcall, flat
option casemap:none
fn proto
externdef string:byte
.data
string db 'hello world!', 0
.code
start:
invoke fn
ret
end start
;-------------------fn.asm--------------------------
.386
.model stdcall, flat
option casemap:none
include msvcrt.inc
includelib msvcrt.lib
externdef string:byte
.code
fn proc
invoke crt_printf, offset string
ret
fn endp
end
#------------------makefile------------
main.exe:main.obj fn.obj
link /subsystem:console /nologo main.obj fn.obj
.asm.obj:
ml /c /coff /nologo {1}lt;
clean:
del *.obj
====================================================
dll模块中的数据调用:
include 保证编译时通过
includelib 保证链接时通过
dll文件 保证运行时正常