Delphi 系统[12]关键字和保留字 asm、assembler

Delphi 系统[12]关键字和保留字  asm、assembler

1、定义:

  • asm 用于在程序中插入汇编代码。使用汇编代码时, 必须使用 asm ...end; 的结构,而非begin ... end; 
  • assembler 用于支持早期的汇编, 如80386等。它和 asm 的区别是 asm 允许使用 Win32 汇编, 而 assembler 只允许 80x86 汇编, 它不允许 Invoke 语句的出现。 

2、示例:

function IntToHex(Value: Integer; Digits: Integer): string;
//  FmtStr(Result, '%.*x', [Digits, Value]);
asm
        CMP     EDX, 32        // Digits < buffer length?
        JBE     @A1
        XOR     EDX, EDX
@A1:    PUSH    ESI
        MOV     ESI, ESP
        SUB     ESP, 32
        PUSH    ECX            // result ptr
        MOV     ECX, 16        // base 16     EDX = Digits = field width
        CALL    CvtInt
        MOV     EDX, ESI
        POP     EAX            // result ptr
        CALL    System.@LStrFromPCharLen
        ADD     ESP, 32
        POP     ESI
end;

function IntToHex(Value: Int64; Digits: Integer): string;
//  FmtStr(Result, '%.*x', [Digits, Value]);
asm
        CMP     EAX, 32        // Digits < buffer length?
        JLE     @A1
        XOR     EAX, EAX
@A1:    PUSH    ESI
        MOV     ESI, ESP
        SUB     ESP, 32        // 32 chars
        MOV     ECX, 16        // base 10
        PUSH    EDX            // result ptr
        MOV     EDX, EAX       // zero filled field  0 for no leading zeros
        LEA     EAX, Value;
        CALL    CvtInt64

        MOV     EDX, ESI
        POP     EAX            // result ptr
        CALL    System.@LStrFromPCharLen
        ADD     ESP, 32
        POP     ESI
end;

3、其他说明:

  • 通常,asm语句中寄存器的使用规则与外部过程或函数的使用规则相同。
  • asm语句必须保留EDI、ESI、ESP、EBP和EBX寄存器,但可以自由修改EAX、ECX和EDX寄存器。
  • 在进入asm语句时,EBP指向当前堆栈帧,ESP指向堆栈顶部。
  • 除了ESP和EBP之外,asm语句不能假设语句项上的寄存器内容。  

  

创建时间:2021.08.11  更新时间:

博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
原文地址:https://www.cnblogs.com/guorongtao/p/15128796.html