7

I am using Delphi 2010. Is it possible to tell Delphi to not generate a prologue for a function? I'm writing some pure assembly functions like this:

procedure SomeAssembly; stdcall;
begin
    asm
        ...
    end;
end;

and I would like to tell Delphi not to generate a prologue and epilogue for this function, like C++'s __declspec(naked) feature.

And so no one wastes their time, I don't need help getting these functions to work with the prologue; I can already do that. It's just a large inconvenience and will make maintenance an huge hassle. I'll have to manually inspect the prologues generated by the compiler to see their length, and if that changes, my program will crash.

I also know I can write the function as a series of bytes in a byte array, but that would be even worse than having to go find the length of Delphi's prologue.

PhiS
  • 4,540
  • 25
  • 35
K. Charles
  • 73
  • 3

3 Answers3

20

Delphi doesn't generate prologues or epilogues for functions having no arguments and declared with the register calling convention. If you want functions without prologues, declare them as zero-argument, register-calling-convention functions. Also, skip the begin-end block and go straight into assembly.

procedure SomeAssembly; // register; (implied)
asm
  // ...
end;

Since you're effectively lying about the nature of the functions, calling them may be tricky. If you've implemented a function as though it received parameters and used a different calling convention, then you'll have to make sure the compiler knows about that at the call site. To do that, declare a function pointer that reflects the "real" type of your function instead of the declared type. For example, if your function is really a two-argument stdcall function, declare something like this:

type
  TSomeAssemblyFunc = function (Arg1: Integer; Arg2: PAnsiChar): Boolean; stdcall;
var
  SomeAssemblyProc: TSomeAssemblyProc;

Now, assign that variable so it points at your function:

SomeAssemblyProc := TSomeAssemblyProc(@SomeAssembly);
if SomeAssembly(2, 'foo') then ...

In addition to skipping the prologue and epilogue, the compiler will generate the incorrect RET instruction for this function (because of the different calling convention), so you'll have to make sure you say ret 8 in your code instead of letting the compiler's default ret instruction occur.


Finding the length of Delphi's prologue is trivial, if you have a working debugger:

  1. Set a breakpoint at the start of the function.
  2. Call the function.
  3. When the debugger stops at the breakpoint, switch to the CPU view.
  4. Look at the instructions that make up the prologue.
  5. Count the bytes displayed beside those instructions.
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • Why can't everyone on SO answer like this? Extremely helpful, thanks Rob. – K. Charles Mar 27 '11 at 17:41
  • @K. Charles: Different level of expertise? That was a wild guess, though, no offence intended to anyone. – Andriy M Mar 27 '11 at 17:53
  • Isn't there an extra level of indirection when you have a function pointer? Personally I'd have thought tasm would be more suitable. – David Heffernan Mar 27 '11 at 18:08
  • Relying on a function pointer to skip the Delphi-generated prologue does sound a bit hacky and not very future-proof. – Cosmin Prund Mar 27 '11 at 18:56
  • @Cosmin, we're not relying on a function pointer to skip the prologue. The prologue doesn't even exist anymore due to the way the function was defined (which was the point of this exercise). – Rob Kennedy Mar 27 '11 at 20:39
  • Yes, @David, it's an extra level of indirection. It's no worse than importing a function from a DLL, though. (In fact, that's where I learned this technique. I was [patching the import table of an EXE](http://www.cs.wisc.edu/~rkennedy/mslu) to point to new assembler functions. The functions were called as stdcall, but if I defined them that way, then they had compiler-provided prologues that I then needed to "undo" before getting to the real work.) – Rob Kennedy Mar 27 '11 at 20:43
  • This is not entirely correct. Also functions (asm..end;-only) with register calling convention, no local variables, and no arguments _passed via the stack_ have no prologue. This means up to 3 (u)int32 arguments are still fine. E.g. the following still has no prologue: function Test1 (a,b,c:longword):longword;asm nop end; – PhiS Jun 20 '11 at 06:34
1

According to the this embarcadero docwiki you can skip the surrounding begin and end and the compiler will skip some of it's stuff. But if you really want pure assembler, why not put your function into a separate assembler file, assemble it with tasm (the exe is named tasm32) and link to it. You'll then use the assembler directive in the delphi code.

Cosmin Prund
  • 25,498
  • 2
  • 60
  • 104
  • @downvoters, please explain. TASM is part of Delphi and it is the natural way of working with 100% pure assembler. Was this strategic voting? – Cosmin Prund Mar 28 '11 at 04:43
0

Doesn't

procedure SomeAssembly; stdcall;
asm
    ...
end;

do the trick?

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
  • If I'm remembering correctly, `stdcall` causes the compiler to include a prologue and epilogue for saving and restoring the frame pointer, even if there are no parameters. – Rob Kennedy Mar 27 '11 at 20:36