汇编:查找匹配字符串

功能要求

程序接收用户键入的一个关键字以及一个句子。如果句子中不包含关键字则显示‘No match!’;如果句子中包含关键字则显示‘Match’,且把该字在句子中的位置用十六进制数显示出来。
assume cs:code, ds:data, ss:stack
data segment
      tips db 'The location of the keyword is: 0X','$'
     match db 'Match!','$'
    nmatch db 'No Match!','$' 
    intips db 'Please enter the string to be processed:',0ah,0dh,'$'
    keytip db 'Please enter keywords:',0ah,0dh,'$' 
    crlf   db 0ah,0dh,'$' 
    
    strbuf db 30,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
           ;db dup100(0)
    keybuf db 30,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
           ;db dup100(0) 
     table db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','$'
data ends

stack segment
    db 100 dup(0)
stack ends

code segment
start: 
      mov ax,data
      mov ds,ax
      mov es,ax
      
      call inandout        ;数据的输入 
      
      lea si,strbuf+2      ;将第一个字符串给SI
      lea di,keybuf+2      ;将第二个字符串给DI
      mov al,strbuf+1
      mov ah,keybuf+1  
      mov bx,si
      sub al,ah
      mov ah,0
      mov cx,ax
      inc cx
       
  lop:push cx
      push di
      push si
      mov ch,0      
      mov cl,keybuf+1 
      cld                 
      repz cmpsb         
      jz mat 
       
      pop si
      pop di             
      pop cx
      add si,1 
      loop lop 
                             
                             
      jmp nomat
      
mat: lea dx,match
     call show
     sub si,bx
     mov bx,si  
     sub bx,2
     
     call show_hex
      
     jmp exit   
     
nomat:
     lea dx,nmatch
     call show 
     call crl
     jmp start 
         

exit:  mov ax,4c00h
      int 21h                
               
input:
      mov ah,0ah
      int 21h
      ret
show:      
     mov ah,09h
     int 21h     
     ret  
crl: lea dx,crlf
     call show
     ret  
     
inandout:
      mov ax,data
      mov ds,ax 
      
      lea dx,intips        ;输入提示
      call show  
      
      lea dx,strbuf        ;输入字符串
      call input 
      
      mov al,strbuf+1
      add al,2
      mov ah,0
      mov bx,ax
      mov strbuf[bx],'$'   ;结束标志
      
      call crl             ;换行
                           
      lea dx,keytip        ;关键字提示
      call show 
                             
      lea dx,keybuf         ;输入关键字
      call input  
      
      mov al,keybuf+1
      add al,2
      mov ah,0
      mov bx,ax
      mov keybuf[bx],'$'    ;结束标志
      
      call crl
      ret  
      
show_hex:
        mov al,bl
        call showbyte
        ret
showbyte: 
        jmp short hshow

 hshow: push bx
        push es
        push cx

        mov cl,4             ;高低四位进行分离
        mov ah,al
        shr ah,cl
        and al,00001111b 
        push ax
        
        call crl
        lea dx,tips
        call show 
        pop ax
        push ax
        call show_h4
        pop ax
        call show_l4
        
        pop cx
        pop es
        pop bx
        ret
                             ;高四位字符偏移地址
show_h4:mov bl,ah
        mov bh,0
        mov dl,table[bx]                      
        mov ah,2
        int 21h
        ret
                               ;低四位字符偏移地址
show_l4:mov bl,al
        mov bh,0
        mov dl,table[bx]
        mov ah,2
        int 21h
        
        




  
end start
code ends

PS: 程序比较简单,可以在字符串搜索部分加一些算法提交效率,平时划水严重,还是应付一下复习概率论吧

posted @ 2021-06-08 14:25  剪水行舟  阅读(310)  评论(0)    收藏  举报