Linking Assembly Language with Delphi Part II

If you have not read Linking Assembly Language with Delphi Part I, it is better to read it now because this article completes it.

Unlike what we did in Part I, where we used a very dated Delphi release, now in the Part II we will only be using the latest edition of Delphi, the XE2, because it has a couple of new features that are relevant to this subject. One of them is 64-bit compilation facilities and the other is the capability to link with COFF object files.

There is no TASM Assembler to compile 64-bit Assembly Language, I don’t think we will have one, but this is not a great loss. A number of assemblers compile to 64-bit object files that Delphi XE2 understand and links seamlessly. Of course MASM can do it, but it lags a bit behind other assemblers in 64-bit compilation. My favorite is JWasm, but this is all a matter of personal preference, I guess.

Many people think otherwise, but 64-bit ASM is indeed easier than 32-bit ASM. One of the things that makes it easier is that there is only one calling convention (i.e, the way a function calls a routine): it is the fast-call (FASTCALL) calling convention, where registers are used to pass the first 4 arguments and the stack the 5th and following parameters, if they exist. There are a number of details to keep in mind but since there is only one calling convention things become really easier. In summary, to use 64-bit Assembly Language with Delphi XE2, all you have to do is compile the ASM with an Assembler and link (for small quantities of ASM you can use BASM as well).

So, download the64-bit Assembly Language source code and compile it from a console window with this command line:

<path>\jwasm -c -win64 -Zp8 asmrotines64.asm

Download and unzip your Delphi project and copy asmrotines64.obj to its folder.

Compile and run as 64-bit application.

Now, we are going to compile an Assembly Language file as COFF. Note that all three 32-bit ASM source files we have used in Part I and here in Part II are almost the same, but not literally the same. Download from here and compile with the following command line:

<path>\jwasm -coff -zt0 asmcoff.asm

The switch -zt0 removes stdcall symbol decoration. Since MASM does not have this facility, we recommend to stick with JWasm, otherwise the obj file will require postprocesssing.

We are almost done, if you have not downloaded yet the Delphi XE2 project do it now.

Note the {$defines} at the top of the Delphi unit. They allow you to build 3 different 32-bit versions, 2 of them using the compiled ASM obj files from Part I. To use the 32-bit COFF from this Part II, keep undefined USINGTASM and define USINGMASMCOFF. This are actually the default settings when you download.

All right, build and run the COFF and the other configurations. This all about it!

Leave a Reply

Your email address will not be published. Required fields are marked *