.Trust our experience in High-Power Network Tools and Communication Tools.

Linking Assembly Language with Delphi Part I

Some editions of Embarcadero (Borland, Codegear and Inprise as well) products,  even some free editions, come with an Assembler called TASM (actually it is TASM32.EXE because we will be using the 32-bit variant, but developers usually refer to it by TASM, the product name). Although some people have been spreading around that TASM is dead, that is not really true, it is not updated often but TASM is still the best hassle free 32-bit Assembly Language compiler to work with 32-bit Delphi.

Those that are used to MASM (Microsoft Macro Assembler) may be surprised to know that TASM is very compatible with MASM, when you are not using the so-called “ideal” mode (and I am one of those that never use the “ideal” mode). To see how true this is, just compare the ASM sources for both in the included samples for this article.

In this article I will show how to link Delphi with compiled Assembly Language. I will use both TASM and MASM to compile the ASM. With MASM the procedure is not guaranteed to work in every case, but usually does.

I will be using Delphi 5 to stress how back in time we can go, but it will not make any difference if you use a recent version of Delphi. Just note that Delphi XE2 and above offer alternative options and I will be dealing with those in another article (actually, I already uncovered a bit this matter in the article “How to use Assembly Language with Delphi 32-bit and 64-bit” but I want to dig deeper).

The Assembly Language module we will be compiling consists of 3 routines. One handles a floating-point calculation, the other 2 are related to the XXTEA encryption algorithm (http://en.wikipedia.org/wiki/XXTEA), which we have already used in other articles (Mixing .Net and Assembly Language in a standalone 64-bit exe and Mixing .Net and Assembly Language in a standalone 32-bit exe). We will be able to verify that our algorithm is not buggy by testing against standard test vectors and you can also encrypt/decrypt hex blocks or files.

Lets start then:

TASM (release 5.0 or later)

Download the Tasm Assembly Language source code and compile it from a console window with this command line:

<path>\tasm32 asmtasm.asm

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

In the top of the DephiAndTasm unit make sure you have {$define USINGTASM}

Compile and run.

MASM (release 8.0 or later, release 8.0 is free from Microsoft)

Download the MASM Assembly Language source code and compile it from a console window with this command line:

ml /c /omf asmmasm.asm

Note: You can also use the clone JWasm to compile this source with: JWasm -omf asmmas.asm

Now, you need to post-process the obtained OMF obj file because it is not compatible with the OMF used by the Delphi compiler. The utility that works for that is OMF2D from EliCZ. I don’t know much about ElicCz, but its utility works well for this, does not contain any virus and is free. The command line is:

<path>\omf2d asmmasm.obj asmmasm_d.obj

Now, if you have not yet downloaded and unzipped the Delphi project, you can do it now. Copy the asmmasm_d.obj to its folder.

In the top of the DephiAndTasm unit make sure you DON’T have {$define USINGTASM}, because this applies to the TASM case.

Compile and run.

As easy as this!