Delphi 使用内联程序集计算数字位数
如何使用Delphi的内联汇编程序获得数字的位数 例如:Delphi 使用内联程序集计算数字位数,delphi,assembly,inline-assembly,Delphi,Assembly,Inline Assembly,如何使用Delphi的内联汇编程序获得数字的位数 例如: 13452 should return 5 1344 should return 4 9721343 should return 7 等 我的尝试是: function CalculateLength(Number : integer) : Integer; begin asm PUSH Length(Number) MOV @Result, EAX end; end; 我的做法: function co
13452 should return 5
1344 should return 4
9721343 should return 7
等
我的尝试是:
function CalculateLength(Number : integer) : Integer;
begin
asm
PUSH Length(Number)
MOV @Result, EAX
end;
end;
我的做法:
function count_of_digits (n:Integer) : Cardinal; {No problems for `n:Cardinal`}
var cnt : Cardinal;
begin
cnt := 0;
repeat
inc (cnt);
n := n div 10;
until (n=0);
count_of_digits := cnt;
end;
function count_of_digits_asm_signed (n:Integer) : Cardinal;
begin
asm
push ebx { An asm statement must preserve the EDI, ESI, ESP, EBP, and EBX registers }
xor ecx, ecx
mov ebx, 10 { Base 10 (decimal), just change it for another base }
mov eax, n
@L1:
add ecx, 1
cdq { Set EDX for `idiv` }
idiv ebx { Decimal shift right by one decimal digit }
test eax, eax
jne @L1
mov @result, ecx
pop ebx
end;
end;
function count_of_digits_asm_unsigned (n:Cardinal) : Cardinal;
begin
asm
push ebx { An asm statement must preserve the EDI, ESI, ESP, EBP, and EBX registers }
xor ecx, ecx
mov ebx, 10 { Base 10 (decimal), just change it for another base }
mov eax, n
@L1:
add ecx, 1
xor edx, edx { Set EDX for `div` }
div ebx { Decimal shift right by one decimal digit }
test eax, eax
jne @L1
mov @result, ecx
pop ebx
end;
end;
VAR
i: Integer;
c1, c2, c3: Cardinal;
BEGIN
i := 13452;
c1 := count_of_digits (i);
c2 := count_of_digits_asm_signed (i);
c3 := count_of_digits_asm_unsigned (i);
writeln (i:11, c1:3, c2:3, c3:3);
i := 1344;
c1 := count_of_digits (i);
c2 := count_of_digits_asm_signed (i);
c3 := count_of_digits_asm_unsigned (i);
writeln (i:11, c1:3, c2:3, c3:3);
i := 9721343;
c1 := count_of_digits (i);
c2 := count_of_digits_asm_signed (i);
c3 := count_of_digits_asm_unsigned (i);
writeln (i:11, c1:3, c2:3, c3:3);
i := -13452;
c1 := count_of_digits (i);
c2 := count_of_digits_asm_signed (i);
c3 := count_of_digits_asm_unsigned (i);
writeln (i:11, c1:3, c2:3, c3:3);
END.
下面是一个避免除法的更快算法:
function CountDigits(anInt: Cardinal): Cardinal; inline;
var
cmp: Cardinal;
begin
cmp := 10;
Result := 1;
while (Result < 10) and (cmp <= anInt) do
begin
cmp := cmp*10;
Inc(Result);
end;
end;
function CountDigitsAsm(anInt: Cardinal): Cardinal;
asm
mov ecx,$a // cmp := 10;
mov edx,$1 // Result := 1;
jmp @loop2
cmp eax,edx // while cmp <= anInt do
jb @done
@loop1:
add ecx,ecx // cmp := cmp*10;
lea ecx,[ecx+ecx*4]
inc edx // Inc(Result);
@loop2:
cmp edx,$0a // (Result < 10)
jnb @done
cmp eax,ecx
jnb @loop1
@done:
mov eax,edx
end;
begin
WriteLn(CountDigitsAsm(10));
WriteLn(CountDigitsAsm(99));
WriteLn(CountDigitsAsm(999));
WriteLn(CountDigitsAsm(9999));
WriteLn(CountDigitsAsm(99999));
ReadLn;
end.
以及展开的版本:
function CountDigitsUnrolled(anInt: Cardinal): Cardinal; inline;
begin
if (anInt < 10) then Result := 1 else
if (anInt < 100) then Result := 2 else
if (anInt < 1000) then Result := 3 else
if (anInt < 10000) then Result := 4 else
if (anInt < 100000) then Result := 5 else
if (anInt < 1000000) then Result := 6 else
if (anInt < 10000000) then Result := 7 else
if (anInt < 100000000) then Result := 8 else
if (anInt < 1000000000) then Result := 9 else
Result := 10;
end;
确定不同解决方案的时间:
Unrolled: 4097 ms
Case: 1444 ms
LUT: 3233 ms
pas: 6199 ms
asm: 6747 ms
测试代码:
sw := TStopWatch.StartNew;
for i := 1 to 1000000000 do
j := CountDigitsXXX(i);
WriteLn(sw.ElapsedMilliseconds,' ',j);
procedure TestXXX(var Distribution: array of Double);
var
sw: TStopWatch;
i,j,k,m: Cardinal;
const
StartIx: array[0..9] of Cardinal = ( 0,10,100,1000,10000,100000,1000000,
10000000,100000000,100000000);
StopIx: array[0..9] of Cardinal = ( 9,99,999,9999,99999,999999,9999999,
99999999,999999999,$FFFFFFFF);
Repeats: array[0..9] of Cardinal = (10000000,1000000,100000,10000,1000,100,10,1,1,1);
begin
for k := 0 to 9 do begin
sw := TStopWatch.StartNew;
for m := 1 to Repeats[k] do
for i := StartIx[k] to StopIx[k] do
j := CountDigitsXXX(i);
Distribution[k] := sw.ElapsedMilliseconds*1000000.0/(1.0*Repeats[k]*(StopIx[k]- StartIx[k] + 1));
WriteLn(sw.ElapsedMilliSeconds,' ',j);
end;
end;
附录 受到, 下面是一个Delphi实现,它是一个O(1)解决方案:
function OpenBit(AValue: Cardinal): Cardinal; register;
asm // Highest bit set
BSR EAX, EAX
end;
function CountDigitsO1(value: Cardinal): Cardinal; inline;
const
Powers: array[0..9] of Cardinal = (
0,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000);
MaxDigits: array[0..32] of cardinal =
(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5,
6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10);
begin
Result := MaxDigits[OpenBit(value)];
if (value < Powers[Result-1]) then
Dec(Result);
end;
测试代码:
sw := TStopWatch.StartNew;
for i := 1 to 1000000000 do
j := CountDigitsXXX(i);
WriteLn(sw.ElapsedMilliseconds,' ',j);
procedure TestXXX(var Distribution: array of Double);
var
sw: TStopWatch;
i,j,k,m: Cardinal;
const
StartIx: array[0..9] of Cardinal = ( 0,10,100,1000,10000,100000,1000000,
10000000,100000000,100000000);
StopIx: array[0..9] of Cardinal = ( 9,99,999,9999,99999,999999,9999999,
99999999,999999999,$FFFFFFFF);
Repeats: array[0..9] of Cardinal = (10000000,1000000,100000,10000,1000,100,10,1,1,1);
begin
for k := 0 to 9 do begin
sw := TStopWatch.StartNew;
for m := 1 to Repeats[k] do
for i := StartIx[k] to StopIx[k] do
j := CountDigitsXXX(i);
Distribution[k] := sw.ElapsedMilliseconds*1000000.0/(1.0*Repeats[k]*(StopIx[k]- StartIx[k] + 1));
WriteLn(sw.ElapsedMilliSeconds,' ',j);
end;
end;
我投票结束这个问题,因为它只是要求代码。不,我已经添加了我的尝试。我不是在问代码,我是在寻求帮助。你在找什么?您的问题是否已详细说明?如何迎合负面价值观?为什么需要使用asm?你对asm有什么了解吗?您有性能限制吗?它们是什么?您将如何处理函数的输出?您是希望我们编写代码,还是渴望学习?请先用Delphi编写。然后查看反汇编(调试),并查看生成的代码类型。试着理解如何自己编写。asm的问题包括:1。使用内嵌到Pascal中的asm。那很危险。使用纯asm函数。2.在asm中使用变量名。这是脆弱的,因为它隐藏了寄存器的使用。3.推到堆栈上,但不弹出。4.一厢情愿地认为
Length(…)
会神奇地满足您的需求。这表明你的理解和现实之间存在巨大的脱节。5.莫名其妙的MOV声明。基本上,你不知道有足够的基础来获得成功的希望。这听起来可能有点刺耳,但我想帮你。你需要获得这个基础,那是巨大的昂贵,执行那些可以用来进行比较的划分。将asm内联到Pascal函数中是不好的做法。而且,使用Pascal变量名也会带来麻烦。将结果放在eax中,而不是引用一个名称。类似这样的事情:要求汇编的问题。在Pascal中回答会隐藏一些汇编问题,如保留寄存器、处理堆栈帧或调用约定。@rkhb,我的答案包括汇编,但在这种情况下,我建议不要使用这种解决方案,因为在这种情况下很难击败编译器(内联asm是不可能的)。呜呼!我的案例
赢了这一轮:)@GJ。这对于这个循环测试是正确的,但是谁知道这个函数使用的数据。。。出于好奇,我检查了为值1(从第一行代码返回)不断返回展开的循环需要多长时间。即使在那时,我的机器上的案例也快了一点(旧i5,app.XE8 32位,64位,版本配置,默认设置)。我还没有检查生成的代码,但是我还希望案例的速度会慢一些。无论如何,我不会深入探讨这个问题。这个问题不是关于性能的…@SilverWarior,是的,这很简单,但性能不太好(在asm中编写代码很难)。非常好的方法,使用准Ln2和LUT。
procedure TestXXX(var Distribution: array of Double);
var
sw: TStopWatch;
i,j,k,m: Cardinal;
const
StartIx: array[0..9] of Cardinal = ( 0,10,100,1000,10000,100000,1000000,
10000000,100000000,100000000);
StopIx: array[0..9] of Cardinal = ( 9,99,999,9999,99999,999999,9999999,
99999999,999999999,$FFFFFFFF);
Repeats: array[0..9] of Cardinal = (10000000,1000000,100000,10000,1000,100,10,1,1,1);
begin
for k := 0 to 9 do begin
sw := TStopWatch.StartNew;
for m := 1 to Repeats[k] do
for i := StartIx[k] to StopIx[k] do
j := CountDigitsXXX(i);
Distribution[k] := sw.ElapsedMilliseconds*1000000.0/(1.0*Repeats[k]*(StopIx[k]- StartIx[k] + 1));
WriteLn(sw.ElapsedMilliSeconds,' ',j);
end;
end;