Quote Originally Posted by Useless Hacker
I pasted the code into Delphi... It seems the actual results are:

3
5
5
5
57
2

Well, at least I got one right! ops:
Heh, at least you gave answers. I got most of them but since I didn't post it then nobody would believe me!

Number 1: the pointer is pointed to the first element of the 5-element array. It's then inc'd on 3 places, meaning it goes to ptest[3] instead of ptest[0]. Ptest[3] = 3

Number 2: the pointer is dec'd four positions. This moves it before the first actual element, onto the internal array length int. This you might remember if you ever used Delphi 1 and its strings (my_string[0] there being the length of the array, later replaced with Length and SetLength, I believe, in Delphi 2).

Number 3: the parameter to the func will be passed in the accumulator (EAX). The value [EAX - 4] means "dereference the address 4 earlier (sizeof integer) than the given variable," which would be the value before the first array element. At least, if I remember my asm right. Again, the size of the array. From the help files: "The built-in assembler treats var parameters as a 32-bit pointers."

EDIT: It's also worth mentioning that the results of functions are put into the accumulator (al, ax or eax)...

Number 4: same deally here - the absolute directive (deprecated, IIRC) places the variable at the same spot in memory as the given var -- so it would be the same address as the untyped parameter. And I believe it works much the same as the previous asm routine.

Number 5: I admit that I was initially a little stumped by the fancy footwork here. Here's what I thought happens...

pInt := @PTest;
pInt := Pointer(pInt^);

Dynamic arrays are dynamically allocated, hence they're pointers with fancy compiler support. pInt is pointed to a pointer's (the array's) address-of, then dereferenced (the original array address again) and cast as a pointer. So, it's still pointing to the first element of the array, I believe, when next dereferenced (ptest[0] = 57).

Number 6: not exactly a difficult one here. Just treat the pint pointer as a pointer to an array of integers starting at the same pos as the first element of the dynamic array. So return the value of ptest[2], in other words, which is 2.

Loved these puzzles. If I got any of the above wrong then please let me know! Got any more of these puzzles for us (or maybe I'll try to think up one...)?