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 segmentstring1 db "Hello World!"count1 dw $-string1string2 db "Hello World!"count2 dw $-string2match db "MATCH",0ah,0dh,'$'nomatch db "NO MATCH",0ah,0dh,'$'
data ends
stack segment stackdw 100 dup (?)top label word
stack ends
code segmentassume cs:code,ss:stack,ds:data
main proc farmov ax,stackmov ss,axlea sp,topmov ax,datamov ds,axmov es,ax;判断串长,不相等直接跳转到Nmov ax,count1mov bx,count2cmp ax,bxjne N;串长相等,执行串比较操作lea si,string1lea di,string2mov cx,count1repe cmpsb ;串比较指令,(si)(di)二者相等继续往下比较,不相等退出jnz N ;ZF=0jcxz Y ;ZF=0并且cx=0,即比较完了整个串;相同显示MATCH
Y:lea dx,matchmov ah,9int 21hjmp exit;不相同显示NO MATCH
N:lea dx,nomatchmov ah,9int 21h exit:mov ah,4chint 21h
main endp
code endsend main