Delphi Programming/Assembler in Delphi

From Wikibooks, open books for an open world
Jump to: navigation, search

Since Turbo Pascal 1.0, the use of assembly code within Borland (currently Embarcadero) languages was quite easy.

In Delphi, simply put an assembly block between asm...end; within a code block, or you can declare a whole function procedure as assembler.

Global or local variables can be used normally, but of course they will work as pointers.

Examples:

function StrLen (S : Pchar ) : cardinal; begin

 asm
   MOV   EDX, EDI
   MOV   EDI, EAX
   MOV   ECX,0FFFFFFFFh
   XOR   AL, AL
   REPNE SCASB
   MOV   EAX, 0FFFFFFFEh
   SUB   EAX, ECX
   MOV   EDI, EDX
 end;

end;

function StrLen (S : Pchar) : cardinal; assembler; asm

 MOV   EDX, EDI
 MOV   EDI, EAX
 MOV   ECX, 0FFFFFFFFh
 XOR   AL, AL
 REPNE SCASB
 MOV   EAX, 0FFFFFFFEh
 SUB   EAX, ECX
 MOV   EDI, EDX

end;