delphi 내장 어셈블리

7712 단어
{
              (MOV),          (ADD),         .

  : ADD AX,BX;      Delphi    AX := AX + BX;

         - Delphi             :
32     : EAX EBX ECX EDX ESP EBP ESI EDI
16     : AX BX CX DX SP BP SI DI
8      : AL BL CL DL AH BH CH DH
16      : CS DS SS ES            : ST

}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//       
function add(x,y: Short): Integer;
var
  count: short;
begin
  asm
    MOV AX,x      {  x        EAX}
    MOV CX,y      {  y        ECX}
    ADD AX,CX    {  EAX + ECX      EAX}
    MOV count,AX  {  EAX       count}
  end;
  Result := count; {   }

{asm            ,    ;           }
end;

//       
function add2(x,y: Integer): Integer;
var
  count: Integer;
begin
  asm
    MOV EAX,x      {  x        EAX}
    MOV EbX,y      {  y        ECX}
    mov Ecx,1000000000
    @s:ADD EAX,EbX    {  EAX + ECX      EAX} {         @   ;       Delphi    }
    loop @s
    MOV count,EAX  {  EAX       count}
  end;
  Result := count; {   }

{asm            ,    ;           }
end;

function add3(x,y:integer):Integer;
var
  i:Integer;
begin
  result:=x;
  for i:=1 to 1000000000 do
    Result:= result+y;
end;

//  
procedure TForm1.btn1Click(Sender: TObject);
var
  i: Integer;
  t:Cardinal;
begin
  //  add     
  //i := add(2,4);
  t:=GetTickCount;
  i:=add3(5,10);
  t:= GetTickCount-t;
  Self.Caption:= IntToStr(t);

  //          ,  delphi              
  t:=0;
  t:=GetTickCount;
  i := add2(5,10);
  t:= GetTickCount-t;
  Self.Caption:=Self.Caption+';'+ IntToStr(t)+';'+inttostr(i);

//  ShowMessage(IntToStr(i)); {6}
end;

end.

좋은 웹페이지 즐겨찾기