Name

Asm Keyword

Syntax

asm
  assembler instructions
end;

Description

The asm keyword starts a block of assembler instructions.

Tips and Tricks

  • An asm block is a statement, and you can use it anywhere that calls for a Pascal statement or block, such as the body of a subroutine.

  • You can refer to variable names within the assembly block and jump to labels declared elsewhere in the procedure. Do not jump into a loop unless you know what you are doing. A label that starts with an @ sign is local to the subroutine and does not need to be declared.

  • Delphi’s built-in assembler tends to lag behind the technology, so you cannot usually rely on having the latest and greatest instruction set. Instead, you can use DB, DW, or DD directives to compile the opcodes manually.

  • An asm block can change the EAX, ECX, and EDX registers, but must preserve the values of EBX, ESI, EDI, EBP, and ESP. As a rule, you should not assume that any registers contain special values, but if you are careful, you can access a subroutine’s parameters in their registers. See the calling convention directives (cdecl, pascal, register, safecall, and stdcall) to learn how arguments are passed to a subroutine.

  • Writing assembly code by hand rarely gives you better performance. The most common reason to use an asm block is to use instructions that are not available in Delphi, such as the CPUID instruction shown in the example.

Example

unit cpuid; // CPU identification. // This unit defines the GetCpuID function, which uses the CPUID ...

Get Delphi in a Nutshell now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.