Netwide Assembler meets Delphi - Getting Started
This site hosted by Free.ProHosting.com
Google

Getting started

There are at least two possibilities how to use your fast Assembly Code in your Delphi Applications. You may compile your code as an "Object file" or you simply write a Dynamic Link Library.

Object Files

It seems that Borland has not worked on external code linking for a long time so this one is a bit tricky. We have to take the following steps to get our code successfully linked:

  1. Make your labels global.
  2. Put every function (global label) in its own segment.
  3. Compile the Assembly Code.
  4. Declare your Delphi procs external.

Step 1 - Globalization

The NASM Macro "global" tells the compiler to write the Labels name into the Object File.

global sillyproc
sillyproc:
nop
ret

Step 2 - Segmentation

Now you place every function in one section (or segment just what you prefer). Here are a lot of traps you can run into. The OMF Format supports both 16 and 32 bit so each and every Segment must be specified as being 32bit. This is why there is the "use32" statement in the section declaration.
The section name was not chosen per accident, here's the point where the Borland Naming Conventions kick in. Just a few important notes on that:

  • Procedures and functions must be in a segment whose name is "CSEG", or something ending in "_TEXT" or "CODE".
  • Initialised data must be in a segment whose name is either "CONST" or something ending in "_DATA".
  • Uninitialised data must be in a segment whose name is either "DATA", "DSEG", or something ending in "_BSS".
[section cseg use32]
global sillyproc
sillyproc:
nop
ret

Step 3 - Compilation

Use the NASM Command Line Compiler v0.98 or any later version to compile your files. The complete Commandline looks like that:

nasm -f obj silly.asm -o C:\Delphi\Projects\AsmTest\Demo.obj

Step 4 - Yet another stupid headline

Nothing really exciting right here but don't forget that your symbols are case sensitive.

{$LINK Demo.obj}
procedure sillyproc; external;

Note that you do not need leading underscores.

Final words

This is my very first Tutorial. So I would appreciate if you drop me a mail (whiskex@gmx.net) with your comments, questions and suggestions.

wkx

« Assembly Page Next Issue »