问题
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'
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
回答1:
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'
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]
来源:https://stackoverflow.com/questions/27464339/assembly-x86-splitting-string-based-on-delimiter