打印本文 打印本文 关闭窗口 关闭窗口
Delphi嵌入式汇编一例
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1033  更新时间:2009/4/23 18:25:56  文章录入:mintao  责任编辑:mintao

用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%。

打印本文 打印本文 关闭窗口 关闭窗口