Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good Resources for using Assembly in Delphi?

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.
like image 402
Shannon Matthews Avatar asked Aug 17 '11 01:08

Shannon Matthews


4 Answers

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...)

like image 122
Bob A Avatar answered Nov 09 '22 23:11

Bob A


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.

like image 42
Jeroen Wiert Pluimers Avatar answered Nov 09 '22 23:11

Jeroen Wiert Pluimers


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.

like image 29
Glenn1234 Avatar answered Nov 10 '22 01:11

Glenn1234


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.

like image 28
PhiS Avatar answered Nov 10 '22 00:11

PhiS