delphi内联汇编写的pos函数

点击下载演示工程

unit Unit1;



interface



uses

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

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;



var

  Form1: TForm1;

  function CharPos(src:char; str:pchar):Integer;

  

implementation



{$R *.dfm}



procedure TForm1.Button1Click(Sender: TObject);

var

  i:Integer;

begin

    i:=CharPos('w', 'hello word');

    ShowMessage(IntToStr(i));

end;



function CharPos(src:char; str:pchar):Integer;

var

    n:Integer;

Label

    Find,NotFind;

begin

    n:=Length(str);

    {方向位清0,与之相反的是std(方向位置1),cld的作用是当每执行一次串操作指令后使得edi加1,std则减1}

    {下列的串操作指令是scasb,前缀repnz表示当当前edi指向的值不等于al中的值时重复执行此条指令}

    {如果使用std方向,则需要把edi指向串的末尾,即:str:=str+n+1; mov edi,str}

    {scasb:串扫描指令,用AL寄存器中的内容与由ES段DI指定的一个字节数据进行比较(减),

     若相等(结果为0)}

    asm

        cld;

        mov edi, str;

        mov al,src;

        mov ecx,n;

        repnz scasb;

        jz Find;

        mov result,-1;

        jmp NotFind;

Find:

        mov eax,n;

        sub eax,ecx;

        mov result,eax;

NotFind:

    end;

end;



end.



你可能感兴趣的:(Delphi)