datarea segment
string1 db "Enter keyword:$"
string2 db "Enter sentence:$"
string3 db "Match at location:$"
string4 db "No match.",13,10,"$"
string5 db "H of the sentence.$"
keyword db 50d,?,51d dup(?)
sentence db 50d,?,51d dup(?)
datarea ends


code segment
main proc far
 assume cs:code,ds:datarea,es:datarea
start:
      push ds
      sub ax,ax
      push ax

      mov ax,datarea
      mov ds,ax
      mov es,ax

      lea dx,string1
      mov ah,09h
      int 21h
      lea dx,keyword
      mov ah,0Ah
      int 21h
      mov ah ,02h
      mov dl,0ah
      int 21h      
      lea dx,string2
      mov ah,09h
      int 21h
      lea dx,sentence
      mov ah,0Ah
      int 21h
      mov ah,02h
      mov dl,0ah
      int 21h
      
      lea  si,keyword+2
      lea  di,sentence+2
      mov  ax,0
      mov  al,[sentence+1]
      mov  ah,[keyword+1]
      cmp  al,ah
      jl   no
      sub  al,ah
      mov  ah,0
      mov  cx,ax
      inc  cx
      
compare:
      push cx
      mov  cx,3
      cld      
      repz cmpsb
      jz   match      
      mov  ax,3
      sub  ax,cx
      sub  si,ax
      mov  ax,2
      sub  ax,cx
      sub  di,ax
      lea  bx,[di]
      pop  cx
      loop compare
no:   lea  dx,string4
      mov  ah,09h
      int  21h
      jmp  exit
match:
      lea  dx,string3
      mov  ah,09h
      int  21h
      sub  bx,word ptr sentence
      inc  bx
      call change
      lea  dx,string5
      mov  ah,09h
      int  21h
exit:
      ret
main endp


change proc near
      push ax
      push bx
      push cx
      push dx
      mov  ch,4
      mov  cl,4
      rol  bx,cl
      mov  al,bl
rotate:and al,0fh
      add  al,30h
      cmp  al,3ah
      jl   printit
      add  al,7h
printit:
      mov  dl,al
      mov  ah,2
      int  21h
      dec  ch
      jnz  rotate
      pop  dx
      pop  cx
      pop  bx
      pop  ax
      ret
change endp      
code ends
end start



查找匹配字符串
程序接收用户键入一个关键字以及一个句子,如果句子不包含关键字则显示“No match"
,如果句子包含关键字则显示match,且把该字在句子中的位置用16进制显示出来,
要求过程如下:
Enter keyword:abc
Enter sentence:We are studying abc.
Match at location :11H of the sentence 





[em6]