18. String processing
The preceding article describes the processing of strings, which are arrays of type Byte, and now implement a piece of code to copy the string string1 data into the string string2
The code is as follows
" Hello world! " 0 0 . Codemov-mov ebx,0. Repeatmov al, String1[ebx]mov string2[ebx], AlInc Ebx.untilcxz
by ecx Decrement, the string string1 each character at once to string2, where the EBX base register is used.
can also be passed through ESI and EDI registers
" Hello world! " 0 . Codemov ecx,Lea esi, string1 Lea EDI, String2.repeat mov Al,[esi] mov [Edi],al Inc EDI Inc Esi.untilcxz
This code can be replaced by other instructions for string manipulation, making the code more concise
1 MOVSB Instructions
MOVSB is a string move instruction that moves the characters that ESI points to the space that EDI points to, and reduces the ecx by 1, registers ESI and EDI content by 1 or decreases by 1
If the only use of MOVSB instruction is not small, with the use of recycling, the function is very powerful. The CLD directive indicates that the direction flag value is clear 0, and calling MOVSB will increase the ESI and EDI content by 1
The STD directive represents setting the direction flag value, and calling MOVSB will reduce ESI and EDI content by 1
Or the above demand, this time using MOVSB to complete the goal
mov ecx,12 string size is mov esi, offset string1 ; esi points to string1 start position mov edi, offset string2 ; EDI point to string2 start position CLD After the CLD directive is called, calling MOVSB causes ESI and EDI to add 1 respectively .repeat MOVSB . Untilcxz
Several prefixes used in conjunction with MOVSB:
Rep repeat operation, equals loop until ECX is 0 exit loop
Repe if equal, then repeat, if not, exit the loop, or ECX 0 to exit the loop
Repne not equal, repeat operation, equal exit loop, or ECX to 0 exit loop
The above code can be changed using rep MOVSB to accomplish the same purpose
mov A Lea ESI, string1 Lea EDI, string2 CLD Rep MOVSB ; Loop until ECX is 0 end
2 SCASB Instructions
The SCASB directive is used to search for the characters stored by AL in the space specified by the EDI register. The SCASB instruction operation flow is: the character that will be queried is placed in the AL Register,
If a matching character is to be found in string strings, the EDI is stored with the first address of the string, called SCASB, and the SCASB instruction calls the EDI
Storing the numeric value plus 1, and ecx value-1, in order to complete string-by-character matching in strings, you need to precede the SCASB with the REPNE directive, so that when unequal
Finds the exit loop until the specified character is found. At this point, the EDI points to the next position in the string that matches the character position.
For example:
. dataname1 byte"Abe Lincoln". CodemovAl' ';storing spaces in the AL register in order to find the space in name1movECX, Lengthof name1;ECX stores name1 string lengths for eachLeaEDI, name1;EDI point to name1 first addressRepne SCASB;matches the characters and spaces of the EDI point to space are equal, and the EDI plus 1 is not equal ;ecx minus 1 until equal or ECX 0 exits the loop
The position of the character L is executed after run ECX for 7,edi
As follows:
3 STOSB Instructions
The STOSB is used to place the data stored by the Register Al into the space pointed to by the Register EDI, and the EDI increases by 1 after the instruction is completed.
STOSB is equivalent to:
mov [edi],al
Inc EDI
4 LODSB Instructions
The LODSB is used to store the spatial data that the register ESI points to in the Al register, and ESI increases by 1 after the instruction is completed.
LODSB is equivalent to:
mov al, [esi]
Inc ESI
The following implements the inverted string name function and adds the first and last names to the middle of the character "" and ","
Like "Abe Lincoln" into "Lincoln, Abe"
. dataname1 byte"Abe Lincoln"name2 byte ADUP (?) . CodemovAl"'movecx, Lengthof name1LeaEDI, name1Repne SCASBPushecxmovESI, EDILeaEDI, name2Rep MOVSBmovAl' 'STOSB movAl' 'STOSBmovecx, Lengthof name2PopeaxSubecx, eaxDececxLeaesi, name1Rep MOVSB
5 CMPSB Instructions
The string comparison directive, which is similar to MOVSB, compares EDI and ESI to spatial data size, and EDI and esi increase by 1 or reduce by 1,
Can be set via CLD or STD, and ecx reduced by 1
Compare "James" and "James"
The code is as follows:
" James " "James". Codemov ecx, Lengthof name1Lea esi, name1Lea EDI, name2cldrepecmpsb
In equal cases, ECX is the next position for both 0,edi and ESI to point to the last character of the string.
If the characters you compare are "James" and "Jamie", the effect is as follows:
If the comparison character is "Marci" and "Marcy", then the above code will not be able to distinguish between equality, because at this time ECX is also 0
If ECX is greater than 0, then two strings are definitely not equal, and if ECX is 0, then ESI and EDI are all 1, getting the last element,
Then compare ESI and EDI to the spatial data.
Let's refine the above code to compare two strings of equal lengths name1 and Name2
movecx, Lengthof name1Leaesi, name1LeaEDI, name2CLD Repe CMPSB. If ecx >0;Output Not Equal. ElseDecESIDecEDImoval, [esi]. if al! = [edi];Output Not Equal. Else;Output Equal. Endif.endif
19. String Summary
The 1 MOVSB instruction moves the contents of the byte-type string that the register ESI points to, where the register EDI points, and whether ESI and EDI are increased or decreased by CLD or STD
2 The CMPSB instruction compares a character in a string that is pointed to by the ESI of a register with one character that is pointed to by EDI, and whether ESI and EDI are increased or decreased by CLD or STD
The rep prefix in the front of the 3 MOVSB directive causes the instruction to be executed repeatedly, the number of cycles equals the value of ECX, and the value of each loop ecx is reduced by 1 until 0 stops.
The Repe prefix in front of the 4 CMPSB Directive acts like the rep prefix, and when ECX is 0, it stops executing the instruction. Or, when ESI and EDI point to a different byte content in the two space, the loop stops.
Repne stops when ESI and EDI point to the same byte content in two spaces.
The 5 SCASB instruction searches a string for the presence of characters stored in the Register AL in the string. If the character is found, then EDI points to an address that is a byte behind the address of the character.
STOSB stores the contents of the Al register in the location of the string that the EDI points to. The LODSB instruction copies the characters in the position of the string that the register ESI points to in the AL register.
My public number, thank you for your attention:
Language learning Notes (vi)