Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access delphi open array parameter in inline assembly

Suppose I have a delphi function like this:

procedure sortArray(arr: array of DWORD); register;
asm
  //access array here
end;

How would I access a specific element of the array in inline-assembly? I already know that arr actually consists of 2 parameters: a pointer to the array and its High(), but I need to know exactly how it works. I assume the pointer will be in eax and the High-value in ebx, but I'm not quite sure.

procedure sortArray(arr: array of DWORD); register;
asm
  mov DWORD PTR [eax+$4], $09 //set the second element of arr to 9 ???
end;

btw. if anyone wonders: I'm doing this in assembly because

a) I want to enhance my asm-skills

b) I have to do this for school and want to make it a little less boring

like image 709
Grisu47 Avatar asked Mar 18 '23 07:03

Grisu47


1 Answers

The first thing to do is to stop passing arrays by value. For large arrays this will be inefficient. Instead of pass by value, declare the parameter to be const.

However, since your function is named sortArray, and since your code attempts to modify the array, it would appear more likely that you need a var parameter to get the desired semantics.

procedure sortArray(var arr: array of DWORD);

The ABI for open arrays is documented in the Program Control topic of the language guide. It states that:

An open-array parameter is passed as two 32-bit values. The first value is a pointer to the array data, and the second value is one less than the number of elements in the array.

So, your function is effectively the same as:

procedure sortArray(ArrPtr: PDWORD; ArrHigh: Integer);

From there, you merely need to understand the calling convention, again documented in the Program Control topic of the language guide:

The first three parameters that qualify are passed in the EAX, EDX, and ECX registers, in that order.

So, ArrPtr is passed in EAX, and ArrHigh is passed in EDX.

like image 72
David Heffernan Avatar answered Apr 02 '23 17:04

David Heffernan