我正在使用 Assembly 在 Delphi 中编写一些函数。所以我想把它放在一个名为 Strings.pas
的 .pas 文件中。在 uses
中使用新的 Delphi 软件。我需要编写什么才能使其成为一个有效的库?
我的功能是这样的:
function Strlen(texto : string) : integer;
begin
asm
mov esi, texto
xor ecx,ecx
cld
@here:
inc ecx
lodsb
cmp al,0
jne @here
dec ecx
mov Result,ecx
end;
end;
计算字符串中的字符数。如何在库 Strings.pas
中使用 uses Strings;
在我的表单中调用?
最佳答案
.pas
文件是一个单元,而不是一个库。一个.pas
文件需要有unit
、interface
、implementation
语句,eg:
字符串.pas:
unit Strings;
interface
function Strlen(texto : string) : integer;
implementation
function Strlen(texto : string) : integer;
asm
// your assembly code...
// See Note below...
end;
end.
然后您可以将.pas
文件添加到您的其他项目,并根据需要使用
Strings
单元。它将被直接编译到每个可执行文件中。您不需要从中创建一个单独的库。但如果你想,你可以。创建一个单独的库 (DLL) 或包 (BPL) 项目,将您的 .pas 文件添加到其中,并将其编译成一个可执行文件,然后您可以在其他项目中引用该文件。
在 DLL 库的情况下,您将无法直接使用
Strings
单元。您将不得不从库中导出
您的函数(string
不是一种安全的数据类型,无法在模块之间传递 DLL 边界),例如:
Mylib.dpr:
library Mylib;
uses
Strings;
exports
Strings.Strlen;
begin
end.
然后您可以让您的其他项目使用引用 DLL 文件的 external
子句声明函数,例如:
function Strlen(texto : PChar) : integer; external 'Mylib.dll';
在这种情况下,您可以创建一个包装器 .pas
文件来声明要导入的函数,将该单元添加到您的其他项目中并根据需要使用
,例如:
字符串库.pas:
unit StringsLib;
interface
function Strlen(texto : PChar) : integer;
implementation
function Strlen; external 'Mylib.dll';
end.
在包的情况下,您可以直接使用
Strings
单元。只需在项目管理器的其他项目的Requires 列表中添加对包的.bpi
的引用,然后根据需要使用
该单元。在这种情况下,string
可以安全传递。
注意:在您显示的汇编代码中,为了使函数不引起访问冲突,您需要保存和恢复ESI
寄存器。请参阅有关 Register saving conventions 的部分在 Delphi 文档中。
关于function - Delphi - 编写带有函数的 .pas 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33812263/