.model small
.data
String1 db 50,?,50 dup(?)
String2 db 50,?,50 dup(?)
location db 0
prompt1 db 'please enter string1:$'
prompt2 db 0ah,0dh,'please enter string2:$'
match db 0ah,0dh,'match',0ah,0dh,'$'
nomatch db 0ah,0dh,'no match',0ah,0dh,'$'
quit db "press any key to exit......",13,10,'$'
.code
start:
mov ax,@data
mov ds,ax
mov es,ax
;input String1
mov dx,offset prompt1
mov ah,09h
int 21h
mov dx,offset String1
mov ah,0ah
int 21h
;input String2
mov dx,offset prompt2
mov ah,09h
int 21h
mov dx,offset String2
mov ah,0ah
int 21h
;process to judge if string2 is part of string1
mov al,String1+1
cmp al,String2+1
jb nosub ;如果String1长度大于String2的则不可能为子串
lea si,String1
lea di,String2
inc di
inc si
mov cx,[di];to store the lenth of string2
inc di
mov dx,di
mov al,String1+1
sub al,String2+1
inc al ;此时al的数值为两个长度之差,大1
loop1: ;实质是双重循环
dec al
cmp al,0
jb nosub
inc si;将主串的指针向前移动
mov bx,si
mov di,dx
cld ;让子串和主串相比,如果CX有机会变为0说明为子串
repe cmpsb ;实质这儿是个循环
jcxz substring
jmp midway
midway:
mov si,bx ;bx中暂存了si
jmp loop1
substring:
mov dx,offset match
mov ah,09h
int 21h
lea di,location
lea ax,String1
add ax,2
sub si,ax
mov [di],si ;here
xor dx,dx
mov dl,location
add dl,30h
mov ah,2
int 21h
jmp exit
nosub:
mov dx,offset nomatch
mov ah,09h
int 21h
jmp exit
exit:
lea dx,quit
mov ah,09h
int 21
mov ax,4c00h
int 21h
end start
对于 dec al
cmp al,0
jb nosub这些代码意思是:
例如'fhjahj'和'ee'长度差为4就是只有到'ee'和'hj'比较后就可以判断是否为子串了
大家帮忙下啊!