Name
Asm Keyword
Syntax
asm
assembler instructions
end;Description
The asm keyword starts a block of assembler
instructions.
Tips and Tricks
An
asmblock 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, orDDdirectives to compile the opcodes manually.An
asmblock can change theEAX,ECX, andEDXregisters, but must preserve the values ofEBX,ESI,EDI,EBP, andESP. 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, andstdcall) 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
asmblock 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access