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 段之目的变址指针。 

猜你喜欢

转载自www.cnblogs.com/lanclot-/p/10963477.html