欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

assembly x86(nasm)串比较

发布时间:2025/3/20 编程问答 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 20db ?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,datamov ds,ax mov dx,offset message mov ah,9 int 21h mov cl,3jmp input rinput: mov dx,offset FAULT ;提示错误mov ah,9 int 21h input: lea dx, buf1 ;字符串输入mov ah, 0ah int 21hxor si,si strcmp: ;串比较mov al,passward[si]cmp al,'$'jz output2mov al,passward[si] cmp buf1[si+2],aljnz output1inc sijmp strcmp output1: loop rinputmov dx,offset ending mov ah,9 int 21hjmp 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,datamov ds,axmov ax,extmov es,ax mov dx,offset message mov ah,9 int 21h mov cx,3jmp input rinput: mov dx,offset FAULT ;提示错误mov ah,9 int 21h input: lea dx, buf1 ;字符串输入mov ah, 0ah int 21hlea di,passwardlea si,buf1+2push cxmov cl,buf1+1repz cmpsb ;当前字符相同则继续循环jz output2jnz output1 output1:pop cxloop rinputmov dx,offset ending mov ah,9 int 21hjmp 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

总结

以上是生活随笔为你收集整理的assembly x86(nasm)串比较的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。