It has been very quiet here lately, so I posted a little exercise for you.
Here is the question: What are those 6 ShowMessages displaying?
No testing with Delphi allowed.
Code:
function Get1(var X): Integer;
var
L: Integer absolute X;
begin
Result := PInteger(L-4)^;
end;
function Get2(var X): Integer;
asm
mov EAX, [EAX-4]
end;
procedure TForm1.Button4Click(Sender: TObject);
type
PIntArray = ^TIntArray;
TIntArray = array[0..MaxInt div SizeOf(Integer) - 1] of Integer;
var
pInt: ^Integer;
PTest: array of Integer;
begin
SetLength(PTest, 5);
PTest[0] := 57;PTest[1] := 1;PTest[2] := 2;PTest[3] := 3;PTest[4] := 4;
pInt := @PTest[0];
Inc(pInt, 3);
ShowMessage(IntToStr(pInt^)); // 1
Dec(pInt, 4);
ShowMessage(IntToStr(pInt^)); // 2
ShowMessage(IntToStr(Get2(PTest[0]))); // 3
ShowMessage(IntToStr(Get1((@PTest)^))); // 4
pInt := @PTest;
pInt := Pointer(pInt^);
ShowMessage(IntToStr(pInt^)); // 5
ShowMessage(IntToStr(PIntArray(pInt)[2])); // 6
end;
Bookmarks