5

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!

4
  • 2
    How about telling some symptoms? Commented Jun 20, 2011 at 5:34
  • I see trash on Z variable. I expected to see number 2 (DataIn[1]). Commented Jun 20, 2011 at 5:37
  • Are you sure that writing this in assembler is a good idea? Commented Jun 20, 2011 at 8:14
  • It is a just a simple example. I am studying assembler. Commented Jun 21, 2011 at 1:47

1 Answer 1

13

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.