汇编实验:查找匹配字符串

来源:互联网 发布:淘宝男装品牌 知乎 编辑:程序博客网 时间:2024/05/22 17:28

2.2查找匹配字符串

      1.题目:查找匹配字符串

      2.实验要求:

    程序接收用户键入的一个关键字以及一个句子。如果句子中不包含关键字则显示’No match!’;如果句子中包含关键字则显示‘Match’,且把该字在句子中的位置用十六进制数显示出来。

;PROGRAM TITLE GOES HERE--search;*******************************datarea segmentmess1 db 'Enter keyword:','$'mess2 db 'Enter Sentence:','$'mess3 db 'Match at location:','$' mess4 db 'No match!',13,10,'$ 'mess5 db 'match.',13,10,'$ 'mess6 db 'H of the sentence.',13,10,'$ ';stoknin1 label bytemax1 db 10act1 db ?stokn1 db 10 dup(?);stoknin2 label bytemax2 db 50act2 db ?stokn2 db 50 dup(?)datarea ends;*********************************prognam segment;---------------------------------main proc farassume cs:prognam ,ds:datarea,es:datareastart:push ds sub ax,axsub bx,bxpush axmov ax,datareamov ds,axmov es,ax;MAIN PART-------------lea dx,mess1;输入关键字mov ah,09int 21hlea dx,stoknin1mov ah,0ahint 21hcmp act1,0je exit        a10:;输入句子call crlflea dx,mess2mov ah,09int 21hlea dx,stoknin2mov ah,0ahint 21hcmp act2,0je nmatchmov al,act1cbwmov cx,axpush cxmov al,act2 sub al,act1js nmatchmov di,0mov si,0lea bx,stokn2;inc ala20:mov ah,[bx+di];开始比较cmp ah,stokn1[si]               ;不等则转到bx+1jne a30                         ;bx+1        inc siinc di;inc bxdec cxcmp cx,0je matchjmp a20a30:inc bxdec alcmp al,0je nmatchmov si,0mov di,0pop cxpush cxjmp a20exit:   call crlfret nmatch:;no match则输出No matchcall crlflea dx,mess4mov ah,09int 21hjmp a10match:;match则输出位置信息call crlflea dx,mess3mov ah,09int 21hsub bx,offset stokn2inc bxcall trans lea dx,mess6mov ah,09int 21h                         jmp a10crlf proc near  ;回车,换行mov dl,0dhmov ah,2int 21hmov dl,0ahmov ah,2int 21hretcrlf   endptrans proc near ;转换为16进制,参考书上例6.3mov ch,4  ;number of digitsrotate: mov cl,4           ;set count to 4bitsrol bx,cl ;left digit to rightmov al,bl ;mov to aland al,0fh ;mask off left digitadd al,30h ;convert hex to ASCIIcmp al,3ah ;is it>9?jl printit ;jump if digit=0 to 9add al,7h ;digit is A to Fprintit:mov dl,al  ;put ASCII char in DLmov ah,2 ;Display Output functint 21h ;call DOSdec ch ;done 4 digits?jnz rotate ;not yetret ;return from transtrans endp;main endp;----------------------------------prognam ends;**********************************end start

运行结果:


0 0
原创粉丝点击