Delphi内联汇编写的字符串复制函数

点击下载演示工程

unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Label1: TLabel;

    Label2: TLabel;

    Edit1: TEdit;

    Edit2: TEdit;

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;

  procedure CopyString(src:PChar; dst:PChar);

implementation

{$R *.dfm}



procedure CopyString(src:PChar; dst:PChar);

var

  n:Integer;

begin

      n:=Length(src);

      asm

        cld           {方向位清0}        

        mov esi,src;  {把源串的地址放到源串寄存器中}

        mov edi,dst;  {把目标串的地址放到目标串寄存器中}

        mov ecx,n;    

        rep movsb;    {movsb只是把ds:esi处的一个字节复制到es:edi,前缀rep是

                       重复执行exc次movsb,每执行一次movsb,esi、edi都会加1}

      end;

end;



procedure TForm1.Button1Click(Sender: TObject);

var

    dst:Array[0..6] of char;

begin

    CopyString(PChar(Edit1.Text),dst);

    Edit2.Text:=dst;

end;



end.





                            

你可能感兴趣的:(Delphi)