我正在研究一些涡轮组装机,这是我在建筑课上学到的。我正在解决的问题是使用
extern
和 include
关键字从一个 asm 文件访问函数/过程并在另一个 asm 文件中使用它。我包含了下面的各个文件,并运行了以下命令:
tasm file1.asm
tasm file2.asm
tlink file1.obj+tlink2.obj
tlink
之后,我收到有关 MyProcedure
是 file2.asm 中未定义符号的错误。文件1.asm:
.MODEL SMALL
.DATA
; Data declarations go here
.CODE
; Code segment
; Define the procedure
MyProcedure PROC
; Procedure code goes here
; For example:
MOV AH, 9 ; DOS interrupt to print string
LEA DX, Msg ; Load the address of the message
INT 21h ; Call DOS interrupt
RET ; Return from the procedure
MyProcedure ENDP
; Data section
Msg DB "Hello, World!$"
END
file1.inc:
; Declare external procedure MyProcedure
EXTRN MyProcedure:NEAR
文件2.asm:
.MODEL SMALL
.DATA
; Data declarations go here
.CODE
; Code segment
; Include external file
INCLUDE file1.inc
; Main program
START:
; Call the procedure defined in file1.inc
CALL MyProcedure
; Other code goes here
; End of main program
END START
我能够弄清楚我在做什么。该答案被认为使用 16 位 TASM Borlands 5.0,因此请记住这一点。我将包含两个不同的答案,请记住相应地使用。
第一种方法:使用外部 ASM 文件的引用 如果您想通过特定过程引用外部文件,这里有一个带有说明的示例
示例: file1.asm(主文件)
; Simple program that will use an external call to file external.asm
; The procedure will call a system interrupt
;External procedure that will execute the system interrupt in the program
;Declare at the top here to let the assembler know that this will be referenced outside this file when creating the assembler object
extern myPrcoedure:PROC
.model small
.stack 100h
.data
; Data declaration goes here
.code
; Code declaration goes here
_start:
CALL myProcedure ; calls the myProcedure that will execute the system interrupt
end _start
第一个文件是使用外部调用过程 myProcedure 的主文件。似乎最佳实践是将
命令放在顶部,以便汇编器知道extern
file2.asm(外部引用文件)
; Program that contains the system interrupt command procedure called myProcedure
; Will be externally referenced in main.asm
;
.MODEL small
.CODE
; This public declaration works like an interface and lets the assembler know that this procedure
; Can be externally referenced and executed by the files that are tlinked with this program respectively
PUBLIC myProcedure
myProcedure PROC NEAR ; Procedure declaration with procedure code for declaration
; Procedure code here
MOV AH, 4Ch
INT 21h
RET
myProcedure ENDP
END
此文件是将包含定义的过程 myProcedure 的外部文件。
关键字只是让程序知道要在段内跳转。在此示例中,当在near
中调用时,它将保留在 Start 段内。如果您发现程序开始变大,您可能需要切换到main.asm
并尝试使用该关键字。far
使用说明:
tasm file1.asm
tasm file2.asm
tlink file1.obj file2.obj
main.exe (or whatever executable that is spit out)
第 1 部分结束
第 2 部分开始
第二种方式:使用 .inc 文件来保存将在运行时包含在主 ASM 文件中的过程。
示例: file1.asm(主文件)
;This program is example on how to use include keyword in TASM 16 bit program to refenrce an external procedure, struct, obj, macro, etc
;All it does is call a system interrupt from the respective .inc file. In this case it is main2.inc
.MODEL small
.STACK 100h
.DATA
; Data section here
.CODE
Include file1.inc ; Contains the procedure I will reference
Start:
Call myProcedure ; Call the desired procedure.
END Start
此主文件仅具有放置在
段中的包含语句,现在汇编器知道要包含您可以在下面在.Code
段中看到的文件,我将在其中调用从Start
file1.inc
引用的过程
file1.inc(包含该过程的要包含的文件)
;.inc file that will contain the system interrupt procedure
PUBLIC myProcedure ; interface declaration that allows assembler to know that the procedure can be used publicly in the respective file
myProcedure PROC NEAR
; Procedure code here
MOV AH, 4Ch
INT 21h
RET
myProcedure ENDP
您可以看到 .inc 的工作方式与 c 或 cpp 中的 hoarder 文件相同。您只需将需要引用的任何内容放在那里,以便可以随时在任何文件中调用它,只要要引用的文件将其包含在
语句中即可。在这种情况下,只要 file1.asm 具有include
,我们就可以使用过程 myProcedureinclude file1.inc
使用说明:
tasm file1.asm
tlink file1.obj
main.exe (or whatever executable that is spit out)
考虑到我们不需要 tasm 或 tlink 第二个文件,您可以看到第二组指令略有不同。您可以直接运行该程序。
第 2 部分结束
我希望这对踏上汇编之旅的其他人有所帮助。请评论和提问。我会尽力回来或者我希望有人能够提供帮助。