CMPS 串比较指令:
CMPS SRC, DST
CMPSB (字节)
CMPSW (字)
执行操作:
((SI)) - ((DI))
根据比较结果置条件标志位:相等 ZF=1;不等 ZF=0
字节操作:(SI)←(SI)±1, (DI)←(DI)±1;
字操作: (SI)←(SI)±2, (DI)←(DI)±2
;比较两个字符串STRING1和STRING2所含字符是否完全相同
;若相同则显示‘MATCH’,若不相同则显示‘NO MATCH’
data segment
string1 db "Hello World!"
count1 dw $-string1
string2 db "Hello World!"
count2 dw $-string2
match db "MATCH",0ah,0dh,'$'
nomatch db "NO MATCH",0ah,0dh,'$'
data ends
stack segment stack
dw 100 dup (?)
top label word
stack ends
code segment
assume cs:code,ss:stack,ds:data
main proc far
mov ax,stack
mov ss,ax
lea sp,top
mov ax,data
mov ds,ax
mov es,ax
;判断串长,不相等直接跳转到N
mov ax,count1
mov bx,count2
cmp ax,bx
jne N
;串长相等,执行串比较操作
lea si,string1
lea di,string2
mov cx,count1
repe cmpsb ;串比较指令,(si)(di)二者相等继续往下比较,不相等退出
jnz N ;ZF=0
jcxz Y ;ZF=0并且cx=0,即比较完了整个串
;相同显示MATCH
Y:
lea dx,match
mov ah,9
int 21h
jmp exit
;不相同显示NO MATCH
N:
lea dx,nomatch
mov ah,9
int 21h
exit:
mov ah,4ch
int 21h
main endp
code ends
end main