"Copyright Notice: respect for the original, reproduced please retain the source: blog.csdn.net/shallnet, the article only for learning Exchange, do not use for commercial purposes"
the CMPS directive is used to compare string values, and the CMPS directive has three formats: CMPSB, CMPSW, CMPSL. The implied source operand and target operand locations are stored in the ESI and EDI registers, and each time the cmps instruction is executed, the ESI and EDI registers are incremented or decremented according to the data length being compared, depending on the DF flag. The cmps instruction subtracts the target string from the source string and sets the carry, symbol, overflow, 0, parity, and abundance carry flags of the EFLAGS register appropriately. After the cmps instruction executes, you can jump to the branch by using the normal conditional jump instruction, based on the value of the string. The cmps directive, together with the rep directive, allows you to repeat comparisons of strings that span multiple bytes. However, the REP directive does not check the flag state between the two repeating processes, it only cares about the count value in the ECX register. So use the other directives in the rep directive: Repe, Repne, RepZ, REPNZ. These instructions check the 0 flag during each repetition and stop repeating if the 0 flag is set. The rep other directives use the following table:
directive |
description |
repe |
equals when repeating |
repne |
Not equal when repeating |
repnz |
No zero repeats |
RepZ |
Repeat for Zero |
Example:
#cmps. s.section. Dataval1: . ASCII "Hello as!" Val2: . ASCII "Hello wd!". Section. Text.globl _start_start: NOP movl $,%eax #system call Sys_exit () Leal Val1,%esi Leal Val2,%edi movl $9,%ecx cld repe cmpsb je equal movl %ecx,%ebx int $0x80equal: MOVL $,%ebx int $0x80
The program loads the location of the source and destination strings into the ESI and EDI registers, the length of the string is loaded into the ECX register, the Repe CMPSB instruction repeats the comparison of strings byte-by-bit, until the ECX register is 0, or the 0 flag is set (the description does not match). The results of the program execution are as follows:
$./cmps $ echo $?2
The ecx register will contain the position of the mismatched character in the string, which is counted back from the end of the string, starting at 0. the scanning of strings uses the SCAS directive, which provides a way to search for a character or multiple characters. The scas directive is similar to other string directives in three formats: SCANB, SCANW, SCANL, and three formats that compare the values of one byte in memory and the value of Al, AX, EAX registers, respectively. The SCAS directive uses the EDI register as the implied target operand. The EDI register must contain the memory address of the string to be scanned, and when the scas instruction is executed, the value of the EDI register is incremented or decremented by the length of the search character data. a very useful function of the SCAS directive is to determine the length of the string at the end of the 0, for a string ending in 0, to search for a position that is obviously 0, and to calculate how many characters are found in 0. The following example:
# scas.s.section. datastring: . Asciz "This was a test string!\n" . Section. Text.globl _start_start: NOP Leal string,%edi #将要用于查找的字符串的内存地址加载到edi寄存器中 movl $0xffff,%ecx #0xffff表明这个程序只能用于长度最大为65535的字符串 Movb $,%al #将要搜索的字符加载到al寄存器中 cld repne scasb #使用repne指令扫描字符串, get search location jne notfound # If not found, jump to NotFound branch SUBW $0xffff,%cx #如果找到了, then its position at the end of the string is stored in the CX register, subtracting the length of the string from the value of the CX register neg%CX #使用neg指令改变结果的值的符号 Dec%cx #因为该长度包含表示结尾的0, so the final value must be reduced by 1 to show the true length of the string. MOVL $,%eax movl%ecx,%ebx #将计算结果存放在ebx寄存器中. int $0x80notfound: movl $,%eax movl $,%ebx int $0x80
The result of running the program is as follows:
$./scas $ echo $?23
Linux Platform x86 compilation (13): Comparison and search of strings