Good resources for using assembly in Delphi?

Question

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

Background Information

I found and read some general assembly and instruction references (x86, MMX, SSE, etc.). But it's hard for me to apply this information in Delphi. Common things, for example, how to get the value of a class property, etc.

I would like to be able to use assembly in code optimization.

I understand:

  • It's hard to beat the compiler.
  • High-level optimization methods are much more likely to improve performance by several orders of magnitude compared to low-level assembly optimizations. (For example, the choice of different algorithms, caching, etc.).
  • Profiling is vital. I use Sampling Profiler to analyze real-time performance and count CPU cycles for low-level parts.

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

  • It is not possible to damage another tool in the toolbar.
  • This will help with understanding the assembly generated by the assembler.
  • Understanding what the compiler does can help you write more efficient pascal code.
  • I am curious.
+7
source share
5 answers

Here is a resource that may be useful ...

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

(I wanted to add a comment to @Glenn with this information, but I have to use the response mechanism, as I am new to this forum and not enough Reps ...)

+12
source

Greater optimization involves creating better algorithms: usually when you can get an “order of magnitude” speed improvements can be achieved.

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

Getting the best algorithm in Delphi saves you from writing this assembly code.

The main area in which assembly can help (but often very thought out Delphi code) is low-level bit / byte bits, for example, during encryption. On the other hand, FastMM (fast memory manager for Delphi) has almost all the code written in Delphi.

As Macro already wrote: starting with parsed code is often a good start. But assembly optimization can go very far.
An example that you can use as a starting point is, for example, the SynCrypto block, which has the ability to use either Delphi or assembly code.

+6
source

As I read your post, you don't know much about assembler resources as resources to explain how Delphi declarations are structured in memory so you can access them through assembler. It is really hard to find, but not impossible.

Here is a good resource that I found to begin to understand how Delphi structures its ads. Since the assembler uses only discrete data addresses for data types defined by the processor, you will be fine with any Delphi structure if you understand it and get access to it properly.

The only problem is how to interact with the headers of the Delphi procedures and functions to obtain the required data (provided that you want to make your assembler using the built-in Delphi tool), but this is simply related to understanding the standard function calls, This and this will useful for this purpose in understanding these.

Now, using the actual assembler (linked OBJ files), as opposed to the built-in assembler, is another topic that will vary depending on the assembler selected. You can also find information about this, but if you have interest, you can always ask this question.

NTN.

+5
source

To use BASM effectively, you need to know how (1) how Delphi does things at a low level, and (2) builds. In most cases, you will not find both of these things described in one place.

However, Dennis Christensen BASM for Beginners and the Delphi3000 article go in that direction. For more specific questions, besides Stackoverflow, the Embarcadero BASM forum is also useful.

+4
source

The simplest solution always encodes it in pascal and looks at the generated assembler.

Speedwise, assembler usually has only advantages in tight loops, and in general, the code is unlikely to improve, if any. There is only one piece of assembler in my code, and the advantage comes from recoding the floating-point vector operation in a fixed-point SSE. The saturation provided by the SIMD instruction sets is an added bonus.

Worse, very poorly informed assembly code floating around the Internet is actually slower than pascal equivalents for modern processors, as processor tradeoffs have changed over time.


Update:

Then simply load the class property into the local var in the prolog of your procedure before entering an assembler loop or moving the assembler to another procedure. Choose your battles.

Examining the source of RTL / VCL can also give an idea of ​​how to access some constructs.

Btw, not all low-level optimization is done using assembler. At the Pascal level with some pointer knowledge, much can be done too, and sometimes at the Pascal level the same cache optimization can be done (see, for example, Cache optimization of rotating bitmaps )

+2
source

All Articles