assembly x86 string comparison (NASM)

Reserved string password, enter the reserved password string and string comparison. If the match is displayed "MATCH! CONGRATULATION", otherwise display "NOMATCH!", And allow the user to re-enter, the program can test the password, but the number of tests up to three times, if all wrong password three times, corresponding prompt information, the program exits.

 

Two approaches:

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

 

Guess you like

Origin www.cnblogs.com/lanclot-/p/10963477.html