一个汇编问题(世界上最快的字符替换函数)(100分)

  • 主题发起人 主题发起人 DreamTiger
  • 开始时间 开始时间
amo:ja果然ok。问个问题,cmp和test哪个效率高?
 
我觉得是 test,
cmp 做的是 减法, test 做的是 and
 
我把它改成这样了,原来的程序'[' 和 '{'是分不清的,试试看就知道了,
因为它的小写字母转大写字母有bug,并非使用正常的方法转换,虽然我十分
佩服这个小函数,但是问题是需要写清楚的.另外,我把它整个改成汇编子程
序了,这样也许速度可以得到进一步优化.
凡有"//Add by SunLujiang "字样的就是我加上的内容,而
"//Remove by SunLujiang"字样的则是我去掉的部分,希望高手们不要骂
我瞎改才好.
//This routine is an identical copy of FastPOS except where commented! The ide
//a is that when grabbing bytes, it ANDs them with $df, effectively making the
//m lowercase before comparing. Maybe this would be quicker if aFindString was
// made lowercase in one fell swoop at the beginning of the function, saving a
//n AND instruction each time.
function FastPosNoCase(
const aSourceString, aFindString : String;
const aSourceLen, aFindLen, StartPos : integer
) : integer;Pascal

// Remove by SunLujiang
{var
SourceLen : integer;
begin
SourceLen := aSourceLen;
SourceLen := SourceLen - aFindLen;
if (StartPos-1) > SourceLen then begin
Result := 0;
Exit;
end;
SourceLen := SourceLen - StartPos;
SourceLen := SourceLen +2;}
// Remove end

asm
push ESI
push EDI
push EBX

// Add by SunLujiang
Mov ECX, aSourceLen
Mov EAX, aFindLen
Sub ECX, EAX
Jo @Result0
Mov EAX, StartPos
Dec EAX
Sub ECX, EAX
Jo @Result0
Inc ECX
Inc ECX
// Add end

mov EDI, aSourceString
add EDI, StartPos
Dec EDI
mov ESI, aFindString

// Remove by SunLujiang
// mov ECX, SourceLen
// Remove end
Mov Al, [ESI]

// Add by SunLujiang
Cmp Al,97
Jb @NotLowerCase1
Cmp Al,122
Jnb @NotLowerCase1
// Add end

// Make Al lowercase.
and Al, $df

// Add by SunLujiang
@NotLowerCase1:
// Add end

@ScaSB:
Mov Ah, [EDI]

// Add by SunLujiang
Cmp Ah,97
Jb @NotLowerCase2
Cmp Ah,122
Jnb @NotLowerCase2
// Add end

// Make Ah lowercase.
and Ah, $df

// Add by SunLujiang
@NotLowerCase2:
// Add end

cmp Ah,Al
jne @NextChar
@CompareStrings:
mov EBX, aFindLen
dec EBX

//add by ShengQuanhu
Jz @EndOfMatch
//add end

@CompareNext:
mov Al, [ESI+EBX]
mov Ah, [EDI+EBX]

// Add by SunLujiang
Cmp Al,97
Jb @NotLowerCase3
Cmp Al,122
Jnb @NotLowerCase3
// Add end

// Make Al and Ah lowercase.
and Al, $df

// Add by SunLujiang
@NotLowerCase3:
Cmp Ah,97
Jb @NotLowerCase4
Cmp Ah,122
Jnb @NotLowerCase4
// Add end

and Ah, $df

// Add by SunLujiang
@NotLowerCase4:
// Add end

cmp Al, Ah
Jz @Matches
Mov Al, [ESI]
// Make Al lowercase.
and Al, $df
Jmp @NextChar
@Matches:
Dec EBX
Jnz @CompareNext

//add by Shengquanhu
@EndOfMatch:
//add end

mov EAX, EDI
sub EAX, aSourceString
inc EAX
mov Result, EAX
jmp @TheEnd
@NextChar:
Inc EDI
dec ECX
jnz @ScaSB

//add by SunLujiang
@Result0:
//add end

mov Result,0
@TheEnd:
pop EBX
pop EDI
pop ESI
// Remove by SunLujiang
// end;
// Remove end
end;

 
cartersun:非常感谢您的程序。但是这个函数最致命的问题,还在于对
中文的支持不够。什么意思呢?它在搜索aSourceString的
时候,并没有考虑到中文,它是完全按照一个字符一个字符
来进行的:
@NextChar:
Inc EDI
dec ECX
jnz @ScaSB
我想,如果改成:
Inc EDI
dec ECX
jz @Result0
cmp ah 128
jb @ScaSB
Inc EDI
dec ECX
jnz @ScaSB
是不是可以?我对汇编不是很了解,希望你能帮我看看,这样有
没有问题。

 
多人接受答案了。
 

Similar threads

I
回复
0
查看
711
import
I
I
回复
0
查看
670
import
I
I
回复
0
查看
739
import
I
I
回复
0
查看
805
import
I
I
回复
0
查看
647
import
I
后退
顶部