程序集x86:基于定界符分割字符串 [英] Assembly x86: splitting string based on delimiter

查看:107
本文介绍了程序集x86:基于定界符分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:获取存储在EDI中的字符串一二三,并调用strtok。
strtok应该拆分字符串,以便ESI指向字符串的其余部分(在某些定界符之后)以及字符串的第一部分(在定界符存储在寄存器中之前)。
在调用strtok之后使用分隔符'',结果应为ESI ='Two Three',而其他寄存器='One'

Goal: Take string 'One Two Three' which is stored in EDI and call strtok. strtok should split the string so that ESI points to the rest of the string after some delimiter and the first part of the string before the delimiter is stored in a register. After Call strtok with the delimiter of ' ' the result should be ESI = 'Two Three' and some other register = 'One'

使用下面的代码我可以在存储在EDI中的字符串中找到空格''的第一个实例的索引。末尾的索引存储在EAX中,定界符定位后的其余字符串存储回edi中,然后移至esi。因此一二三 ESI变为二三。我的问题是如何跟踪定界符之前的字符串的第一部分。因此,在此示例中,当repne scasb删除字符串的第一部分直到定界符时,如何将 One存储在寄存器中。

Using the code below i can find the index of the first instance of a space ' ' in string stored at EDI. index at the end is stored in EAX, the rest of the string after the delimiter is located is stored back in edi and then moved to esi. so 'One Two Three' ESI becomes 'Two Three'. My question would be how do I keep track of the first part of the string before the delimiter. So in this example how do i store 'One' in a register when it looks like repne scasb deletes the first part of the string up until the delimiter.

    strtok:
        Mov Ecx, 0  ;reset ecx to 0
        Not Ecx     ;set Ecx to -1 or highest possible integer
        Mov Al, ' ' ;Initialize a1 to delimiter of (space) ' '
        Cld         ;Clear Direction Pointer
        Repne Scasb ;scan edi one byte at a time until delimiter found
        Not Ecx
        Dec Ecx
        Lea Eax, [Ecx] ;Set Eax to index of found delimiter

        Xchg Esi, Edi  ;Take Edi which is now equal to string after found delimiter and put in esi


推荐答案

只需添加 mov esi,edi 作为您的 strtok 例程的第一条指令。最后, xchg esi,edi 会给出所需的结果。 EDI将指向一个,ESI将指向两个三个。

Just add mov esi,edi as the first instruction of your strtok routine. The xchg esi,edi at the end will then give the desired result. EDI will point at 'One' and ESI will point at 'Two Three'

额外:您可以结合以下说明来获得更好的代码。现在,使用LEA似乎是多余的,因为简单的MOV可以解决问题。

也许会更改

Extra: You could combine the following instructions to get a nicer code. Now the use of LEA seems overkill because a simple MOV can do the trick.
Perhaps change

Dec Ecx
Lea Eax, [Ecx] ;Set Eax to index of found delimiter

into

lea eax, [ecx-1]

这篇关于程序集x86:基于定界符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