Unusual behavior in delphi assembly block

I come across some weird behavior with a built-in Delphi build, as shown in this very short and simple program:

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. 

It’s just for an example ( mov ing [asdf] in eax not much, but works for an example). If you look at the assembly for this program, you will see that

 mov eax, [asdf] 

turned into

 mov eax, ds:[4] 

(as presented by OllyDbg), which is obviously falling. However, if you do this:

 var temp: TAsdf; begin temp := asdf; asm int 3; mov eax, [temp]; end; 

It changes to mov eax, [ebp-4] which works. Why is this? I usually work with C ++, and I'm used to using such instance instances, it is possible that I am using instance variables incorrectly.

EDITOR: Yes, that’s all. Changing mov eax, [asdf] to mov eax, [Self.asdf] fixes the problem. Sorry.

+6
assembly delphi instance-variables basm
source share
2 answers

The method gets a Self pointer in the EAX register. You must use this value as the base value to access the object. So your code will look something like this:

 mov ebx, TBlah[eax].asdf 

See http://www.delphi3000.com/articles/article_3770.asp for an example.

+10
source share

In the first case, mov eax, [asdf], the assembler will search for asdf and find that this is an offset field of 4 in the instance. Since you used the indirect addressing mode without a base address, it will only encode the offset (this is similar to 0 + asdf in assembler). If you wrote it like this: mov eax, [eax] .asdf, it would be encoded as mov eax, [eax + 4]. (here eax contains "I" as passed from the caller).

In the second case, the assembler will look for Temp and see that it is a local variable indexed by EBP. Since he knows the base address register to use, he may decide to encode it as [EBP-4].

+12
source share

All Articles