汇编语言 CMPS 指令
CMPS 指令比较两个字符串。此指令比较 DS:SI 和 ES:DI 寄存器指向的单字节、字或双字的两个数据项,并相应设置标志。您还可以将条件跳转指令与此指令一起使用。
以下实例演示如何使用 CMPS 指令比较两个字符串:
section .textglobal _start ;must be declared for using gcc_start: ;tell linker entry pointmov esi, s1mov edi, s2mov ecx, lens2cldrepe cmpsbjecxz equal ;jump when ecx is zero;If not equal then the following codemov eax, 4mov ebx, 1mov ecx, msg_neqmov edx, len_neqint 80hjmp exitequal:mov eax, 4mov ebx, 1mov ecx, msg_eqmov edx, len_eqint 80hexit:mov eax, 1mov ebx, 0int 80hsection .datas1 db 'Hello, world!',0 ;our first stringlens1 equ $-s1s2 db 'Hello, there!', 0 ;our second stringlens2 equ $-s2msg_eq db 'Strings are equal!', 0xalen_eq equ $-msg_eqmsg_neq db 'Strings are not equal!'len_neq equ $-msg_neq
结果如下:
Strings are not equal!