Delphi Programming & Assembly langage



Version FRANCAISE
DOWNLOAD ZONE: BOTTOM OF THIS PAGE - SCREENSHOOT ZONE: MIDDLE PAGE

NEW:
Last update : 10/18/2001 # VERSION 1.7 #
DIBUltra ver 2.0.0 release coming soon !
New features :
The Mask becomes a real powerful object.
A very efficient TSprite Manager.
Many surprises... ;-)

I'm french. Please excuse my awful english...

There are many WEB sites devoted to DELPHI but I do not know one of them which really exploits the true power of the compiler while speaking about the programming ASSEMBLER under Delphi 2 .. 6 & Kylix.

Also, I will try this approach by placing at your disposal a graphic unit encapsulating the DIB and carrying out various operations. This unit is under development but gives already appreciable results. It is significant to underline its teaching aspect because one can find in the sources a very significant mass of comments and explanations of any kind.

This unit is addressed nevertheless to people, who have some assembly langage knowledges. If it's not your case, go to Yahoo or Altavista or your favorite search engine and try "assembly programming" to get the bases and a minimum knowledge of the thing.
(Ok, now you read and understood ten pages on the subject).
As a memorandum, you will find here 80x86 asm instructions (french) (This text is from Gael LE MAHEC). However, to program seriously it is necessary to have the technical handbook (format pdf) exempted by INTEL. However before INTEL gives me the right to deliver its handbook on line, you should better go to recover it.
Thanks to the Delphi or C++Builder compiler, you have constantly the possibility to switch in ASM mode while typing:

asm
... your asm instructions...
end;
Of course, there are certain conventions to respect (EBX to be saved, etc...) The best for you would be to read the Delphi.hlp for that. In your programs, when you turn a lot of time in a loop, (a million times to treat the pixels of an image for example), it can be judicious to translate it into assembly langage. However, translating all your program into assembler is completly useless! So you gave to target the critical zone and try to do better than the compiler ! (this last remark has to be taken seriously ;-) )

Do not worry, assembly langage is not very difficult and Delphi facilitates us its use (asm syntax supports variables, records, procedures, etc... defined in the Pascal parts of the program) and to reassure you, find here after three examples of the source code.
You can see they are fully commented : (but do not try to understand them out of their context!)



@LINEHORZT24BITS:
    // Trace horizontal line
    MOV  EAX, Coul
    MOV  EBX, CoulTmp     // Permute the Red color
  @RepeatLNX24:
      ROL  MASQUE, 1      // Repeat Loop
      JNC  @LNX24NOTRACE  //   If PlotON Then Begin
      MOV  [EDI + 0], AL  //     PxB^[Pt] := CoulB; <--- [Mem] <= AL (8 bits)
      MOV  [EDI + 1], AH  //     PxB^[Pt] := CoulV; <--- [Mem] <= AH (8 bits)
      MOV  [EDI + 2], BL  //     PxB^[Pt] := CoulR; <--- [Mem] <= DH (8 bits)
    @LNX24NOTRACE:        //   End Else ;                           ----------
      ADD  EDI, AdXPlus   //   Inc(Pt,AdXPlus);                     = 24 bits
      SUB  ECX, PasX      //   Dec(dx, PasX)
      JNZ  @RepeatLNX24   //   Until (dx=0);
      MOV  EBX, PasY
      ADD  EDI, AdYPlus   // Inc(Pt,AdYPlus)
    RET




    // During the double indirection, we calculate:  
    // Seg.dy :=  Seg.y2 - Seg.y1;  
    // Seg.dx :=  Seg.x2 - Seg.x1;  
    // That removes the data dependance between instructions 
    // and avoids the cycles of freeze of the 
    // processor (standard Pentium and successor x86)

    MOV ECX,    Seg.y2 {Calculation of Seg.dy}
    LEA EAX,    [EAX * 1 + @Table1] // Double Indirection : first level
    SUB ECX,    Seg.y1 {Calculation of Seg.dy}
    MOVZX EAX,  BYTE PTR [EAX]      // Double Indirection : Recovery of N of One N Goto
    MOV Seg.dy, ECX   {Calculation of Seg.dy}
    LEA EAX,    [EAX * 4 + @Table2] // Double Indirection : second level
    MOV ECX,    Seg.x2 {Calculation of Seg.dx }
    MOV EAX,    [EAX]               // Prepare to JUMP
    SUB ECX,    Seg.x1 {Calculation of Seg.dx}
    MOV Seg.&dy,ECX    {Calculation of Seg.dx}
    JMP EAX                         // GO ! : On X Goto n1,n2,n3,n4,n5...



    procedure TDIBUltra.RotateStyle (n : Byte); assembler;
    asm    // turn around the mask (N Modulo 32) times
      MOV  CL, n
      ROL  [Self].Mask, CL
    End;

    function  TDIBUltra.PlotON : Boolean; assembler;
    asm
      MOV  Result, True
      ROL  [Self].Mask, 1
      JC   @End
      MOV  Result, False
    @End:
    End;

    procedure TDIBUltra.PrepareStyle; assembler;
    asm    // Turn around the mask until presenting a printable point
      MOV  EAX, [Self].Mask
      BSR  ECX, EAX
      SUB  ECX, 31
      NEG  ECX
      ROL  [Self].Mask, CL
    End ;

