📄 字符串的查找.asm
字号:
stack segment stack 'stack'
dw 20 dup(?)
stack ends
;***********************************
data segment
crl db 13,10,'$'
str_buf db 20 dup(?)
count db 1h
a db 0h ;输入字符树组位移量
b db 0h ;原数组位移量
c db 0h ;第一次相等标记
d db 0h ;标记是否已经输出单词
e db 0h ;记录字符串长度
f db 0h ;已经到达最后
string db 00,24,'apply book cook dog zero ',40 dup(?)
meg db '$'
meg0 db 'Please input a word','$'
meg1 db 'The word has been insert into the string as follow','$'
meg2 db 'The position of the word is ','$'
meg3 db 'The word has not been found','$'
data ends
;********************************************************
code segment
main proc far
assume cs:code,ds:data,ss:stack
mov ax,data
mov ds,ax
mov si,0
mov di,0
start:
call print
call crlf
call input
call search
exit:
mov ah,4ch
int 21h
main endp
;-------------------------
input proc near ;输入单词
newchar:
mov ah,01h
int 21h
cmp al,' '
jz return
mov str_buf[si],al
inc si
jmp newchar
return:
mov cx,si
mov si,0
mov dx,0
ret
input endp
;----------------------------
search proc near ;查找单词
lea bx,string+2
campare:
mov al,[bx][di]
mov ah,str_buf[si]
cmp ah,al
jz nextchar
cmp al,' '
jz nextword
inc di
cmp c,1h
jnz continue
inc b
continue:
push bx ;是否到头
mov bh,0
mov bl,string[1]
cmp di,bx
pop bx
jae insert
jmp campare
nextword:
inc di
mov dx,di
mov si,0
mov b,0h
mov a,0h
mov c,0h
inc count
jmp campare
nextchar:
mov c,1h
inc di
inc b
inc si
inc a
cmp si,cx
jz iffound
jmp campare
iffound:
push ax
mov al,a
cmp al,b
pop ax
jnz p
call position
call printword
mov si,0
p:
inc di
mov al,[bx][di]
cmp al,' '
jz nextword
jmp p
insert:
cmp d,1
jz retp
push dx
call crlf
lea dx,meg3
mov ah,09h
int 21h
pop dx
call insertword
retp:
mov ah,4ch
int 21h
ret
search endp
;-------------------------
printword proc near ;打印找到的单词
mov d,1h
mov si,dx
push dx
push ax
next:
mov dl,[bx][si]
cmp dl,' '
jz ret1
mov ah,02h
int 21h
inc si
jmp next
ret1:
pop ax
pop dx
ret
printword endp
;---------------------------------------------
position proc near ;显示单词的位置
call blank
push dx
push ax
mov dl,count
add dl,30h
mov ah,02h
int 21h
mov dl,':'
mov ah,02h
int 21h
pop ax
pop dx
ret
position endp
;----------------------------
insertword proc near
mov di,0
mov si,0
mov dx,0
mov count,1h
mov al,string[1]
mov e,al
campare1:
lea bx,string+2
mov al,[bx][di]
mov ah,str_buf[si]
cmp ah,al
jz nextchar1
ja nextword1
jb insert1
nextchar1:
inc di
cmp di,e
jz ifend
inc si
jmp campare1
nextword1:
inc di
mov al,[bx][di]
cmp al,' '
jz p1
jmp nextword1
p1:
inc count
inc di
mov dx,di
mov si,0
jmp campare1
ifend:
mov f,1h
mov dx,di
insert1:
mov e,cl
inc e
push ax
push si
push di
mov al,string[1]
mov ah,0
mov si,ax
add al,e
mov di,ax
move:
mov ah,[bx][si]
mov [bx][di],ah
dec si
dec di
cmp si,dx
jnb move
mov di,dx
mov si,0
cpy:
mov al,str_buf[si]
mov [bx][di],al
inc di
inc si
cmp si,cx
jz rett
jmp cpy
rett:
mov al,' '
mov [bx][di],al
pop di
pop si
pop ax
call crlf
lea dx,meg1
mov ah,9
int 21h
mov al,string[1]
mov ah,0
call long
call output
ret
insertword endp
;------------------------------
long proc near
call crlf
add al,cl
inc al
cbw
mov ah,0 ;显示结果长度
mov cl,10d
div cl
mov dl,al
add dl,30h
push ax
mov ah,2
int 21h
pop ax
mov dl,ah
add dl,30h
mov ah,2
int 21h
ret
long endp
;-----------------------------
output proc near ;显示插入后的字符串
call blank
call position
lea dx,string+2
mov ah,9
int 21h
ret
output endp
;-------------------------------
print proc near ;输出提示语句
lea dx,meg0
mov ah,9
int 21h
ret
print endp
;--------------------------------------------
crlf proc near
push ax
push dx
lea dx,crl
mov ah,09h
int 21h
pop dx
pop ax
ret
crlf endp
;--------------------------------------
blank proc near
push dx
push ax
mov dl,' '
mov ah,2
int 21h
pop ax
pop dx
ret
blank endp
;---------------------------------------
code ends
end main
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -