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 .

 

 

转载于:https://www.cnblogs.com/Jekhn/archive/2010/12/30/1922145.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值