EXTENSION of DIBUltra:

Because many functionalities of this library avoid having recourse to GDI calls, we can plan to develop applications, wich only use those calls (for GDI calls of course). BitBlit functions (memory DIB to video memory) are very close in all systems, so it's possible of make run a application under Linux or Win95 & Co with a appearance and a behavior similar!

Use Kylix Open Edition ! DIBUltra 1.x doesn't compile under Kylix but DIBUltra 2.x does yet ! (DIBUltra 1.x run with Wine, the Win32 "emulator") For more information or reflexion about a bi-OS library, E-Mail me !


I will not detail here the functionalities of the DIBUltra unit knowing that 7 small projects are provided to explain its use. Of course, all the projects are provided with the sources. The current licence is the GNU Licence.
Let us say simply that DIBUltra makes it possible to trace in a faster way of the GDI of Windows, the lines. (the function Pixels[x,y] := ClColor is for example 10 times faster) This unit implements a PenStyle (not, dashes, defined by the user...) very easy to use.
DIBUltra has his own format of compressed images DIBs (but can also read normal BMP). A clipping zone can be freely defined by the user who can thus limit his layout. A TDIBUltra object is typically used except screen, (in Blit surfaces), so DIBUltra offers buffering management functions.
The last version (1.5) corrects some bugs and brings the support of the Alpha layer (various densities of transparency are possible, definable before (with your favorite drawing editor). You can now choose a pen mode (COPY, ORPEN, ANDPEN, XORPEN...)

When it was possible, the methods and properties of TDIBUltra are identical to those of their TBitmap equivalent. There are some differences (ex Bitmap.Canvas.MoveTo(x,y) becomes DIB.MoveTo(x, y) or Bitmap.Canvas.Pen.Color := ClRed becomes DIB.PenColor := ClRed...) but in general one makes the bearing rather quickly. Finally remember that this unit has a teaching object too and that the assembly code is not yet fully optimized. Next version will undoubtedly be faster...
The 7 small applications provided with DIBUltra show:

AboutBox : - the use of Pixels[x, y ] for a fast movement
Compress : - the use of the methods of the images loading / saving on disk / resource / stream.
Drawing : - the use of the style of brush (dash, dot, dashdotdot ...), of the indexed colors, ScanLine, Buffering.
ScreenSplash : - how to show a screensplash logo (like DIBUltra logo) (see it !)
SimpleDemo : - a very simple demo for beginner (someone ask for that !)
AlphaDemo : - the use of the AlphaChanelBlit function to display an image with various levels of transparency.
AlphaTool : * This application makes it possible to compile compressed images incorporating the Alpha layer.
Some screenshots for the curious ones:


AboutBox (By the way) of the DRYSIM Project


Idem a few seconds later...



The unreal fairy is displayed with certain
very transparent zones and others opaque...



To build a Alpha layer DIB, a small utility assembles
the various files (the image and its mask).



The application of the methods LoadFrom / SaveTo



The window of tracing of the multiple lines...


And with various styles!


DIBUltra breaks up into 4 units:

DIBUltra.pas : Core of DIBUltra, TDIBUltra object
DIBtype.pas : All the types usable are there
DIBCodec.pas : This unit allows enCOding and DECoding UDC files. (lzh, rle...)
DIBFX.pas : This file is directly included ($INCLUDE) in DIBUltra, it acts of the special effects

One very quickly notices the advantage of assembler (and comments) in certain cases, indeed in spite of 450 KB of source files, this compiled unit does not exceed 55 KB dcu (counting all units).


Discussed enough good, let us pass to the downloading units with the examples projects:

DIBUltra (1.3) with BINARY EXE + SOURCES 780 KB
DIBUltra (1.7 !!) with SOURCES NOT COMPILED 337 KB (better not?)

Enjoy yourself !


Return.

WordPress Appliance - Powered by TurnKey Linux