Using assembly example in Delphi
----- is also a big two, then the previous example. Oh, the program is a bit chaotic at the time, please forgive :).
Using compilation in Delphi is mixed and programmed, it is an exotic convenience. So, when you want to get efficiency, you can choose compilation, but most of you work, you still enjoy visualization, object-oriented programming. Delphi's language, by using the ASM ... END block write assembly code, you can call the high-level variable in the assembly code segment; you need to pay attention to protect some register variables, then, some details and DOS The next assembly is different. Everyone see this example. First, some little knowledge is said: 1. Delphi calls compilation, there are two ways. As - procedure tform1.drawrain; varX1, y1, x2, y2, d, i: integer; begin // delphi program start for i: = 0 to 100 dobeginx1: = random (537); Y1: = Random 280); D: = random (7); ASM // embedded assembly start PUSH Eaxmov Eax, X1SUB Eax, DMOV X2, Eaxmov Eax, Y1AX, DMOV Y2, EaxPop Eaxend ;; // Embedded assembly end Times: = Times 1; DrawLine2 (X1, Y1, X2, Y2, Clmedgray); Wait (); if (I Div 2) = 0 THEN DRAWLINE2 (X1, Y1, X2, Y2, CLWindow); end; end; // Delphi Program end
Also, remove the outer begin ... End, directly enter the assembly through the ASM ... END.
Procedure tform1.wait (); ASM // embedded assembly PUSH Eaxmov Eax, 0 @ loop: add eax, 1cmp eax, 1000000jnz @looppop eaxnd; // Embedded assembly end
Second, use Delphi variables in assembly
Everyone see the example of the first code, it is easy to understand: ASM // embedded assembly start PUSH Eaxmov Eax, X1 // x1 is a Delphi local variable, this is used for the value SUB EAX, DMOV X2, EAX // x2 is Delphi Local variables, and used as memory address MOV EAX, Y1ADD EAX, DMOV Y2, EAXPOP EAXEND ;; / / Embedded assembly ends three, compiling different details examples, everyone compares, such as @LOOP: //. ..jnz @loop In addition, most of the INT interruption may not be used, I have not tried, and I have not seen any information. Who knows, give me a message? Thank you, first.
Here is a relatively large example, an animation effect. Because I have just begun to learn this knowledge, it is very rough. It is a small wood, keeping the scenes of the rain - a lot of problems, the performance is outstanding, first floating point instructions are not used (I will attach the test example of the floating point directive to continue to be tried), Some algorithms are still not, and the result is a comparative monotony. Secondly, the coordination between the scene is not good enough, such as the tree is not effectively protected, and then it is raining "flower" :). But you still have an example, don't laugh at me, there is no time to write this example, two, learn the assembly in Delphi, it can still explain some questions. Moreover, in general information, it is to avoid this application. In the individual advanced programming, occasionally mention, you can master it, most of you have to practice more, this example can give you a lot of lessons :). Unit unit1;
Interface
Useswindows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Extctrls, Menus, Extdlgs, FileCtrl, Stdctrls, Comctrls, Imglist;
typeTForm1 = class (TForm) MainMenu1: TMainMenu; File1: TMenuItem; Exit1: TMenuItem; N1: TMenuItem; Help1: TMenuItem; About1: TMenuItem; g1: TMenuItem; Spring1: TMenuItem; Summer1: TMenuItem; Autumn1: TMenuItem; Winter1: TMenuItem; StatusBar1: TStatusBar; ext1: TMenuItem; Timer1: TTimer; procedure Exit1Click (Sender: TObject); procedure About1Click (Sender: TObject); procedure Spring1Click (Sender: TObject); procedure FormCreate (Sender: TObject); procedure Summer1Click (Sender: TObject ); procedure Autumn1Click (Sender: TObject); procedure Winter1Click (Sender: TObject); procedure ext1Click (Sender: TObject); privateprocedure DrawLine1 (x1, y1, x2, y2: integer; icolor: TColor); // x2> x1 and Y2> Y1Procedure Drawline2 (x1, y1, x2, y2: integer; icr: tcolor); // x2
Varform1: TForm1; Times: SINGLE; ImplementationUses Unit3, Unit5; {$ r * .dfm} Procedure TForm1.Wait (); Asmpush Eaxmov Eax, 0 @ loop: Add Eax, 1cmp Eax, 1000000jnz @loopPop EAXEND;
Procedure TForm1.drawLine1 (x1, y1, x2, y2: integer; icr: tcolor); const i: integer = 1; var j, k1, k2, d: integer; beginif ((x2-x1) <= 0) OR ((Y2-Y1) <= 0) THEN EXIT; for J: = 0 to (x2-x1) DOBEGINFORM1.CANVAS.PEN.COLOR: = ICOLOR; FORM1.CANVAS.ELLIPSE (X1, Y1, X1 2, Y1 2); Form1.canvas.ellipse (x1-1, Y1-1, X1 1, Y1 1); if ((x2-x1) = 0) or (Y2-Y1) = 0) THEN EXIT; Asmpush EAXPUSH EBXPUSH ECXPUSH EDXMOV ECX, 0MOV EBX, Y2SUB EBX, Y1MOV D, EBXMOV EDX, 0DIV DMOV K2, EAX
MOV EAX, Y2SUB EAX, Y1MOV EBX, X2SUB EBX, X1MOV D, EBXMOV EDX, 0DIV DMOV K1, EAX
Mov Eax, x1 @ loop2: add eax, IINC ECXCMP ECX, K2JB @ Loop2MOV X1, EAX
MOV Eax, Y1 @ loop1: add eax, IINC ECXCMP ECX, K1JB @ Loop1Mov Y1, EAX
POP EDXPOP ECXPOP EBXPOP EAXEND; END; // end forend; procedure tform1.drawline2 (x1, y1, x2, y2: integer; icr: tcolor); Const i: integer = 1; var j, k1, k2, d: integer Beginif ((x1-x2) <= 0) OR (Y2-Y1) <= 0) THEN EXIT; for J: = 0 TO (X1-x2) DOBEGINFORM1.CANVAS.PEN.COLOR: = IColor; Form1. Canvas.ellipse (x1, y1, x1 2, y1 2); Form1.canvas.ellipse (x1-1, Y1-1, X1 1, Y1 1); if ((x1-x2) = 0) OR ((Y2-Y1) = 0) THEN EXIT; ASMPUSH EAXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH ECXPUSH EBXPUSH EDX
MOV ECX, 0
MOV EAX, X1SUB EAX, X2MOV EBX, Y2SUB EBX, Y1MOV D, EBXMOV EDX, 0DIV DMOV K2, EAX
MOV EAX, Y2SUB EAX, Y1MOV EBX, X1SUB EBX, X2MOV D, EBXMOV EDX, 0DIV DMOV K1, EAX
Mov Eax, X1 @ loop2: Sub Eax, IINC ECXCMP ECX, K2JB @ Loop2MOV X1, EAX
MOV Eax, Y1 @ loop1: add eax, IINC ECXCMP ECX, K1JB @ Loop1Mov Y1, EAX
POP EDXPOP ECXPOP EBXPOP EAXEND; end; // end forend; procedure tform1.drawline3 (x1, y1, x2, y2: integer; icr: tcolor); var j: integer; beginif ((x2-x1) <> 0) and ((Y2-Y1) <> 0) THEN EXIT; IF (x2-x1) = 0 Thenfor J: = 0 to (Y2-Y) DOBEGINFORM1.CANVAS.PENVAS.ELOR: = ICOLOR; FORM1.CANVAS.ELLIPSE (x1 -1, Y1-1, X1 1, Y1 1); if (Y2-Y1) = 0 THEN EXIT; ASMPUSH EAXMOV EAX, Y1Inc Eaxmov Y1, EDPOP END eEnd; End // End ForeLse for J: = 0 TO X2-x1) DOBEGINFORM1.CANVAS.PEN.COLOR: = IColor; Form1.canvas.ellipse (x1-1, y1-1, x1 1, y1 1); if (x2-x1) = 0 THEN EXIT; ASMPUSH eaxmov eax, x1inc eaxmov x1, eaxpop eaxend; end; // end forend; procedure TForm1.Timer1Timer (sender: TObject); beginend; procedure TForm1.DrawRain; varx1, y1, x2, y2, d, i: integer; beginfor i : = 0 to 100 dobeginx1: = random (537); Y1: = random (280); D: = Random (7); Asmpush Eaxmov Eax, X1Sub Eax, DMOV X2, Eaxmov Eax, Y1AX, DMOV Y2, EaxPop Eaxend Times: = Times 1; DrawLine2 (X1, Y1, X2, Y2, CLMEDGRAY); Wait (); IF (I Div 2) = 0 THEN DRAWLINE2 (X1, Y1, X2, Y2, CLWindow); END; END; ;
procedure TForm1.DrawTree (x, y, height: integer); varx1, y1, x2, y2, x3, y3, x4, y4, x5, y5, d1: integer; beginasmpush eaxpush ebxpush ecxpush edxmov eax, heightmov edx, 0mov ecx 2DIV ECXMOV D1, EAXMOV EAX, XMOV EBX, Y
sub eax, heightmov x1, eaxsub ebx, heightmov y1, ebxadd eax, heightsub eax, 5sub ebx, heightmov x2, eaxmov y2, ebxmov eax, xadd eax, 3sub ebx, heightmov x3, eaxmov y3, ebxadd eax, 8add ebx, heightmov x4 , Eaxmov Y4, EbxAdd Eax, Heightsub Eax, 5Add EBX, Heightmov X5, EAXMOV Y5, EBX
POP EDXPOP ECXPOP EBXPOP EAXEND; DRAWLINE2 (X3, Y3, X2-D1, Y2, CLCREAM); DrawLine1 (X3, Y3, X4 D1, Y4, CLCREAM); DrawLine3 (X2-D1, Y2, X2, Y2, CLCREAM) Drawline3 (X4, Y4, X4 D1, Y4, CLCREAM); DrawLine2 (X2, Y2, X1, Y1, CLcReam); DrawLine1 (X4, Y4, X5, Y5, CLCREAM); DrawLine2 (x2 2, y2, X1, Y1 2, CLcReam; DrawLine1 (X4-2, Y4, X5, Y5 2, CLcReam); Drawline3 (X1, Y1, X5, Y5, CLCREAM); DrawLine3 (x1, y1-1, x5, y5 -1, clcream; DrawLine3 (x, y5, x, y, clcream); DrawLine3 (X, Y, X 5, Y, CLcream); DrawLine3 (x 5, Y5, X 5, Y, CLCReam) Form1.canvas.brush.color: = CLGreen; Form1.canvas.floodfill (x, y1-5, clcream, fsborder); form1.canvas.floodfill (x 2, y-5, clcream, fsborder); Procedure TFORM1.EXIT1CLICK (Sender: TOBJECT); Beginclose; End;
Procedure TFORM1.About1Click (Sender: TOBJECT); Beginform3.Show;
Procedure tform1.spring1click (sender: TOBJECT); VAR i, K, J: Inteder; beginstatusbar1.panels [1] .text: = 'Wind and rain Spring ...'; Form1.canvas.brush.color: = CLWindow; Form1 .Canvas.floodfill (1, 1, clred, fsborder); for i: = 0 to 10dobegink: = random (10); J: = Random (10); J: = - J; DrawTree (10 k * 50, 300 J * 20, 20); end; for i: = 0 to 10 dobeginform1.canvas.brush.color: = CLWindow; Form1.canvas.Floodfill (1, 1, clgreen, fsborder); Drawrain; SLEEP (100); end ;
END;
procedure TForm1.FormCreate (Sender: TObject); begintimes: = 0; form1.canvas.Brush.Color: = clwindow; form1.caption: '! Please select the Pictype ...' = form1.caption ; end; procedure TForm1. Drawclear; var i, k: integer; beginform1.canvas.brush.color: = clback; form1.canvas.floodfill (15, 15, clcream, fsborder); for i: = 0 to 5 dobegink: = random (300); Drawline3 (20 K, 1, 20 K, 307, CLYELOW); DrawLine3 (200 K, 1,200 K, 307, CLYELOW); end; end; procedure tform1.summer1click (sender: TOBJECT); begindrawclear; Form1. Canvas.font.color: = CLBLUE; form1.canvas.font.size: = 18; Form1.canvas.Textout (150, 150, 'Green Ocean ... Time ^ _ ^!'); // DrawTree;
Procedure TFORM1.AUTUTUMN1CLICK (Sender: TOBJECT); begindrawclear; form1.canvas.font.color: = CLBLUE; FORM1.CANVAS.FONT.SIZE: = 18; Form1.canvas.Textout (150, 150, 'Red Yushan ... is holding ^ _ ^! ');
Procedure TForm1.Winter1click (Sender: Tobject); // Var i, x, y: integer; begindrawclear; form1.canvas.font.color: = CLBLUE; FORM1.CANVAS.FONT.SIZE: = 18; Form1.canvas.Textout (150, 150, 'Silver Snake ... No open ^ _ ^!');
Procedure TFORM1.EXT1CLICK (Sender: TOBJECT); beginform5.show;
End.
Here is the test program that uses floating point commands. At that time, the original, not achieved: // Instruction library Check :) Procedure TFORM1.XX; VARXMAX, YMAX, X, Y, A, B: Single i, z: integer; begina: = 2.0; B: = 10.0; xmax: = image1.width; ymax: = image1.height; for i: = 0 to image1.width dobeginasm /////////////ww130x87
Finitfld xmax // push xmax in st (0) fdiv afchs // let st (0) = - ST (0) fadd i // st (0) = ST (0) ifdiv b // st (0) = ST (0) / BFSTP X // Put St (0) in x without popfld xfmul xfchsfadd ymax // frndint // Fabsfstp Yend; x: = (i-xmax / 2) / 10; y: = - x * x ymax ; z: = trunc (z); Image1.canvas.ellipse (i-1, trunc (y) -1, i 1, trunc (y) 1); // image1.canvas.pixels [x, y] End; end; choose one of the processes, huh, huh, laugh. Everyone may wish to take out your own things. Experience said, I am going to listen to :). please----