Jump to content
wqmeng

SetLength of TArray<double> in ASM get strange result

Recommended Posts

SetLength of TArray<double> in ASM get strange result.

 

Hello,

 

I have a procedure which try to setlength for 3 TArray<Double>. But seems cause unexcpeted result.

procedure OpArrSSE2(const srcData: TArray<Double>; A1, A2, A3: Integer;
  var dst1, dst2, dst3: TArray<Double>;
  var P1, P2, P3: Double;
  var F1, F2, F3: Double;
  const B1: Boolean);
asm
  ...
  
  // start to setlength of dst1, dst2, dst3.
  {
  mov eax, dst1     // dst1 Pointer
  mov edx, edi
  call System.@SetLength 
  }
  
  // procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: NativeInt; lengthVec: PNativeint);
  mov eax, dst1     // dst1 Pointer
  mov edx, offset TypeInfoArrayDouble   // Type info: Get the type information pointer for TArray<Double>
  mov ecx, 1            // DimCnt (number of dimensions), for a one-dimensional array it's 1
  push edi              // New length (NativeInt), edi = newlength_value
  call System.@DynArraySetLength // procedure DynArraySetLength(var a: Pointer; typeInfo: Pointer; dimCnt: NativeInt; lengthVec: PNativeint);
  add esp, 4            // pop edi
  
  // fill 0
  mov eax, dst1        // dst1 Pointer -> @dst1[0]
  mov eax, [eax]        // @dst1[0]
  mov edx, edi          // Number of elements
  shl edx, 3            // Multiply by SizeOf(Double) (which is 8)
  xor ecx, ecx          // Value to fill (zero)
  call System.@FillChar // Call FillChar function  
  
  // then repeat the same operate on dst2, dst3.
  problem is here, after FillChar dst3.
  the dst2 array become empty, and then some exception will raise up.
  Seems the fillchar of dst3 override some memory data of dst2, and other unknown data which cause AV error.  
end.

Please see the above problem descritption at the bottom of the code.

 

I have tried SetLength at first, but seems it not work, as SetLength is a overload function, I do not know how to call it correctly, then try DynArraySetLength, which seems could setlength correctly, but after fillchar, it shows that the memory which get may has confulic with other data, what's wrong of my codes? Any advice will be appreciated.

 

Thank you.

Share this post


Link to post

The best thing you can do is debug the identical procedure write in Pascal and see how the compiler acts ...

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×