How To: MMX instructions with Delphi 2

Posted: (EET/GMT+2)

 

How To: MMX instructions with Delphi 2

Level: Advanced


This is a updated version 1.01 of the article/code. I've fixed the single MOVD bug, and all instructions should assemble correctly now. Special thanks to Martin Knudsen for pointing out the problem.


As a computer user and software developer, you have probably heard of the new MMX technology by Intel. This new technology promises even 60 percent performance increases. Of course, this requires that the application has been optimized for MMX processors. This "optimization" means that the new, special MMX instructions have to be used.

As a Delphi 2 programmer, you probably know that you can use the inline assembler (with the "asm" block) to use normal 386-compatible Intel architecture (IA) instructions. But as the MMX technology is so new, the Delphi assembler doesn't support these new instructions, like PADDUSW. So you might think: "so I just can't use Delphi to develop MMX software". Wrong! The inline assembler was developed to allow "extensions".

You might wonder how the assembler can be extended. If you have used ordinary standalone assemblers before, you know that every data element you want to have must be defined with the DB (define byte), DW (word) or DD (double word) directive. And the DB directive, fortunately supported by the Delphi inline assembler, is the key to MMX instructions (or any non-supported instruction for that matter.)

Of course, you can't use the Delphi debugger to debug the MMX instructions or watch the registers in the watch window, but at least you can use the new instructions. Coding these instructions by hand requires of course a lot of work, so I've written a very simple MMX code to Delphi "inline" code assembler.

I won't describe here how the assembler works (you have to code for that purpose), but I will tell you how to use it. The assembler itself is very much in a "hack" state, so there is no nice GUI or any decent error reporting. Besides, the assembler doesn't support all MMX instruction combinations. All 56 instructions are supported, but not all addressing modes. See the code comments for information about this.

The assembler takes the file "source.in" as it's input. This file must have only MMX instructions without any comments or stuff like that. Also, every instruction must be on it's own line. For example, your "source.in" might look like this:

    movq mm1,[edx]
    movq mm0,[eax]
    pxor mm0,mm1
    emms
    


After you run the assembler by simply typing "assemble" at the DOS prompt, the file "source.out" is produced. It will look something like this:

    db $0F, $6F, $0A                          { movq mm1,[edx]        }
    db $0F, $6F, $00                          { movq mm0,[eax]        }
    db $0F, $EF, $C1                          { pxor mm0,mm1          }
    db $0F, $77                               { emms                  }
    


You can then paste this file into your Delphi code, for example like this:

    Asm
    ...
      cmp  IsMMXProcessor,False
      je   @@noMMX
    @@MMX:
      db   $0F,$6F,$0A    { movq mm1,[edx] }
    @@MMXloop:
      db   $0F,$6F,$00    { movq mm0,[eax] }
    ...
    End;
    


Of course, not every computer has a MMX processor, so you have to provide "normal" code to handle things in case there is no MMX support in the computer your code runs on.

For detection purposes, I've written an unit, which defines a global variable, IsMMXProcessor. You can use this variable to check if the computer has a MMX processor. For example:

    Uses MMX;
    ...
    Begin
      If IsMMXProcessor Then Begin
        { ...MMX code... }
      End
      Else Begin
        { ...normal code... }
      End;
    End;
    



With the assembler and the unit, you should be ready to write MMX enabled software. Of course, teaching you MMX programming is beyond the scope of this document. If you want to learn MMX programming, check out Intel's site at mmx.com. It has a wealth of information, including the MMX programmers manual, which I personally find the most interesting. The sad thing is that you can't find much example MMX code from the Net.

I've also provided a simple example program, which demonstrates en/decryption of usernames and passwords.

Now you know that even Delphi can be used to code MMX instructions with a little effort. Of course, it would be a lot easier if Delphi would support these new instructions, and I do hope that future versions of Delphi will do just that. In the meantime, start learning MMX, and your apps might just become 60% faster... ;-)


Code for this How To


  • Environment: Windows 95 or NT 4.0
  • Compiler: Delphi 2.0 for Win32
  • Memory: 4 MB RAM, 60k disk
  • Filename: mmxandd2.zip
  • Size: 27 554 bytes, 27kB
  • Date (M/D/Y): 11/21/97 01:01AM
  • Download time: 14.4k: about 20 seconds, 28.8k: about 10 seconds
  • Note: Updated version 1.01, fixed the MOVD bug.
  • Distribution: Freeware
  • Registration price: none
  • Download!


Questions? Comments? Give feedback. Or just fill in a form.