Question

Question

Are there any resources for learning how to use assembly in Delphi?

Background Information

I've found and read some general assembly and instruction set references (x86, MMX, SSE etc). But I'm finding it difficult to apply that information in Delphi. General things like how to get the value of a class property etc.

I would like to have the option to use assembly when optimising code.

I understand:

  • It will be difficult to beat the compiler.
  • High-level optimisation techniques are much more likely to increase performance by several orders of magnitude over low-level assembly optimisations. (Such as choosing different algorthims, caching etc)
  • Profiling is vital. I'm using Sampling Profiler for real-world performance analysis and cpu cycle counts for low-level details.

I am interested in learning how to use assembly in Delphi because:

  • It can't hurt to have another tool in the toolbox.
  • It will help with understanding the compiler generated assembly output.
  • Understanding what the compiler is doing may help with writing better performing pascal code.
  • I'm curious.
Était-ce utile?

La solution

Here is a resource that could be helpful...

www.guidogybels.eu/docs/Using%20Assembler%20in%20Delphi.pdf

(I wanted to add a comment to @Glenn with this info, but am forced to use the Answer mechanism as I am New to this forum and not enough Reps...)

Autres conseils

Most optimization involves creating better algorithms: usually that's where you can get the 'order of magnitude' speed improvements can be obtained.

The x64 assembly world is a big change over the x86 assembly world. Which means that with the introduction of x64 in Delphi in XE2 (very soon now ), you will have to write all your assembly code twice.

Getting yourself a better algorithm in Delphi relieves you of writing that assembly code at all.

The major area where assembly can help (but often smartly crafted Delphi code helps a lot too) is low level bit/byte twiddling, for instance when doing encryption. On the other hand FastMM (the fast memory manager for Delphi) has almost all code written in Delphi.

As Macro already wrote: starting with the disassembled code is often a good start. But assembly optimizations can go very far.
An example you can use as a starting point is for instance the SynCrypto unit which has an option for using either Delphi or assembly code.

The way I read your post, you aren't looking so much for assembler resources as resources to explain how Delphi declarations are structured within memory so you can access them via assembler. This is indeed a difficult thing to find, but not impossible.

Here is a good resource I've found to begin to understand how Delphi structures its declarations. Since assembler only involves itself with discrete data addresses to CPU defined data types, you'll be fine with any Delphi structure as long as you understand it and access it properly.

The only other concern is how to interact with the Delphi procedure and function headers to get the data you want (assuming you want to do your assembler using the Delphi inline facility), but that just involves understanding of the standard function calling conventions. This and this will be useful to that end in understanding those.

Now using actual assembler (linked OBJ files) as opposed to the inline assembler is another topic, which will vary depending on the assembler chosen. You can find information on that as well, but if you have an interest you can always ask that question, too.

HTH.

To use BASM efficiently, you need to have a knowledge both of (1) how Delphi does things at a low level and (2) of assembly. Most of the times, you will not find both of these things described in one place.

However, Dennis Christensen's BASM for beginner and this Delphi3000 article go in that direction. For more specific questions, besides Stackoverflow, also Embarcadero's BASM forum is quite useful.

The simplest solution is always coding it in pascal, and look at the generated assembler.

Speedwise, assembler is usually only at a plus in tight loops, and in general code there is hardly improvement, if any. I've only one piece of assembler in my code, and the benefit comes from recoding a floating point vector operation in fixed point SSE. The saturation provided by SIMD instruction sets is an additional bonus.

Worse even, much ill advised assembler code floating around the web is actually slower than the pascal equivalents on modern processors because the tradeoffs of processors changed over time.


Update:

Then simply load the class property in a local var in the prologue of your procedure before you enter the assembler loop, or move the assembler to a different procedure. Choose your battles.

Studying RTL/VCL source might also yield ideas how to access certain constructs.

Btw, not all low level optimization is done using assembler. On Pascal level with some pointer knowledge a lot can be done too, and stuff like cache optimization can sometimes be done on Pascal level too (see e.g. Cache optimization of rotating bitmaps )

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top