Jump to content

Delphi Programming/Assembler in Delphi

From Wikibooks, open books for an open world

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;
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;