CnPack Forum


 
Subject: Delphi汇编学习5(从右边开始查找某个子串的位置)
不得闲
灌水科科长
Rank: 3Rank: 3



UID 40680
Digest Posts 3
Credits 118
Posts 33
点点分 118
Reading Access 10
Registered 2008-7-23
Status Offline
Post at 2008-7-23 23:28  Profile | Blog | P.M. 
Delphi汇编学习5(从右边开始查找某个子串的位置)

从一个字符串的右边开始查找子字符串出现的位置,里面代有详细的注释,希望有能者,能搞出一个效率更高的算法出来
我这个只是用汇编实现了,并未考虑到算法
如:
RightPos('不得闲','123不得闲24不得闲')  
返回结果12既最后一个‘不得闲’ 出现的位置

{*************************************************************}
{*    名称: RightPos                                       *}
{*    功能: 在str串中从右边开始查找SubStr第一次出现的位置   *}
{*    作者: 不得闲                                          *}
{*    时间: 2008-07-23                                      *}
{*************************************************************}
function RightPos(Const SubStr,Str: string): integer;
asm
     push ebx
     push esi
     push edi
     mov  esi,[eax-4] //substr长度
     mov  edi,[edx-4] //str长度
     cmp  esi,edi //比较两个的长度
     ja   @@Exit  //大于退出
     mov  ecx,edi
     sub  ecx,esi      
     mov  edi,0
     push esi   //保存子字符串的长度
   @@DoFind:
     mov  ebx,ecx     
     add  ebx,edi
     mov  bl,byte ptr[edx+ebx] //str字符串的倒数Length(SubStr)字符开始
     mov  bh,byte ptr[eax+edi] //SubStr的第一个字符开始循环
     cmp  bl,bh
     jnz  @@Next //如果不相等,则进行下一个操作,将主字符串指针前移一个
     dec  esi    //否则继续向下比较
     jz   @@Ret
     inc  edi
     jmp  @@DoFind

   @@Next:
     Pop  esi  //还原子串长度,进行下一个操作
     dec  ecx
     mov  edi,0
     push esi //保存子串长度
     jmp  @@DoFind //移动主串指针后,继续查找
   @@Ret:
     pop  esi
     inc  ecx
     mov  eax,ecx
     jmp  @@Pop
   @@Exit:
     mov  eax,0
   @@Pop:
     Pop  edi
     pop  esi
     pop  ebx
end;

[ 本帖最后由 不得闲 于 2008-7-23 23:33 编辑 ]
Top
lixupeng
普通灌水员
Rank: 2



UID 33687
Digest Posts 0
Credits 66
Posts 31
点点分 66
Reading Access 10
Registered 2007-12-11
Status Offline
Post at 2008-7-24 22:00  Profile | Blog | P.M. 
看看
Top
 




All times are GMT++8, the time now is 2024-4-24 20:35

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.015826 second(s), 7 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP