Delphi的嵌入式asm

字体大小: 中小 标准 ->行高大小: 标准
Byte快速转换为16进制字符串

上次delphi做的asm内嵌过程/函数格式是:

procedure procname(...);

asm

...

end;

  对这种过程/函数,整个由asm写成。内嵌asm还有一种常见用法,

就是在程序局部调用asm:

function bytetohex(src: byte): string;

begin

setlength(result, 2);

asm

mov edi, [result]

mov edi, [edi]

mov al, src

mov ah, al // save to ah

shr al, 4 // output high 4 bits

add al, '0'

cmp al, '9'

jbe @@outcharlo

add al, 'a'-'9'-1

@@outcharlo:

and ah, $f

add ah, '0'

cmp ah, '9'

jbe @@outchar

add ah, 'a'-'9'-1

@@outchar:

stosw

end;

end;

  该子程序可以实现把一字节的src转换为16进制形式的字符串。刚好

一位站友有这样的问题,够快了吧? :)

************************************

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

此文章由 http://www.ositren.com 收集整理 ,地址为: http://www.ositren.com/htmls/67745.html

相关