4

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

2
  • 4
    You can determine this by yourself when you'll be working with the CPU debug window. Commented Oct 6, 2014 at 14:11
  • 1
    I'm not a big fan of trial and error learning. Reading the documentation should be the first step. Why resort to reverse engineering? Commented Oct 6, 2014 at 22:28

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, but are you sure that ArrHigh is actually a delphi Integer? Since the number of elements can never be negative, I think it would rather be an unsigned 32bit int, thus a DWORD/CARDINAL. The documentation doesn't say whether it's signed or not. Also could you tell my if this line is right? mov DWORD PTR [eax+$4], $09 //set the second element of arr to 9 ???
@Cody227 The number of elements can never be negative. That is true. But ArrHigh is not the number of elements. It is the index of the last element. So, High(arr) = Length(arr)-1. And when there are no elements, HighArr is -1. So yes, the second parameter is signed.
Yes, mov DWORD PTR [eax+$4], $09 will assign $9 to the second element. Of course, you'd need to check that there actually was a second element in real code.
Of course, one needs to realize that array of X, if found in a parameter list, can only be an open array parameter. I wrote an atricle about this (shamelessly plugging): Open array parameters and array of const.

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.