Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi + Assembly array access

I am having a problem to access an element of an array in assembly(delphi).

The code is:

procedure TMaskBit.AllocBuffer;
begin
     SetLength(DataIn, 6);  //array of integer
     DataIn[0] := 1 ;
     DataIn[1] := 2 ;
     DataIn[2] := 3 ;
     DataIn[3] := 4 ;
     DataIn[4] :=5 ;
     DataIn[5] := 6 ;
end;

procedure TMaskBit.SetValue();
asm
   lea edx, [eax].TMaskBit.DataIn     //indice
   mov ecx, [edx+8]                  //second ement
   mov [EAX].TMaskBit.Z, ecx
end;

What might be wrong?

Thanks!

like image 968
p.magalhaes Avatar asked Jun 20 '11 05:06

p.magalhaes


1 Answers

Dynamic Array is a pointer, so you should use mov instead of lea:

type
  TIntArray = array of Integer;

  TMaskBit = class
    Z: Integer;
    DataIn: TIntArray;
    procedure AllocBuffer;
    procedure SetValue();
  end;

procedure TMaskBit.AllocBuffer;
begin
     SetLength(DataIn, 6);  //array of integer
     DataIn[0] := 1 ;
     DataIn[1] := 2 ;
     DataIn[2] := 3 ;
     DataIn[3] := 4 ;
     DataIn[4] :=5 ;
     DataIn[5] := 6 ;
end;

procedure TMaskBit.SetValue();
asm
   mov edx, [eax].TMaskBit.DataIn     // edx references DataIn[0] !!!
   mov ecx, [edx+8]                   // DataIn[2]
   mov [EAX].TMaskBit.Z, ecx
end;

procedure TForm7.Button3Click(Sender: TObject);
var
  MB: TMaskBit;

begin
  MB:= TMaskBit.Create;
  MB.AllocBuffer;
  MB.SetValue;
  ShowMessage(IntToStr(MB.Z));
end;
like image 59
kludg Avatar answered Sep 28 '22 15:09

kludg