[汇编] 比较2个字符串是否相等

 

 1 ; multi-segment executable file template.
 2 
 3 data segment
 4     STRING DB 'SPACE EXPLORERS INC'
 5     PRLINE DB 'SPACE EXPLORERS INCE'
 6     LAST   DB ' '  
 7     same   DB 'MATCH$'
 8     nsame  DB 'NO MATCH$'
 9 ends
10 
11 stack segment
12     dw   128  dup(0)
13 ends
14 
15 code segment
16 start:
17 ; set segment registers:
18     mov ax, data
19     mov ds, ax
20     mov es, ax
21 
22     ; add your code here 
23     mov cx,PRLINE-STRING        ;先判断长度,长度不相等直接不匹配
24     cmp cx,LAST-PRLINE
25     jnz NEQUAL
26      
27     lea si,STRING               ;长度相等则逐个匹配
28     mov di,offset PRLINE
29     cld
30     rep cmpsb
31     jz  EQUAL
32     jnz NEQUAL
33     
34     EQUAL:                      ;输出结果
35     lea dx,same 
36     jmp NEXT 
37     NEQUAL:
38     lea dx,nsame
39     NEXT:
40     mov ah, 9
41     int 21h        ; output string at ds:dx
42     
43     ; wait for any key....    
44     mov ah, 1
45     int 21h
46     
47     mov ax, 4c00h ; exit to operating system.
48     int 21h    
49 ends
50 
51 end start ; set entry point and stop the assembler.

 

posted @ 2014-04-03 14:56  beautifulzzzz  阅读(5987)  评论(0编辑  收藏  举报