Search code examples
c#assemblyunmanagedfreepascalcalling-convention

Dereference a C# by ref pointer in an asm DLL


I have the following, which works perfectly:

procedure ShuffleAry16(var Ary16: TByteAry; MaskLow, MaskHigh: Int64); cdecl; assembler;
asm
  movdqu        xmm0, [rcx]             // unaligned load Ary16
  movq          xmm1, rdx
  pinsrq        xmm1, r8, 1
  pshufb        xmm0, xmm1
  movdqu        qword ptr [rcx], xmm0   // unaligned save Ary16
end; 

C#:

public static unsafe extern void ShuffleAry16(byte* ary, ulong maskLow, ulong maskHigh);

I have another case where I need to pass the byte pointer by reference. Let's keep the current method as an example:

public static unsafe extern void ShuffleAry16(ref byte* ary, ulong maskLow, ulong maskHigh);

I can't figure out how to correctly get the address of ary (RCX) in this case. I tried lea, but it didn't work:

  lea           rax, [rcx]
  movdqu        qword ptr [rax], xmm0

How do I do this?


Solution

  • I don't think that you want lea here, from my understanding you should just use mov as ref byte * is marshaled the same as byte**.

    Should be something like:

    mov rax,qword ptr [rcx]
    

    For reference I searched what was exactly lea and while it's made to be used when referencing things in array it doesn't really access anything just compute adresses. This stackoverflow answer was useful to my understanding : What's the purpose of the LEA instruction?