zoukankan      html  css  js  c++  java
  • assembly x86(nasm)串比较

    预留字符串口令,输入口令串与预留密码串比较。若匹配则显示“MATCH!CONGRATULATION”,否则显示“NOMATCH!”,并让用户重新输入,程序能对口令进行测试,但测试次数最多3次,若3次输入密码皆错,给出相应的提示信息,程序退出。

    两种做法:

    data    segment
    message        db    'This is a sample program of passward'
                db    0dh,0ah,'Please strike the key!',0dh,0ah,'$'
    passward     db     'huangchangsheng$'
    buf1         db    20
                db  ?
                db     20 dup('$')                
    FAULT         db     0dh,0ah,'NOMATCH!Please enter again!',0dh,0ah,'$'
    RIGHT         db     0dh,0ah,'MATCH!CONGRATULATION!',0dh,0ah,'$'
    ending        db    0dh,0ah,'You have typed the wrong passward for three times!',0dh,0ah,'$'
    data    ends
    code    segment
    assume    cs:code,ds:data
    start:        
                mov    ax,data
                mov    ds,ax            
                mov    dx,offset message            
                mov    ah,9                        
                int    21h    
                mov cl,3
                jmp input
    rinput:        
                mov    dx,offset FAULT                ;提示错误
                mov    ah,9                        
                int    21h
    input:        
                lea    dx, buf1                     ;字符串输入
                mov ah, 0ah                        
                int 21h
                xor si,si            
    strcmp:                                        ;串比较
                mov al,passward[si]
                cmp al,'$'
                jz output2
                mov al,passward[si]                
                cmp buf1[si+2],al
                jnz output1
                inc si
                jmp strcmp            
    output1:    
                loop rinput
                mov    dx,offset ending        
                mov    ah,9                        
                int    21h
                jmp exit            
    output2:    
                mov    dx,offset RIGHT                ;提示正确
                mov    ah,9                        
                int    21h            
    exit:        
                mov    ah,4ch                                    
                int    21h
    code    ends
    end    start

    第二种比较难受,因为di和附加段搞了好久,然后是看大佬代码才发现自己错哪了的

    https://blog.csdn.net/pengwill97/article/details/79249631传送门

    data    segment
    message        db    'This is a sample program of passward'
                db    0dh,0ah,'Please strike the key!',0dh,0ah,'$'
    buf1         db     20,?,20 dup('$')                
    FAULT         db     0dh,0ah,'NOMATCH!Please enter again!',0dh,0ah,'$'
    RIGHT         db     0dh,0ah,'MATCH!CONGRATULATION!',0dh,0ah,'$'
    ending        db    0dh,0ah,'You have typed the wrong passward for three times!',0dh,0ah,'$'
    data    ends
    ext segment
    passward db 'huangchangsheng$'
    ext ends
    code    segment
    assume    cs:code,ds:data,es:ext
    start:        
                mov    ax,data
                mov    ds,ax
                mov ax,ext
                   mov es,ax            
                mov    dx,offset message            
                mov    ah,9                        
                int    21h    
                mov cx,3
                jmp input
    rinput:        
                mov    dx,offset FAULT                ;提示错误
                mov    ah,9                        
                int    21h
    input:        
                lea    dx, buf1                     ;字符串输入
                mov ah, 0ah                        
                int 21h
                lea di,passward
                lea si,buf1+2
                push cx
                mov cl,buf1+1
                repz cmpsb                        ;当前字符相同则继续循环
                jz output2
                jnz output1            
    output1:
                pop cx
                loop rinput
                mov    dx,offset ending        
                mov    ah,9                        
                int    21h
                jmp exit            
    output2:    
                mov    dx,offset RIGHT                ;提示正确
                mov    ah,9                        
                int    21h            
    exit:        
                mov    ah,4ch                                    
                int    21h
    code    ends
    end    start

    如果使用repz cmpsb,密码应该放在附加段,不然可能会出bug,原因可能是di是目的变址寄存器,可用来存放相对于 ES 段之目的变址指针。 

  • 相关阅读:
    总体设计
    需求分析概述
    毕业论文管理系统(面向对象方法)
    结构化与面向对象项目前期
    各人博客园地址链接
    软件测试
    读后感作业
    运行及总结
    图书馆管理系统面向对象编程
    图书管理系统设计类图
  • 原文地址:https://www.cnblogs.com/lanclot-/p/10963477.html
Copyright © 2011-2022 走看看