汇编语言-比较字符串

 比较两个字符串

1. 题目:比较字符串是否相等

2. 要求:写一程序,比较两个字符串String1和String2所含的字符是否相同;若相同则显示’Match’,否则显示’No Match’。

输入两个字符串之后,将串操作所必须的寄存器等参数设置好,然后使用串操作指令进行从头到尾的比较,两个字符串相等的条件是串长度相等且对应的字符相同。

 1 ; Example assembly language program -- 
 2 ; Author: karllen
 3 ; Date:    revised 05/2014
 4 
 5 .386
 6 .MODEL FLAT
 7 
 8 ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
 9 
10 INCLUDE io.h            ; header file for input/output
11 
12 cr      EQU     0dh     ; carriage return character
13 Lf      EQU     0ah     ; line feed
14 
15 .STACK  4096            ; reserve 4096-byte stack
16 
17 .DATA     
18        str1    BYTE 80 DUP(?)
19        str2    BYTE 80 DUP(?)
20        value   BYTE 11 DUP(?)
21        length1 DWORD ?
22        length2 DWORD ?  
23        
24        promot1 BYTE "Please Enter String1",cr,Lf,0
25        promot2 BYTE "Please Enter String2",cr,Lf,0
26        crlf    BYTE  cr,Lf,0
27        
28        answerYes BYTE "Match",cr,Lf,0
29        answerNo  BYTE "No Match",cr,Lf,0       
30 PUBLIC _start                   ; make entry point public       
31 .CODE                          ; start of main program code             
32 _start:
33        output promot1
34        input  str1,80
35        lea    eax,str1
36        push   eax
37        call   strlen
38        add    esp,4
39        mov    length1,eax
40        dtoa   value,eax
41        output value
42        output crlf
43        
44        output promot2
45        input  str2,80
46        lea    eax,str2
47        push   eax 
48        call   strlen   
49        add    esp,4  
50        mov    length2,eax
51        dtoa   value,eax
52        output value
53        output crlf
54        
55        mov  edx,length2
56        ;;cmp String1 and String2
57        cmp    eax,edx        ;如果长度不相等
58        jne    endCMP              ;则结束
59        ;比较
60        lea esi,str1
61        lea edi,str2
62        mov ecx,length2  ;比较的长度
63        repe cmpsb 
64        jz   found       ;比较成功则跳转
65       
66        endCMP:
67               output answerNo
68               jmp endMatch
69        found:
70               output answerYes
71         ;
72        endMatch:
73        
74         INVOKE  ExitProcess, 0  ; exit with return code 0
75 
76 strlen      PROC    NEAR32
77             push    ebp             
78             mov     ebp, esp
79                                        
80             sub     eax, eax        
81             mov     ebx, [ebp+8]    
82 whileChar:  cmp     BYTE PTR [ebx], 0  
83             je      endWhileChar   
84             inc     eax           
85             inc     ebx            
86             jmp     whileChar      
87 endWhileChar:
88             pop     ebp
89             ret               
90 strlen      ENDP
91 END                             ; end of source code

 

转载于:https://www.cnblogs.com/Forever-Kenlen-Ja/p/3734430.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值