初学 Delphi 嵌入汇编[6] - & 操作符


//在汇编中访问 Delphi 的变量可以使用 & 操作符

procedure Proc(str1,str2: string);

var

  s1,s2: string;

begin

  asm

    mov ecx, &str1  {}

    mov edx, &str2



    mov &s1, ecx   {}

    mov &s2, edx

  end;



  ShowMessage(s1 + s2);

end;

{在没有歧义的情况下, 操作符 & 是可以省略的, 譬如上面的例子就可以省略}



//测试

procedure TForm1.Button1Click(Sender: TObject);

begin

  Proc('我是', '万一'); {显示: 我是万一}

end;


 
   
//在什么情况下不能省略呢? 例如: procedure TForm1.Button2Click(Sender: TObject); var ecx: Integer; {这个变量和其中一个寄存器重名了} begin ecx := 99; asm mov ecx, &ecx {现在 ecx 是寄存器; &ecx 是变量} add ecx, 1 mov &ecx, ecx end; ShowMessage(IntToStr(ecx)); {100} end;
{现在也得知: 其实这之前的例子, 只要是在汇编中使用的本地变量都可以加 & }

你可能感兴趣的:(Delphi)