Delphi子过程参数传递
默认调用方式Register,传递参数的顺序前三个为al,dl,cl / ax,dx,cx / eax,edx,ecx分别对应一个字节,两个字节,四个字节,当参数>3个时,多出的参数入栈,所以第四个参数的地址为[esp+8],第五个参数的地址为[esp+12],[esp+4]至[esp+0]这四个字是函数的返回地址。以上针对类以外的子过程,在类里面定义的过程参数传递有所改变,主要改变为eax保存了类的首地址即:self。
代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function _AddFunc(pa,pb,pc,pd,pe:Integer):Integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1._AddFunc(pa, pb, pc, pd, pe: Integer): Integer;
asm
xor eax,eax
add eax,edx
add eax,ecx
add eax,[esp+8]
add eax,[esp+12]
add eax,[esp+16]
end;
procedure TForm1.Button1Click(Sender: TObject);
var
iSum: Integer;
begin
iSum := _AddFunc(10,20,30,40,50);
ShowMessageFmt('Integer Sum:%d',[iSum]);
end;
function addFunc(pa,pb,pc,pd: Byte):Byte;overload;assembler;
asm
add al,dl
add al,cl
add al,[esp+8]
end;
function addFunc(pa,pb,pc,pd: Word):Word;overload;assembler;
asm
add ax,dx
add ax,cx
add ax,[esp+8]
end;
function addFunc(pa,pb,pc,pd: Integer):Integer;overload;assembler;
asm
add eax,edx
add eax,ecx
add eax,[esp+8]
end;
function addFunc(pa,pb,pc,pd,pe: Integer):Integer;overload;assembler;
asm
add eax,edx
add eax,ecx
add eax,[esp+8]
add eax,[esp+12]
end;
procedure TForm1.Button2Click(Sender: TObject);
var
bSum: Byte;
wSum: Word;
iSum: Integer;
begin
bSum := addFunc(1,2,3,4);
wSum := addFunc(3,4,5,6);
iSum := addFunc(10,20,30,40,50);
ShowMessageFmt('Byte Sum:%d',[bSum]);
ShowMessageFmt('Word Sum:%d',[wSum]);
ShowMessageFmt('Integer Sum:%d',[iSum]);
end;
function addFuncNew(pa,pb,pc,pd,pe: Integer):Integer;assembler;
asm
add eax,edx
add eax,ecx
add eax,[esp+8]
add eax,[esp+12]
end;
procedure TForm1.Button3Click(Sender: TObject);
asm
push ebp
mov ebp,esp
add esp,-16
mov eax,10
mov edx,20
mov ecx,30
push 40 //esp := esp + 4
push 50 //esp := esp + 4
call addFuncNew
lea edx,[esp+4] //4,8 ok, 12:error
call system.IntToStr
mov eax,[esp+4]
call showMessage
add esp,16
mov esp,ebp
pop ebp
end;
end.