我在使用 Delphi 的内联汇编时遇到了一些奇怪的行为,如这个非常简短的程序所示:
program test;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TAsdf = class
public
int: Integer;
end;
TBlah = class
public
asdf: TAsdf;
constructor Create(a: TAsdf);
procedure Test;
end;
constructor TBlah.Create(a: TAsdf);
begin
asdf := a;
end;
procedure TBlah.Test;
begin
asm
mov eax, [asdf]
end;
end;
var
asdf: TAsdf;
blah: TBlah;
begin
asdf := TAsdf.Create;
blah := TBlah.Create(asdf);
blah.Test;
readln;
end.
这只是为了示例(mov
ing [asdf]
intoeax
没有多大作用,但它适用于示例)。如果您查看该程序的程序集,您会看到
mov eax, [asdf]
已经变成
mov eax, ds:[4]
(由 OllyDbg 表示)显然崩溃了。但是,如果您这样做:
var
temp: TAsdf;
begin
temp := asdf;
asm
int 3;
mov eax, [temp];
end;
它更改为 mov eax, [ebp-4] 有效。为什么是这样?我通常使用 C++ 并且习惯于使用这样的实例变量,可能是我使用错误的实例变量。
编辑:是的,就是这样。更改mov eax, [asdf]
以mov eax, [Self.asdf]
解决问题。对于那个很抱歉。