Delphi嵌入式汇编一例

Delphi教程 2025-07-28

用delphi写的程序,把x指针指向的4个字节次序颠倒过来:

function toulong(x: pchar): longWord;

begin

result := (longword(x^) shl 24) or

(longword((x + 1)^) shl 16) or

(longword((x + 2)^) shl 8) or

(longword((x + 3)^));

end;

以下是用delphi的嵌入式汇编写法:

function toulong(x: pchar): longword;

asm

mov esi,eax

mov ax,[esi]

xchg ah,al

shl eax,16

mov ax,[esi+2]

xchg ah,al

end;

说明:默认情况下,delphi使用“register”方式,若参数在3个已内,

将分别使用eax、edx和ecx,超过3个参数部分将使用堆栈。返回参数的

存放视长度而定,例如8位用al返回,16位用ax,32位用eax,64位用用两个

32位寄存器edx:eax,其中eax是低位。

效率:本例asm大约比delphi或c快50%。